How to verify Tooltip text using Actions class in Selenium?

In previous tutorials, we discussed mouse hover scenario, right click actions, double click actions, drag and drop actions and keyboard action handling through Selenium WebDriver. All these activities are performed by using the Actions class of Selenium WebDriver. In today’s agenda that is about the verification of tooltip text, we are going to use Actions class too. Hence, you can figure out that Actions class has the wider use in Selenium WebDriver. Of course, it has because it handles all the mouse as well as keyboard operations actively.

We will further continue the discussion about verification of the tooltip text, but, before that, I would like to share the basic introduction about tooltip so that a new learner could get the knowledge as well.

What is Tooltip Text?

Tooltip text is the hidden text behind the elements like image, buttons, link, text area, or any other web elements (objects) and the text appears when we mouse hover the pointer over these objects. Such text gives information about the objects.

For example, you can visit inviul blog section, then you find a section on the right side which is Connect Social. There are social media buttons when you mouse hover over each of the buttons, you get the description of each button as Facebook, Google +, Git, etc.

Following image highlights, the tooltip text appears after mouse hovering on the Facebook button.

Inviul Blog Tooltip

 

Hope the basic concept of the tooltip is clear now.

How is tooltip implemented on a web page?

Tooltip is implemented as the title attribute of any web element. Hence, you need to get the attribute’s value using Selenium WebDriver to verify the tooltip text. UI/UX developers use CSS tooltip, jQuery or JavaScript plugins to implement the tooltip to the web page.

It is good to implement tooltip as it is useful for accessibility. Hence the scope of verifying tooltip text goes to accessibility testing of any web page. Also, if any object is not properly visible so we can get the information about that particular object by reading its tooltip text.

Click here to know more about tooltip.

How to verify tooltip in Selenium?

We saw social media buttons at the inviul blog so we will take it as a demo scenario wherein we need to navigate to the inviul blog and we need to verify the tooltip text of the social media buttons. We will verify the tooltip text of the Facebook button.

The following image is the screenshot of the inspector. We see there is a text which is set for the title attribute of the Facebook button so we will first perform mouse hover action then we will retrieve the text by using getAttribute() method.

verify tooltip text

Let’s make our hands dirty with the code.

package Test;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
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 TooltipTextDemo {

	public static void main(String[] args) throws InterruptedException {
		 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");	
		  
		  WebDriver driver  = new ChromeDriver();
		  
		  driver.get("https://www.inviul.com/blog/");
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.manage().window().maximize();
	  	  
	  	  Thread.sleep(2000);
	  	  
	  	  WebElement ele = driver.findElement(By.xpath("//*[@class='social-facebook']//a"));
	  	  
	  	  Actions act = new Actions(driver);
	  	  act.moveToElement(ele).build().perform();
	  	  
	  	Thread.sleep(2000);
	  	
	  	 String tootltipText = ele.getAttribute("title");
	  	 
	  	 String expectedText = "Facebook";
	  	 
	  	 System.out.println("Tooltip Text is- "+tootltipText);
	  	 
	  	 Assert.assertEquals(expectedText, tootltipText);
	  	 
	  	 driver.close();
	  	 
	  	 driver.quit();
	}

}

This was all about verifying tooltip text using Actions class in Selenium WebDriver. You can post your queries in the comment form below. Also, you can join our Facebook group for quick updates on Selenium WebDriver and Automation testing.

Join Inviul fb group

Leave a Reply