Let's see the demonstration of accessing and verifying the tool tips in the simple scenario
- Scenario 1: Tooltip is implemented using the "title" attribute
We will try to verify the tooltip of the "github" icon at the top right of the page.
In order to do it, we will first find the element and get its 'title' attribute and verify with the expected tool tip text.
Since, we are assuming the tool tip is in the "title" attribute, we are not even automating the mouse hover effect but simply retrieving the attribute's value using the "getAttribute()" method.
In order to do it, we will first find the element and get its 'title' attribute and verify with the expected tool tip text.
Here is the code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class ToolTip {
public static void main(String[] args) {
String baseUrl = "http://demo.guru99.com/test/social-icon.html";
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTooltip = "Github";
// Find the Github icon at the top right of the header
WebElement github = driver.findElement(By.xpath(".//*[@class='soc-ico show-round']/a[4]"));
//get the value of the "title" attribute of the github icon
String actualTooltip = github.getAttribute("title");
//Assert the tooltip's value is as expected
System.out.println("Actual Title of Tool Tip"+actualTooltip);
if(actualTooltip.equals(expectedTooltip)) {
System.out.println("Test Case Passed");
}
driver.close();
}
}
Explanation of code
- Find the WebElement representing the "github" icon.
- Get its "title" attribute using the getAttribute() method.
- Assert the value against the expected tooltip value.
.