How do you handle Alerts and Pop Ups?

Answer Posted / sehgalkhyati@gmail.com

@Test
public void plainAlertTest() {
WebElement alertButton;
alertButton = driver.findElement(By.cssSelector("#alertexamples"));
alertButton.click();
assertThat(alertMessage, driver.switchTo().alert().getText());
// accept alert dialog to close
driver.switchTo().alert().accept();
}

@Test
public void confirmAlertTestOK() {
WebElement confirmAlertButton;
confirmAlertButton = driver.findElement(By
.cssSelector("#confirmexample"));
confirmAlertButton.click();
assertThat((driver.switchTo().alert().getText()),
is(confirmAlertMessage));
// accept alert dialog to close via 'OK'
driver.switchTo().alert().accept();
// verify page text is 'true'
String returnText = driver
.findElement(By.cssSelector("#confirmreturn")).getText();
assertThat(returnText, is("true"));
}

@Test
public void confirmAlertTestCancel() {
WebElement confirmAlertButton;
confirmAlertButton = driver.findElement(By
.cssSelector("#confirmexample"));
confirmAlertButton.click();
assertThat((driver.switchTo().alert().getText()),
is(confirmAlertMessage));
// accept alert dialog to close via 'OK'
driver.switchTo().alert().dismiss();
// verify page text is 'false'
String returnText = driver
.findElement(By.cssSelector("#confirmreturn")).getText();
assertThat(returnText, is("false"));
}

@Test
public void promptAlertTestOK() {
WebElement confirmAlertButton;
confirmAlertButton = driver.findElement(By
.cssSelector("#promptexample"));
confirmAlertButton.click();
// verify prompt alert text
assertThat((driver.switchTo().alert().getText()),
is(promptAlertMessage));
// change message
driver.switchTo().alert().sendKeys(promptText);
/*
* accept alert dialog to close via 'OK' how sendKeys help in closing OK
* button, why we simply dont click on click or press enter from
* keyboard.
*/
driver.switchTo().alert().accept();
// verify return text is (promptText)
String returnText = driver.findElement(By.cssSelector("#promptreturn"))
.getText();
assertThat(returnText, is(promptText));
}

@Test
public void promptAlertTestCancel() {
WebElement confirmAlertButton;
confirmAlertButton = driver.findElement(By
.cssSelector("#promptexample"));
confirmAlertButton.click();
// verify prompt alert text
assertThat((driver.switchTo().alert().getText()),
is(promptAlertMessage));
// change message
driver.switchTo().alert().sendKeys(promptText);
// REJECT alert dialog to close via 'Cancel'
driver.switchTo().alert().dismiss();
// verify return text is default "pret"
String returnText = driver.findElement(By.cssSelector("#promptreturn"))
.getText();
assertThat(returnText, is("pret"));
}

Is This Answer Correct ?    0 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is selenium-rc (remote control) ?

542


How you can use recovery scenario with selenium?

514


What is the use of context menu in selenium ide?

507


What is the use of @listener annotation in testng?

495


Write a code to wait for a particular element to be visible on a page. Write a code to wait for an alert to appear.

742






What are the advantages of selenium?

601


What are driver.close and driver.quit in WebDriver? Which is more preferable?

491


List some of the test types that are supported by selenium.

468


Mention what is the difference between selenium and sikuli?

661


List the different types of drivers available in web drivers.

508


What are the limitations of using selenium as a test tool?

474


What are the annotations available in testng?

496


How to retrieve css properties of an element?

483


What kind of testing does selenium support?

484


How to handle a dropdown in selenium webdriver? How to select a value from dropdown?

480