3

I have to write a selenium automation test script , where test has to create a template that fills up Generalize data in form and enter specific details manually(trying to wait here till manual entry is finished) and then click on SAVE button. However if some of mandatory field is left the system shows JavaScript's validation alert. I am handling this alert by using Alert alert = driver.switchTo().alert(); alert.accept(); After this I want to get back to main page and wait for several minutes to write the description for missing fields and now click on SAVE button.

How can I achive this in Selenium web driver?

Mitaleek
  • 33
  • 1
  • 2
  • 5
  • 2
    Just because you have switched to the alert does not mean the control permanently stays with the alert. Once you perform clicking on ok the control comes back to the main page. – Vinay Sep 27 '13 at 09:21

4 Answers4

1

when you deal with alerts firstly you have to check whether alert is present. I would use this approach:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

for more in click here, Also do not forget about debug step by step to get to know on what step alert appears/not appears. Hope this helps you.

aimbire
  • 3,697
  • 1
  • 15
  • 28
Chetan
  • 2,360
  • 3
  • 18
  • 33
1

You can go to the alert window from parent window using switchTo() method:

<html>
Alert alrt = driver.switchTo().alert();   // Switching the control to Alert window

alrt.accept(); // to accept Yes to delete the task

//alrt.dismiss(); 

// to dismiss the action - once you click on YES, of course you don't have option to dismiss
</html>

Now, answering to your question -

once you select any option from the Alert window (say - YES/NO), the control automatically comes back to the parent or main window. And you know what, the same follows in Hidden divs too.The switching back to parent window comes into picture when it is a question of HTML Pop ups.

Hope this helps.

jsborn17
  • 455
  • 1
  • 7
  • 12
  • After alert.accept(); Control do come back to Main Window. However next statement in the script won't execute. Also driver.manage().timeouts().implicitlywait(20, timeunit.MINUTES) seems not working.( Trying to wait for 20 mins. for the user to enter specific data manually and after 20 mins. Automated script will click on SAVE button) – Mitaleek Sep 27 '13 at 14:19
  • I have the same issue as @Mitaleek anyone find a solution to this? – d0rf47 Jan 29 '21 at 19:36
1

After Accepting Alert box. we want to Switch to main window by geting the window handle details.

//Stores the handle details

    String windowHandle=driver.getWindowHandles();

//Alert portion

    Alert alert = driver.switchTo().alert();

    alert.accept();

//After switching back to main window..

    driver.navigate.windows(windowHandle);

//It takes to the main window....
Peanut
  • 3,753
  • 3
  • 31
  • 45
0

You can give a try on the following code: driver.navigate.back();

Shyamala
  • 69
  • 4
  • 6
  • 13