0

我无法检测是否在原生 android 应用上显示弹出窗口。场景如下:

我开始创建工单并开始添加产品,但有时当我添加产品时,我会收到一条错误消息,提示我无法添加该产品(这是正确的)。问题是,每次我尝试添加产品时,我都需要检查是否显示了该错误。以下是我迄今为止尝试过的事情:

private boolean checkErrorInesperado() {
    try {
        //clicks on desired element
        utils.click(EMicroposVentanasErrores.BOTON_ACEPTAR);
        return true;
    } catch (NoSuchElementException ex) {
        //do nothing it's expected
        return false;
    }
}

private boolean isElementPresent() {
    return (driver.findElements(By.id("foo")).size() > 0) ? true : false;
}

但是当我运行测试时,上述方法都不起作用,有时它会卡住验证。任何帮助,将不胜感激。

4

1 回答 1

0

I am not sure if I know exactly what your problem is, since you ask about checking if a pop-up error is shown, but then you seem to be testing for a NoSuchElementException, which shouldn't cause a pop-up in the first place (since it's an error in the Java program and not the browser). If you really need to test for pop-up warnings, this worked for me:

private Alert getAlert() {
    return ExpectedConditions.alertIsPresent().apply(driver);
}

This method returns the alert, and "driver" here refers to the implementation of the WebDriver interface in the same way that you're doing in your examples.

You can use the following method to see if an alert has been raised:

private boolean isAlertPresent() {
    return ExpectedConditions.alertIsPresent().apply(driver) != null;
}

The below method, when passed an Alert (such as from the first method shown above), will return the alert's message:

private String getAlertText(Alert alert) {
    return alert.getText();
}

However, sometimes merely ordering Selenium to click on an object that it doesn't think exists will cause it to raise an exception before the alert can be logged. If you face that problem, then the following may seem like a messy solution but it has worked for me:

private void clickAndCheckAlerts() {
    try {

        // (put code for clicking a component here)

        // Optionally you may want make the thread sleep for a little while
        // since alerts may not pop up immediately
        Thread.sleep(SLEEP_TIME);

    } catch (UnhandledAlertException e) {
        // Make the following method check for alerts and log their text
        checkForAlertsAndLogAlertText();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This probably isn't the most polished or proper way to handle this situation, so if anyone knows a better way please inform us.

于 2014-10-15T20:39:52.980 回答