Tool tips are real mutual elements of spider web page too visible on mouse hover of element. It tin john hold out on text box, link or anywhere else on the page. Sometimes nosotros take away to capture tool tip text In Selenium WebDriver to verify If text Is right or not. Let's acquire how to read tool tip text In selenium WebDriver examination too therefore verify It.
Look In to bellow given Images. There Is 1 link too 1 text box. Both bring tool tip which Is displayed on chemical division mouse hover.
Now If you lot run across In to a higher house Images, tool tip text Is stored In "title" attribute of element. Here nosotros tin john use getAttribute method to read championship attribute text. Earlier nosotros learnt getAttribute method to become text of disabled attribute In THIS POST too to become text of text box's value attribute In THIS POST.
So nosotros tin john locate chemical division too therefore purpose getAttribute method to capture championship attribute text too shop It In variable. Then nosotros tin john compare expected too actual text using testng Assert.assertEquals(actual, expected) assertion (VIEW EXAMPLE).
Syntax to read championship attribute of link Is every bit bellow.
// Get tooltip text from link. String tooltiptext1 = driver.findElement(By.xpath("//a[contains(.,'Hover over me')]")).getAttribute("title");
Bellow given illustration volition demo you lot how to capture tool tip text of link too text box chemical division to verify tool tip text are every bit 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; populace flat GetTooltip { WebDriver driver; @BeforeTest populace void setup() throws Exception { driver = novel FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get(" "); } @Test populace 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 means nosotros tin john verify tool tip text In selenium webdriver. You tin john purpose getAttribute method to read value of whatever other attribute too.