How to test tooltip text with MouseOver using Selenium webdriver

I have a small luggage icon that displays a tooltip when you hover over the icon. I want to verify this by writing C # code in Visual Studio.

Here's how the luggage icon is displayed in html:

<div class="icon_png information icon_baggageyes" title="1 piece included in this fare."></div> 

And here is my code for checking the tooltip:

 Actions a = new Actions(driver); IWebElement tooTipObject = driver.FindElement(By.XPath("//div[@class='icon_png information icon_baggageyes']")); a.MoveToElement(tooTipObject).Click().Build().Perform(); 

The problem is that I am debugging the code above step by step, a tooltip text will appear. But when I run the test, the tooltip text now displays.

When searching the Internet, some people say that the reason is that the mouse does not focus on the baggage icon long enough for the tooltip to display.

But how to solve this problem? I was looking for solutions, but could not find one that works on mine.

And also my question: a.MoveToElement(tooTipObject).Click().Build().Perform(); tooltip text will only be displayed if I put .Click() in this code. But I check the mouse over the function, do not click on the icon. This is so strange.

+4
source share
5 answers

Since the tooltip is triggered by the div title attribute, how about just checking this attribute value? This would be based on the assumption that all browsers display title as a tooltip when they freeze (which, as far as I know, all desktop browsers).

Short-term switching, it seems that something in this direction should work:

 WebElement element = driver.FindElement( By.XPath("//div[@class='icon_png information icon_baggageyes']")); string titleText = element.getAttribute("title"); 

After that, you can check the titleText as expected.

+2
source

Use a Java robot to interact with the user interface; The robot is used here to control mouse actions.

 WebElement targetElement = driver.findElement(By.id("value")); Point coordinates = targetElement.getLocation(); Robot robot = new Robot(); robot.mouseMove(coordinates.getX(), coordinates.getY() + 65); //Number 65 should vary Thread.sleep(3000); String tooltip = driver.findElement(By.id("value"")).getAttribute("title"); System.out.println(tooltip); 
+1
source

I have the same problem. If what you are saying is the correct OP that you are not hanging too fast on an element, I am confused why the method is called "move to element" and not "go to element and then immediately from element"

Also click and hold for me the same behavior as for navigating to an element. I think this is probably a mistake with selenium.

This is disappointing. I think I'm just using click, but clicking on an element sets up a different set of actions that I don’t want to set, while freezing should help me just show a tooltip.

I am using chromedriver.

0
source

I use a robot to hover over the icon. I had to calibrate the robot to work with selenium to make it easier to click on the elements.

See the answer here for calibration information for the selenium robot.

0
source

I was probably a bit late, but what I found to work was:

 IWebElement elementToHover = graphRegion.FindElement(By.CssSelector(CSSSelector)); Actions hover = new Actions(driver); hover.MoveToElement(elementToHover); Thread.Sleep(2000); hover.Perform(); 

I primarily test in Chrome in many OS combinations, with a browser extension, and without sleep, I ran into problems over time when a tooltip popped up, primarily on Linux (low reliability on Mac).

I'm a little unsure why Thread.Sleep (2000) is running as part of the "Action". I tried several different custom wait methods, but I achieved only the reliability that I need, through explicit Sleep of 2 seconds or more.

I am sure there are better options, but it works for me!

0
source

Source: https://habr.com/ru/post/1469142/


All Articles