Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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....

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top