HOW
TO CAPTURE TOOL TIP MESSAGE IN SELENIUM WEB DRIVER:
Tooltip is nothing but an Advice Message to the User
(or) Helper Text.
For example In Gmail Signup Page while you are
clicking or entering username or Password helper text appears, those text
messages are called Tooltip messages.
Steps
to read tooltip message:
1. Open browser, enter your application URL (www.gmail.com
in this example)
2. Click on the Web element (Password field in this
example)and mouse Hover to that tooltip text (Mouse Hover can be performed by
using Actions(C) or JavaScript Executor(I)).
3. call get Text() on that mouse hovered Web Element
,store the value in String format and print on the console.
4. Verify with Actual text with Expected result.
Sample Program for reading or capturing the tool tip
message :
package
com.rameshsoft.demos;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.Test;
public class
CaptureTooltipDemo {
WebDriver driver;
@Test
public void mm() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
driver=new
FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.manage().deleteAllCookies();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default");
Thread.sleep(2000);
//click on Password
driver.findElement(By.xpath(".//*[@id='Passwd']")).click();;
WebElement ele=driver.findElement(By.xpath("html/body/div[3]/div[1]/div[2]"));
//perfroming mouseHover
mouseHOver(ele);
String ActualText="at
least 8 characters. Don’t use a password from another site, or something too
obvious like your pet’s name. Why?";
String expectedText=ele.getText();
System.out.println("text
is: "+expectedText);
if(ActualText.equalsIgnoreCase(ele.getText()))
{
System.out.println("Both
Actual and Expected values are
Equal");
}
else
{
System.out.println("Both
Actual and Expected values are not Equal");
}
}
public void mouseHOver(WebElement ele)
{
String mouseOverScript = "if(document.createEvent){var
evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true,
false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject)
{ arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,ele);
}
}
OUTPUT:
text is: Use at least 8
characters. Don’t use a password from another site, or something too obvious
like your pet’s name. Why?
Both Actual and Expected values are Equal