简体   繁体   中英

selenium webdriver modal dialog java

I am testing my form and when I don't type needed data I get javascript alert in my web app that tells the user to enter missing data. I can't handle this with selenium because when I partially fill form and try to submit I get exception

org.openqa.selenium.UnhandledAlertException: Modal dialog present

If I catch exception the alert in webdriver is not shown. Is that any solution to solve this issue?I would like to be able to submit form and catch the alert. I am using Linux Mint,Firefox 18 and selenium 2.28.0 with java Best regards UPDATE I have following in my code

somePage.fillName(sth); //only 1 of 2 required field are filled
somgePage.submit(); //here js alert is shown right after clicking submit
somePage.getCurrentAlert();
//here are code parts
public Alert getCurrentAlert(){
    return driver.switchTo().alert();
}
public AdminHome submit(){
        saveUrl();
        WebElement submit = driver.findElement(By.id("add_quiz_submit_button"));
        try{
            submit.click();
            if(urlChanged()){
                return new AdminHome(driver);
            }
        }
        catch(Exception e){
            e.printStackTrace();// exception 1
            return null;
        }
        return null;
    }
//Exception 1
org.openqa.selenium.UnhandledAlertException: Modal dialog present
//The test fails because of:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)

However if I click manual on submit the test work as expected. Thanks in advance

you should handle the alert as soon as the action is done and there shouldn't be any other action before handling the alert.

for instance your code should be

 try{
        submit.click();
        if (alertPresent())
            getCurrentAlert();
        if(urlChanged()){
            return new AdminHome(driver);
        }
    }

This will check alert and then accept the alert. The interaction of webdriver is more similar to the action we interact with manually with browser. So when the click on submit is done we will be able to see alert and no actions can be done until accept or reject it.

Vishal

It is because driver accepts the alert itself when the UnhandledAlertException is thrown. How can you submit the form if you have filled it partially?

If it is even possible, just catch that exception, and in catch block write the line which clicks on the submit button.

Use Robot class (Press enter) to close modal dialog box

 try {
        (new Robot()).keyPress(java.awt.event.KeyEvent.VK_ENTER);

         (new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ENTER);
         } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM