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

How do I launch the browser using webdriver?

482


Could you explain the line of code webdriver driver = new firefoxdriver()?

471


What are the performance testing tools?

523


What is the selenese command to show the value of a variable in the log file?

482


What types of tests are compatible with Selenium?

481






List out the test types that are supported by selenium?

471


Tell me how you can capture server side log selenium server?

474


How to perform right click action (context click) in selenium webdriver?

462


How can you find if an element is displayed on the screen?

472


Mention what is selenium 3.0?

471


Describe the purpose of xpath.

491


Tell us how will you use selenium to upload a file?

457


What is intellij?

482


Can you write the code to double click an element in selenium?

492


What are the advantages and disadvantages of selenium over other testing tools like qtp and testcomplete?

957