What is ToolTip Text Message?
ToolTip message is the hint to GUI user. Below Screenshot shows ToolTip message.
There are two ways to read and verify tooltip message.
1. Using Actions class
Whenever you focus on element and a tool-tip appears it is displayed inside a “div” which is made visible on mouse focus.
- Move to element using actions classActions action = new Actions(driver); action.moveToElement(‘element_to_be_focused’).build().perform();
- Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned.
- Now simply write code to wait for visibility of element having tooltip text.
new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(‘element_locator’));
- Get text from tooltip element using getText()
Below is sample code –
package learnAboutActionsClass; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class VerifyToolTipMessage { public static void main(String[] args) { 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(); 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(); } }
2. Using “title” Attribute
Whenever you will have tooltip then “title” attribute will be there
package learnAboutActionsClass; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class VerifyToolTipMessage1 { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in"); String ToolTipText = driver.findElement(By.xpath(".//*[@id='mbtnav']/li[1]/a")).getAttribute("title"); System.out.println("ToolTip message is: "+ToolTipText); driver.quit(); } }
Refer below links to learn more about Actions Class in Selenium WebDriver –
Actions class in Selenium WebDrivr
MouseHover action in selenium WebDriver
Right Click (Context click) in Selenium
Verify Tool Tip Message text using Selenium WebDriver
How to Perform series of actions using Actions class in selenium WebDriver?
How to use tab button in selenium WebDriver?
How to perform Drag and Drop operation using selenium WebDriver?
How to send text in capital letters into a text-box using Actions class in Selenium WebDriver?
How to refresh webpage using Action class in selenium WebDriver?
Upload file in Selenium Using ROBOT Class
Upload file in Selenium using sendKeys() method
Hope this helps !!!