Showing posts with label How to verify if alert is displayed in Selenium Webdriver in Java?. Show all posts
Showing posts with label How to verify if alert is displayed in Selenium Webdriver in Java?. Show all posts

Monday, 17 November 2014

How to verify if alert/warning/message is displayed in Selenium Webdriver in Java?

How to verify if alert/warning/message is displayed in Selenium Webdriver in Java?

package com.snapdeal;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestIsDisplayed {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
try {
driver.get(“https://accounts.google.com/ServiceLoginAuth”);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id(“signIn”)).click();
WebElement e = driver.findElement(By
.xpath(“//*[@id='errormsg_0_Email']“));
boolean actual = e.isDisplayed();
if (actual) {
System.out.println(“Message is Displayed”);
// Get the warning message text
String testdata = driver.findElement(
By.xpath(“//*[@id='errormsg_0_Email']“)).getText();
System.out.println(“Test message is : ” + testdata);
} else {
System.out.println(“Message is not Displayed”);
}
} catch (Exception ex) {
System.out.println(“Exception” + ex.getMessage());
} finally {
driver.close();
}
}
}

/*
Output is
Message is Displayed
Test message is : Enter your email address.
*/