Your ultimate guide

Handling Tooltip text

Handling ToolTip Text:

When we move on mouse point on a particular element, it will display the title of the element which is called Tooltip Text. 


Based on the different Cases we follow different ways to get Tooltip text.
Case 1:  Generally Tooltip text will be stored in the "title" attribute.
Example Program: 

public class ToolTip {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        String MicXpath= "//*[@aria-label='Search by voice']";
        WebElement  mic = driver.findElement(By.xpath(MicXpath));

        //To the Mouse into the mic element
        Actions action = new Actions(driver);
        action.moveToElement(mic).perform();
        //Getting Tooltip text by using the 'title' attribute
        String actualText = mic.getAttribute("title");
        String expectedText = "Search by voice";

        if(actualText.equals(expectedText))
        {
            System.out.println("Actual and Excepted Titles are Match);
        }
        else
        {
            System.out.println("Actual and Excepted Titles are not Match");
        }
        driver.close();
    }
}

Case 2: If the 'title' attribute is not there then we will get the Tooltip text by using the getText() method.
Example Program:

public class ToolTip {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        String MicXpath= "//*[@aria-label='Search by voice']";
        WebElement  mic = driver.findElement(By.xpath(MicXpath));

        //To the Mouse into the mic element
        Actions action = new Actions(driver);
        action.moveToElement(mic).perform();
        //Getting Tooltip text by using getText() method
        String actualText = mic.getText();
        String expectedText = "Search by voice";

        if(actualText.equals(expectedText))
        {
            System.out.println("Actual and Excepted Titles are Match);
        }
        else
        {
            System.out.println("Actual and Excepted Titles are not Match");
        }
        driver.close();
    }
}

No comments:

Post a Comment