The Following code is used handle Alert message in webpages from javascript.
Refer - http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
/* The following code goes to http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_validate" , where a JS Alert is popped whenever a user types a non-numeric value to message box */
import
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.events.EventFiringWebDriver;
import
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public
class handle_javascript_err {
{
WebDriver driver=
new FirefoxDriver();
EventFiringWebDriver d=
new EventFiringWebDriver(driver);
driver.get(
"http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_validate");
driver.switchTo().frame(
"viewIFRAME");
driver.findElement(By.id(
"demo")).sendKeys("abc");
driver.findElement(By.xpath(
"//button[text()='Click Me!']")).click();
WebDriverWait wait =
new WebDriverWait(driver, 300); /*timeout in seconds*/
{
System.
out.println("alert was not present");
}
{
System.
out.println("alert was present");
driver.switchTo().alert().accept();
}
Thread.sleep(5000);
driver.findElement(By.id(
"demo")).sendKeys("abcfff");
driver.quit();
}
}
Thanks for sharing this blog post,Nice written skill selenium Online Training Hyderabad
ReplyDeleteIt wasso informative..Thanks for sharing..keep upate best selenium training in chennai
ReplyDeletebest selenium training institute in chennai