Handling of Alert, Confirmation Message or error message while execution is very important for smooth execution.
Alerts:
public class alertWindow {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebElement button , textarea = null;
String parentWindowHandle = null;
Wait<WebDriver> wait = null;
Alert alert =null;
driver.get(System.getProperty("user.dir")+ "\\Sample\\Prompt.html");
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 10);
parentWindowHandle = driver.getWindowHandle();
button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button")));
button.click();
if (alert != null)
System.out.println("Alert Not found");
else
System.out.println("Alert found");
alert = driver.switchTo().alert();
System.out.println("Alert Text " + alert.getText());
alert.sendKeys("Mohit Jain");
alert.accept();
textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());
button.click();
alert.dismiss();
textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());
driver.quit();
}
}
Confirmation :
public class confirmationMesg {
/**
* @param args
*/
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebElement button ,firstname, textarea = null;
String parentWindowHandle = null;
Set<String> allWindows;
Wait<WebDriver> wait = null;
Alert alert =null;
String windowtitle =null;
driver.navigate().to("http://www.ranorex.com/web-testing-examples/vip/");
driver.manage().window().maximize();
firstname =driver.findElement(By.id("FirstName"));
//firstname.sendKeys("Mohit");
button = driver.findElement(By.id("Save"));
parentWindowHandle = driver.getWindowHandle();
button.click();
allWindows = driver.getWindowHandles();
/*
for (String allWindow : allWindows ) {
driver.switchTo().window(allWindow);
if (driver.getTitle().equalsIgnoreCase("VIP Database")){
driver.manage().window().maximize();
driver.findElement(By.cssSelector("button")).click();
}
}
driver.switchTo().window(parentWindowHandle);
firstname.sendKeys("Mohit");*/
for (String allWindow : allWindows ) {
driver.switchTo().window(allWindow);
driver.manage().window().maximize();
if (driver.getTitle().equalsIgnoreCase("VIP Database")){
//driver.close();
driver.quit();
}
}
}
Alerts:
public class alertWindow {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebElement button , textarea = null;
String parentWindowHandle = null;
Wait<WebDriver> wait = null;
Alert alert =null;
driver.get(System.getProperty("user.dir")+ "\\Sample\\Prompt.html");
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 10);
parentWindowHandle = driver.getWindowHandle();
button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button")));
button.click();
if (alert != null)
System.out.println("Alert Not found");
else
System.out.println("Alert found");
alert = driver.switchTo().alert();
System.out.println("Alert Text " + alert.getText());
alert.sendKeys("Mohit Jain");
alert.accept();
textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());
button.click();
alert.dismiss();
textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());
driver.quit();
}
}
Confirmation :
public class confirmationMesg {
/**
* @param args
*/
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebElement button ,firstname, textarea = null;
String parentWindowHandle = null;
Set<String> allWindows;
Wait<WebDriver> wait = null;
Alert alert =null;
String windowtitle =null;
driver.navigate().to("http://www.ranorex.com/web-testing-examples/vip/");
driver.manage().window().maximize();
firstname =driver.findElement(By.id("FirstName"));
//firstname.sendKeys("Mohit");
button = driver.findElement(By.id("Save"));
parentWindowHandle = driver.getWindowHandle();
button.click();
allWindows = driver.getWindowHandles();
/*
for (String allWindow : allWindows ) {
driver.switchTo().window(allWindow);
if (driver.getTitle().equalsIgnoreCase("VIP Database")){
driver.manage().window().maximize();
driver.findElement(By.cssSelector("button")).click();
}
}
driver.switchTo().window(parentWindowHandle);
firstname.sendKeys("Mohit");*/
for (String allWindow : allWindows ) {
driver.switchTo().window(allWindow);
driver.manage().window().maximize();
if (driver.getTitle().equalsIgnoreCase("VIP Database")){
//driver.close();
driver.quit();
}
}
}
No comments:
Post a Comment