Y
Y
Yulia Pavlova2015-06-09 12:44:56
Java
Yulia Pavlova, 2015-06-09 12:44:56

How to teach autotest to work with alert windows?

I started writing autotests in java + Selenium WebDriver and a problem arose, I just can’t find a way to explain to the test what to do when an alert window appears. Maybe someone has already experienced this? It is necessary that the test, when displaying this window, click OK and confirm the action.
4eb7010b910c4f7ca46356d5207e7fc9.jpg
The picture shows the window that appeared when you clicked on the "Trash" icon, it is in it that you need to confirm the deletion. The code for the delete icon is also on the screenshot.
If you need some specific functions from third-party libraries, then I will install it, the main thing is to solve the problem.
Thanks in advance to all who respond!
PS I'm a bad programmer (from the word "none"), I'm learning to automate and program at the same time, so I beg you to help me with a detailed and explained answer.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Blokhin, 2015-06-09
@TITnet

You need to go here: docs.seleniumhq.org/docs/03_webdriver.jsp#popup-di...
First you have to switch to this Alert, and then perform the necessary actions with it.

M
mipan, 2015-06-09
@mipan

internetka.in.ua/selenium-driver-aler

E
Evgeny Tkachenko, 2015-06-09
@eugene20tkachenko

Conceptually, you need two switches with OK and Cancel actions:
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();

K
Kenshir007, 2015-08-21
@Kenshir007

I suffered for a very long time with Allerts in principle. The problem is that if the project uses Iplicidic waits, the driver does not always understand whether an alert has appeared. I solved this problem with this method:
public void acceptPopUp() {
Integer tries = 0;
Integer maxTries = 30;
while (tries < maxTries) {
tries++;
try {
disableImplicityWait();
waitInSeconds(2);
Alert alert = driver.switchTo().alert();
if (alert != null && alert.getText().length() > 1) {
alert.accept();
enableImplicityWait();
return;
}
waitInSeconds(1);
} catch (Exception e) {
e.getSuppressed();
}
}
}
private void disableImplicityWait() {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
}
private void enableImplicityWait() {
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question