Monday, February 18, 2013

Selenium : JS Alert handling using "Try"and "Catch"

The following code does the same job as the previous except that it uses TRY and CATCH to resolve the issue.


import
org.openqa.selenium.Alert;


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 {
 
public static void main(String[] args) throws InterruptedException
{
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();
try
{
System.out.println( driver.switchTo().alert().getText()+" --- Alert is present");
driver.switchTo().alert().accept();
}
catch (org.openqa.selenium.UnhandledAlertException e)
{
System.
out.println("not present");
}
Thread.sleep(1000);
driver.quit();
}
}

No comments:

Post a Comment