SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Capture And Verify Tool tip Text In Selenium WebDriver Example

Tool tips are very common elements of web page and visible on mouse hover of element. It can be on text box, link or anywhere else on the page. Sometimes we need to capture tool tip text In Selenium WebDriver to verify If text Is correct or not. Let's learn how to read tool tip text In selenium WebDriver test and then verify It.

Look In to bellow given Images. There Is one link and one text box. Both have tool tip which Is displayed on element mouse hover.


Now If you see In above Images, tool tip text Is stored In "title" attribute of element. Here we can use getAttribute method to read title attribute text. Earlier we learnt getAttribute method to get text of disabled attribute In THIS POST and to get text of text box's value attribute In THIS POST.

So we can locate element and then use getAttribute method to capture title attribute text and store It In variable. Then we can compare expected and actual text using testng Assert.assertEquals(actual, expected) assertion (VIEW EXAMPLE).

Syntax to read title attribute of link Is as bellow.
// Get tooltip text from link.
String tooltiptext1 = driver.findElement(By.xpath("//a[contains(.,'Hover over me')]")).getAttribute("title");

Bellow given example will show you how to capture tool tip text of link and text box element to verify tool tip text are as per expected or not.

package STTA.MavenProject1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GetTooltip {

 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2015/03/chart.html");
 }

 @Test
 public void clickBeforeLoad() {
  // Get tooltip text from link.
  String tooltiptext1 = driver.findElement(By.xpath("//a[contains(.,'Hover over me')]")).getAttribute("title");
  System.out.println("Tootltip text on link Is : " + tooltiptext1);
  //Verify tooltip text of link.
  Assert.assertEquals(tooltiptext1, "tooltip text!");
  
  // Get tooltip text from textbox.
  String tooltiptext2 = driver.findElement(By.xpath("//input[@id='tooltip-1']")).getAttribute("title");
  System.out.println("Tootltip text on textbox Is : " + tooltiptext2);
  //Verify tooltip text of text box.
  Assert.assertEquals(tooltiptext2, "Enter You name");
 }
}

This way we can verify tool tip text In selenium webdriver. You can use getAttribute method to read value of any other attribute too.

No comments:

Post a Comment