How to verify Tool tips using Selenium WebDriver?

Posted by

Tool tips are used to make a user interface easy to use. These tool tips must be checked for correctness and accuracy. So, how do we verify tool tips using Selenium webdriver?

You may also like to read complete Selenium tutorials for beginners on this blog.

What is a tool tip?

In Google search page, if the mouse moves to the search text box, small “hover box” with text gets displayed. 

This is called a tooltip of web element

verify Tooltips using Selenium WebDriver

Tooltips are hints to the user what type of object it is which gets displayed when the mouse is hovered over some link, text or sometimes images.

As mentioned above, the purpose is to provide a hint to the user what type of object it is.

It is required to verify text displayed is as per the expected text.

For this reason, it is required to retrieve the text inside the tooltip and verify the text. 

This can be done in different ways depending upon how tooltip is getting inserted in the HTML. 

Below listed are 2 cases-

  • Case One: When the tooltip is available in the ‘title’ attribute. Here, we can retrieve tooltip by using getAttribute() method.
  • Case Two: When the tooltip is available in ‘div’ tag. Here, we can retrieve tooltip using Actions class methods.

Let’s start by discussing Case 1…

Case 1: Capturing ToolTip using getAttribute()

ToolTip gets displayed when hovered over an HTML object. 

This can also be seen from the Developer tool of the browser where tooltip text is seen as a value set in ‘title’ attribute. 

Sample Code:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.testng.Assert;

public class ToolTip {

static WebDriver driver;

public static void main(String args[])

{

System.setProperty(“webdriver.chrome.driver”,”G:\\Maven_Selenium\\SeleniumGroup\\Jar\\chromedriver.exe”);

                    driver = new ChromeDriver();

                    driver.get(“http://www.google.com”);

                    String Exepctedresult= “Search”;

                   WebElement search = driver.findElement(By.xpath(“//input[@class=’gLFyf gsfi’]”));

                   String actualResult =search.getAttribute(“title”);

                   Assert.assertEquals(actualResult, Exepctedresult, “Testcase pass”);

                    System.out.println(“Testcase pass”);

          }

}

Case 2: Capture tool tip in Selenium using Actions Class

Hover over the object and try to read the description of the tooltip. And this can only be done with the help of Actions Class in Selenium
Let’s see how to use Actions class methods to get it done.

Step 1:

Instantiate an Actions class to make use of its object. 

Actions action = new Actions(driver);

Step 2:

Locate the tooltip web-element. 

WebElement element = driver.findElement(Any By strategy & locator); 

Step 3:

Invoke moveToElement(), this method of Actions class moves the mouse to the middle of the element.

action.moveToElement(element).perform(); 

perform() method  is necessary to use, after using moveToElement() method to perform the action.

Example program

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

public class Tooltips2

{                                                                                                                                                                            

//Variable declared

  static WebDriver driver;

public static void main(String[] args)

{                                                                                                                     

// TODO Auto-generated method stub System.setProperty(“webdriver.chrome.driver”,”G:\\Maven_Selenium\\SeleniumGroup\\Jar\\chromedriver.exe”);                                                                              

driver = new ChromeDriver();                                                     

//lauching website

driver.get(“http://demoqa.com/tooltip-and-double-click/”);   

driver.manage().window().maximize();                                                                      

driver.manage().window().maximize();                                                                      

//Creating object for Action class

Actions actions = new Actions(driver);

WebElement element = driver.findElement(By.id(“tooltipDemo”)); actions.moveToElement(element).perform();

WebElement toolTip = driver.findElement(By.cssSelector(“.tooltiptext”)); 

// To get the tool tip text and assert                                                             

String toolTipText = toolTip.getText()                    

System.out.println(“toolTipText–>”+toolTipText);                           

//Verification if tooltip text is matching expected value  if(toolTipText.equalsIgnoreCase(“We ask for your age only for statistical purposes.”))     

 {                                                                                                                                              

System.out.println(“Pass* : Tooltip matching expected value”);          

 }

Else                 

{                                                                                                                                              

System.out.println(“Fail : Tooltip NOT matching expectedvalue”);   }

  // Close the main window

   driver.close();

           }

          }

Conclusion

In this post, we discussed about how to verify tool tips using Selenium. We have verified the tool tips with the desired text.

Selenium Training with Job readiness package

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

We have a Business analyst training course with domain training in-built into it. This training program offers you the opportunity to get certified with ECBA certification as well as have banking domain understanding.

Selenium Certification Training