Selenium 2 (WebDriver):在调用 Alert.getText() 之前检查文本是否存在
我遇到了在切换到警报之前检查警报是否存在<中描述的问题< /a>.我发现捕获 NullPointerException 非常糟糕。有没有人更优雅地解决这个问题?
我当前的解决方案使用捕获 NPE 的等待。客户端代码只需调用 waitForAlert(driver, TIMEOUT)
:
/**
* If no alert is popped-up within <tt>seconds</tt>, this method will throw
* a non-specified <tt>Throwable</tt>.
*
* @return alert handler
* @see org.openqa.selenium.support.ui.Wait.until(com.google.common.base.Function)
*/
public static Alert waitForAlert(WebDriver driver, int seconds) {
Wait<WebDriver> wait = new WebDriverWait(driver, seconds);
return wait.until(new AlertAvailable());
}
private static class AlertAvailable implements ExpectedCondition<Alert> {
private Alert alert = null;
@Override
public Alert apply(WebDriver driver) {
Alert result = null;
if (null == alert) {
alert = driver.switchTo().alert();
}
try {
alert.getText();
result = alert;
} catch (NullPointerException npe) {
// Getting around https://groups.google.com/d/topic/selenium-users/-X2XEQU7hl4/discussion
}
return result;
}
}
I came across the problem described in Checking If alert exists before switching to it. I find horrible to capture a NullPointerException
. Has anyone solved this problem more elegantly?
My current solution uses a wait that captures the NPE. The client code just have to invoke waitForAlert(driver, TIMEOUT)
:
/**
* If no alert is popped-up within <tt>seconds</tt>, this method will throw
* a non-specified <tt>Throwable</tt>.
*
* @return alert handler
* @see org.openqa.selenium.support.ui.Wait.until(com.google.common.base.Function)
*/
public static Alert waitForAlert(WebDriver driver, int seconds) {
Wait<WebDriver> wait = new WebDriverWait(driver, seconds);
return wait.until(new AlertAvailable());
}
private static class AlertAvailable implements ExpectedCondition<Alert> {
private Alert alert = null;
@Override
public Alert apply(WebDriver driver) {
Alert result = null;
if (null == alert) {
alert = driver.switchTo().alert();
}
try {
alert.getText();
result = alert;
} catch (NullPointerException npe) {
// Getting around https://groups.google.com/d/topic/selenium-users/-X2XEQU7hl4/discussion
}
return result;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FluentWait.until() 的 JavaDoc
由于
NullPointerException
表示错误条件,并且WebDriverWait
仅忽略NotFoundException
,因此只需删除 try/捕获块。在apply()
中抛出未经检查、未忽略的Exception
在语义上等同于在现有代码中返回null
。JavaDoc for
FluentWait.until()
Since
NullPointerException
denotes a false condition, andWebDriverWait
is only ignoringNotFoundException
, just remove the try/catch block. An unchecked, unignoredException
thrown inapply()
is semantically equivalent to returningnull
as in your existing code.基于@Joe Coder 答案,这种等待的简化版本是:
我编写了一个简短的测试来证明这个概念:
在这段代码中,在
until
中MyObject.get()
被调用四次。第三次,它抛出一个被忽略的异常,但最后一次抛出一个未被忽略的异常,中断了等待。输出(为了便于阅读而进行了简化):
请注意,由于
RuntimeException
未被忽略,因此不会打印“Returned from wait”日志。Based upon @Joe Coder answer, a simplified version of this wait would be:
I have written a short test to proof the concept:
In this code, in the
until
theMyObject.get()
is invoked four times. The third time, it throws an ignored exception, but the last one throws a not-ignored exception, interrupting the wait.The output (simplified for readability):
Note that as the
RuntimeException
is not ignored, the "Returned from wait" log is not printed.