In most web applications the handling of pop ups/alert/confirmations/prompts during the automation testing is a common issue.
In this post we will describe how we can use the SeleniumRC and the Webdriver in order to manage them.
Selenium RC
Selenium RC provides the 3 following methods in order to handle alert-confirmation and prompt pop-ups.
The first two are used for hanling of alert-confirmation pop-ups and the third for prompt pop-ups
The tricky part with these methods is that they must called BEFORE the action that fires the pop-up.
Consider for example the following scenario:
Select a object from the page, Click on Delete button. It triggers a alert saying "Object is going to be deleted, Click OK to Confirm or Cancel to Cancel the Operation".
So if selenium is a Selenium instance, in order to Delete the object, the code snippet in out test automation script should be as follows:
In order to Cancel the deletion of the object, the code snippet in out test automation script should be as follows:
Keep in mind that in both cases during the execution of the test automation script, the user WILL NOT SEE the actual alert pop-up but the action is done 'behind the scenes';
The code is similar in the case the action fires a prompt pop-up(the user is prompted to enter a text inside the pop-up)
Web Driver
Web Driver provides a more efficient manner to handle all these events through the Alert interface, using the following methods:
So if driver is a WebDriver instance, regarding the scenarios we mentioned earlier the code snippet in out test automation script should be as follows:
1st case
2nd case
3rd case
If you need to wait until the Alert present you can use the following code:
It is important to notice two things here:
Have fun...
In this post we will describe how we can use the SeleniumRC and the Webdriver in order to manage them.
Selenium RC
Selenium RC provides the 3 following methods in order to handle alert-confirmation and prompt pop-ups.
void chooseOkOnNextConfirmation(); void chooseCancelOnNextConfirmation(); void answerOnNextPrompt(answer);
The first two are used for hanling of alert-confirmation pop-ups and the third for prompt pop-ups
The tricky part with these methods is that they must called BEFORE the action that fires the pop-up.
Consider for example the following scenario:
Select a object from the page, Click on Delete button. It triggers a alert saying "Object is going to be deleted, Click OK to Confirm or Cancel to Cancel the Operation".
So if selenium is a Selenium instance, in order to Delete the object, the code snippet in out test automation script should be as follows:
selenium.chooseOkOnNextConfirmation(); selenium.click('locator of Delete button');
In order to Cancel the deletion of the object, the code snippet in out test automation script should be as follows:
selenium.chooseCancelOnNextConfirmation(); selenium.click('locator of Delete button');
Keep in mind that in both cases during the execution of the test automation script, the user WILL NOT SEE the actual alert pop-up but the action is done 'behind the scenes';
The code is similar in the case the action fires a prompt pop-up(the user is prompted to enter a text inside the pop-up)
selenium.answerOnNextPrompt('text'); selenium.click('locator of element that triggered the prompt');
Web Driver
Web Driver provides a more efficient manner to handle all these events through the Alert interface, using the following methods:
void dismiss(); void accept(); String getText(); void sendKeys(String keysToSend);
So if driver is a WebDriver instance, regarding the scenarios we mentioned earlier the code snippet in out test automation script should be as follows:
1st case
driver.findElement(By.id('id of Delete button')).click(); Alert myAlert = driver.switchTo().alert(); //Switch to alert pop-up myAlert.accept(); //accept the alert - equivalent of pressing OK
2nd case
driver.findElement(By.id('id of Delete button')).click(); Alert myAlert = driver.switchTo().alert(); myAlert.dismiss(); //cancel the alert - equivalent of pressing CANCEL
3rd case
driver.findElement(By.id('id of web element')).click(); Alert myAlert = driver.switchTo().alert(); alert.sendkeys('text'); //Write text inside the prompt myAlert.accept();
If you need to wait until the Alert present you can use the following code:
WebDriverWait driver = new WebDriverWait(driver, 10); Alert myAlert = driver.until(ExpectedConditions.alertIsPresent());
It is important to notice two things here:
- In the case of WebDriver the methods are called AFTER the action that fires the pop-up(which is the real case scenario), and
- During the execution of the test automation script, the user WILL BE ABLE TO SEE the actual alert pop-up (Web Driver simulates exactly the user case scenario).
Have fun...