Ripon Al Wasim
Ripon Al Wasim

Reputation: 37816

How to verify tooltipText in Selenium WebDriver using Java

I have the following snippet of HTML code:

<div style="float:right; padding-right: 50px; margin-top: -10px;" id="divTooltips">
<img width="25px" height="25px" src="/mkteditor/css/images/tooltip.png" alt="">
</div>

During mouse over tooltip.png a tooltip text "help text at top place" is displayed. I want to verify the tooltipText in WebDriver. How to do it?

Upvotes: 3

Views: 23405

Answers (5)

Subitha
Subitha

Reputation: 51

You can try the following:

WebElement tooltipControl = driver.FindElement(By.id("divTooltips");
string titleText = tooltipControl.GetAttribute("textContent");

textContent attribute will provide the Text displayed in the tooltip.

Hope this will help you.

Upvotes: 0

Himanshu Tewari
Himanshu Tewari

Reputation: 323

you can use this

WebElement element = driver.FindElement(By.id("divTooltips");
 string titleText = element.getAttribute("title");

now the title is stored in a titleText string.In case tooltip is hidden and you want to see hidden text on it check this

Enjoy!

Upvotes: 0

Sameer Patil
Sameer Patil

Reputation: 461

My observation says, whenever you focus on element and a tool-tip appears it is displayed inside a "div" which is made visible on mouse focus.

Here is something i would suggest,

  1. Move to element using actions class

    Actions action = new Actions(driver); action.moveToElement('element_to_be_focused').build().perform();

  2. Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned.

  3. Now simply write code to wait for visibility of element having tooltip text.

    new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated('element_locator'));

  4. Get text from tooltip element using getText()

To give an example, Click here

On given website once we mouse hover to textbox asking for age, we can see tooltip saying 'We ask for your age only for statistical purposes.'

Following is code snippet to get tooltip text:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://jqueryui.com/tooltip/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
System.out.println("Is Tooltip displayed ? : " + isToolTipDisplayed);
            if (isToolTipDisplayed) {
                String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
System.out.println("Tooltip Text:- " + tooltipText);
}
driver.switchTo().defaultContent();
driver.quit();

Hope this will help you !

Upvotes: 3

Eyal Sooliman
Eyal Sooliman

Reputation: 2258

You can use the following to verify the tooltip text:

try{
    String tooltipText = "help text at top place";
    String tooltipTextToVerify = driver.findElement(By.id("divTooltips").getAttribute("title");
      if (tooltipText.equels(tooltipTextToVerify)){
         return true;
      else {return false;}
}

Upvotes: 0

Anand Somani
Anand Somani

Reputation: 801

Whenever you will have tooltip then "title" attribute will be there. (as per my observation ).

for example: Goto http://www.linkedin.com/ and view HTML code for LinkedIn image on top. LinkedIn

    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.linkedin.com/");
    WebElement onElement = driver.findElement(By.xpath("html/body/div[1]/div[1]/div/h2/a"));
    System.out.println("Tooltip : " + onElement.getAttribute("title"));

Upvotes: 3

Related Questions