在处理警报时需要帮助 - 等待警报存在&未处理的警报异常

时间:2016-09-22 11:13:00

标签: java selenium

我有以下警报。问题是从wait.until(ExpectedConditions.alertIsPresent())返回UnhandledAlertException行的错误,即使我可以看到屏幕上触发的警报。如果我删除了UnhandledAlertException,那么我会收到错误消息UnhandledAlertException。能否请您提供解决方案,以及一些解释,以便了解发生了什么? `

//Alert
    Alert getAlert() {
        try{
        Alert alert = driver.switchTo().alert();
        String alertCheck = alert.getText();
        this.bcase = true;
        System.out.println(alertCheck);
        if(alertCheck.equals("Define higher than 0 quantity for all products")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if(alertCheck.equals("Invalid Payment Document Number")){
            m.print("Correct error message appeared! User cannot advance further!");
        }else if (alertCheck.equals("Define rates for all products")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if (alertCheck.equals("Define delivery dates for all products")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if(alertCheck.equals("NOT IMPLEMENTED YET - WAITING DELIVERY DOC INPUT")){
            m.print("Correct error message appeared! User cannot advance further!");
        }else if (alertCheck.equals("You must upload an invoice file!")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if (alertCheck.equals("You must upload a payment order file!")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if (alertCheck.equals("Invalid Payment Document Date")){
            m.print("Correct error message appeared! User cannot advance further!");
        } else if (alertCheck.equals("Invalid Payment Document Number")){
            m.print("Correct error message appeared! User cannot advance further!");}  
        else if (alertCheck.equals("Invalid Payment Document Date")){
            m.print("Correct error message appeared! User cannot advance further!");} 
        else if(driver.findElement(By.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='Open Order Request']")).isDisplayed()){
            this.bcase = false;
            m.print("(FAIL!!!) The user can advance with unselected/wrongly completed fields, even though an error message is present! (FAIL!!!)");
        }   else {m.print("(FAIL!!!) The user cannot advance, but there is no pop-up message informing him of the problem! (FAIL!!!)");}
        driver.switchTo().alert().accept();
        return alert;
        } catch (NoAlertPresentException e){
            m.print("(FAIL!!!) The user can advance with wrongly completed fields, without any error popup to inform them! (FAIL!!!)");
            this.bcase = false; 
            return null;
        } catch (UnhandledAlertException f){
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            String alertCheck = alert.getText();
            if (alertCheck.equals("You must upload an invoice file! You must upload a payment order file!")){
                m.print("Correct error message appeared! User cannot advance further!");
            } else if(driver.findElement(By.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='Open Order Request']")).isDisplayed()){
                this.bcase = false;
                m.print("(FAIL!!!) The user can advance with unselected/wrongly completed fields, even though an error message is present! (FAIL!!!)");
            }driver.switchTo().alert().accept();return null;
        }
    }

`

1 个答案:

答案 0 :(得分:0)

我认为处理警报的更好方法是先等待警报出现,然后再处理。您可以使用以下两种方法执行此操作:

public boolean checkNativeAlert() {
    boolean exist = false;

    try {
        WebDriverWait wait = new WebDriverWait(webDriver, 3);
        if(!(wait.until(ExpectedConditions.alertIsPresent()) == null)) {
            exist = true;
        }
    }
    catch (Exception ignore) {}

    return exist;
}

public String getNativeTextAlert(boolean close) {
    String text = null;
    try {
        Alert alert = webDriver.switchTo().alert();
        text = alert.getText();
        if (close) {
            alert.accept();
        }
    }
    catch (Exception ignore) {}

    return text;
}

使用Selenium处理警报有点棘手,从我的角度来看更好地使用这两个其他选项:

  • 将警报更改为模态对话框(至少在调试/测试模式下)
  • 使用autoIt处理Selenium测试中的警报