How do you handle Alerts and Pop Ups?

Answers were Sorted based on User's Feedback



How do you handle Alerts and Pop Ups?..

Answer / annasaheb

To Handle Alert

Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();//to accept alert
alert.dismiss();//to cancel alert

Is This Answer Correct ?    19 Yes 1 No

How do you handle Alerts and Pop Ups?..

Answer / chetan phani.j

Alert popup
Alert a =driver.switchto().alert()
String text=a.getText().
syso(text);

Is This Answer Correct ?    0 Yes 0 No

How do you handle Alerts and Pop Ups?..

Answer / 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

More Selenium Interview Questions

How to click on a hyper link using linktext?

0 Answers  


Why should Selenium be selected as a testing tool?

0 Answers  


What are the main advantages of selenium grid?

0 Answers  


What is same origin policy? How you can avoid same origin policy?

0 Answers  


How to download a file in selenium webdriver?

0 Answers  






How to delete browser cookies with selenium web driver?

0 Answers  


Can you run the tests registered with Selenium IDE in other browsers?

0 Answers  


What kinds of test types are supported by selenium?

0 Answers  


What are some of the advantages of selenium grid?

0 Answers  


How you can insert a start point in selenium ide?

0 Answers  


What are the testing kinds that can be supported by selenium?

0 Answers  


How to get an attribute value using selenium webdriver?

0 Answers  


Categories