2

我正在测试我的表单,当我没有输入需要的数据时,我会在我的网络应用程序中收到 javascript 警报,告诉用户输入丢失的数据。我无法用 selenium 处理这个问题,因为当我部分填写表格并尝试提交时,我得到了异常

org.openqa.selenium.UnhandledAlertException: Modal dialog present

如果我捕获异常,则不会显示 webdriver 中的警报。这是解决此问题的任何解决方案吗?我希望能够提交表单并收到警报。我正在使用带有 java 的 Linux Mint、Firefox 18 和 selenium 2.28.0 最好的问候更新我的代码中有以下内容

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)

但是,如果我按预期单击手动提交测试工作。提前致谢

4

3 回答 3

1

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

于 2013-04-14T19:49:40.207 回答
0

这是因为当抛出 UnhandledAlertException 时,驱动程序本身会接受警报。如果您填写了部分表格,如何提交?

如果可能的话,只需捕获该异常,然后在 catch 块中写入单击提交按钮的行。

于 2013-03-05T09:14:11.263 回答
-1

使用 Robot 类(按回车键)关闭模态对话框

 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();
            }
于 2013-02-27T08:03:22.870 回答