0

I need to verify if hover over is working for a webelement using selenium. I know there are lots of answers asking to use either action class or getTitle().For ex: https://www.guru99.com/verify-tooltip-selenium-webdriver.html In both these solutions, it is about getting the text and asserting it. But my question is how can it ensure that hover over is working (I mean, when the user does a hover over the tooltip text should be displayed). For ex: in the below code, Actions class is used to clickAndHold and moveToElement. And then getText() is done to get the hover over text. Isn't the end result the same as using WebElement.getText() without using Actions class?

import org.openqa.selenium.interactions.Action;     
import org.openqa.selenium.interactions.Actions;        
import org.openqa.selenium.By;      
import org.openqa.selenium.WebDriver;       
import org.openqa.selenium.chrome.ChromeDriver;     
import org.openqa.selenium.*;       

public class JqueryToolTip {                
    public static void main(String[] args) {                                    
     
        String baseUrl = "http://demo.guru99.com/test/tooltip.html";                    
        System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");   
        
        WebDriver driver = new ChromeDriver();                  
        String expectedTooltip = "What's new in 3.2";                   
        driver.get(baseUrl);                    
                
        WebElement download = driver.findElement(By.xpath(".//*[@id='download_now']"));                         
        Actions builder = new Actions (driver);                         

        builder.clickAndHold().moveToElement(download);                 
        builder.moveToElement(download).build().perform();  
        
        WebElement toolTipElement = driver.findElement(By.xpath(".//*[@class='box']/div/a"));                           
        String actualTooltip = toolTipElement.getText();            
        
        System.out.println("Actual Title of Tool Tip  "+actualTooltip);                         
        if(actualTooltip.equals(expectedTooltip)) {                         
            System.out.println("Test Case Passed");                 
        }       
        driver.close();         
   }        
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sindhu
  • 1
  • 2
  • When you hover on some element if it displays tool-text then capture that and verify text else if it is a button or link capture the text/title and verify. – Nandan A Nov 26 '21 at 08:34

1 Answers1

0

getText()

getText() gets the visible (i.e. not hidden by CSS) text of this element, including sub-elements.

Without Mouse Hover Download now button's following-sibling <div> element will be having style attribute set as display: none;

display_none

In those cases, Selenium won't be having visibility on the element

<div class="tooltip"... display: none;>

and you may face NoSuchElementException.


Where as, if you Mouse Hover the Download now button's following-sibling <div> element will be having style attribute set as display: block;

display_block

Then Selenium would be having visibility on the element

<div class="tooltip"... display: block;>

and you can extract the required texts.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for the clarification. In my case, it is a link. When I hover over it, the message "ticket available" is displayed. But in the html code, before and after hovering, the text is always present. Here is a sample html code:
    "ticket available"
    I have used getText() but it did not retrieve anything. So I have used innerText (as below).
    – Sindhu Nov 26 '21 at 10:21
  • WebElement ticketMsg = (//span/div[contains(@id,'ccc')]/parent::span/span[@class='tooltiptext'])[1]; String s= ticketMsg.getAttribute("innerText"); System.out.println(s); Assert.assertEquals(s, "ticket available"); So the question is, will this verification (that I am doing with this code) ensure that tootltip text will be displayed for the user on mouseover. Mouseover Text can still fail to show due to styling or whatever code effects which make the hover over happen, right? – Sindhu Nov 26 '21 at 10:22
  • sorry for the bad format. I don't know how to make it better. – Sindhu Nov 26 '21 at 10:25
  • @Sindhu Answer to first comment: Possibly the `` does the trick. Needs a detailed review of the DOM. – undetected Selenium Nov 26 '21 at 14:16
  • @Sindhu Answer to second comment: As per the HTML in your first comment, the _xpath_ didn't work for me to locate the element with text as _`ticket available`_ and the possible reason behind `getText()` did not retrieve anything. Feel free to raise a new question. But I'm sure you understood the explanation within my answer. – undetected Selenium Nov 26 '21 at 14:23
  • Thanks. I have clarified with the developer and he said when the class 'tooltip' is mentioned, the text will be displayed on mouseover. So I am convinced to go with an xpath with class tootltip and then innerText or getTitle(). But now, what if the class -tooltip happened to have some code issues in a release. Then hover over will not show the text. But my automation script will still pass because I am verifying the text and the text will be always there in the html code. So I need some other way to verify the hover over to overcome this. Thanks in advance. – Sindhu Nov 27 '21 at 10:50