Tuesday, August 28, 2018

How To Capture Together With Verify Tool Tip Text Inwards Selenium Webdriver Example

| Tuesday, August 28, 2018
Tool tips are real mutual elements of spider web page together with visible on mouse hover of element. It tin lav hold upward on text box, link or anywhere else on the page. Sometimes nosotros postulate to capture tool tip text In Selenium WebDriver to verify If text Is right or not. Let's larn how to read tool tip text In selenium WebDriver exam together with thus verify It.

Look In to bellow given Images. There Is 1 link together with 1 text box. Both accept tool tip which Is displayed on chemical cistron mouse hover.


Now If you lot come across In higher upward Images, tool tip text Is stored In "title" attribute of element. Here nosotros tin lav use getAttribute method to read championship attribute text. Earlier nosotros learnt getAttribute method to cash inward one's chips text of disabled attribute In THIS POST together with to cash inward one's chips text of text box's value attribute In THIS POST.

So nosotros tin lav locate chemical cistron together with thus role getAttribute method to capture championship attribute text together with shop It In variable. Then nosotros tin lav compare expected together with actual text using testng Assert.assertEquals(actual, expected) assertion (VIEW EXAMPLE).

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

Bellow given representative volition exhibit you lot how to capture tool tip text of link together with text box chemical cistron to verify tool tip text are equally 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;  world shape GetTooltip {   WebDriver driver;   @BeforeTest  world void setup() throws Exception {   driver = novel FirefoxDriver();   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);   driver.get("aa/search?q=testng-assertion-assertnotnull-with");  }   @Test  world 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 lav verify tool tip text In selenium webdriver. You tin lav role getAttribute method to read value of whatsoever other attribute too.

Related Posts