Selenium webdriver Selenium 2(WebDriver):在调用Alert.getText()之前检查文本是否存在
我遇到了中描述的问题。我发现捕获Selenium webdriver Selenium 2(WebDriver):在调用Alert.getText()之前检查文本是否存在,selenium-webdriver,webdriver,alert,Selenium Webdriver,Webdriver,Alert,我遇到了中描述的问题。我发现捕获NullPointerException非常糟糕。有人能更优雅地解决这个问题吗 我当前的解决方案使用捕获NPE的等待。客户端代码只需调用waitForAlert(驱动程序,超时): /** *如果在几秒钟内没有弹出警报,此方法将抛出 *一种未指定的可丢弃的东西。 * *@返回警报处理程序 *@see org.openqa.selenium.support.ui.Wait.until(com。谷歌。普通的。基本功能) */ 公共静态警报waitForAler
NullPointerException
非常糟糕。有人能更优雅地解决这个问题吗
我当前的解决方案使用捕获NPE的等待。客户端代码只需调用waitForAlert(驱动程序,超时)
:
/**
*如果在几秒钟内没有弹出警报,此方法将抛出
*一种未指定的可丢弃的东西。
*
*@返回警报处理程序
*@see org.openqa.selenium.support.ui.Wait.until(com。谷歌。普通的。基本功能)
*/
公共静态警报waitForAlert(WebDriver驱动程序,整数秒){
Wait Wait=新的WebDriverWait(驱动程序,秒);
返回wait.until(new AlertAvailable());
}
私有静态类AlertAvailable实现了ExpectedCondition{
私有警报=空;
@凌驾
公共警报应用(WebDriver){
警报结果=空;
如果(空==警报){
alert=驱动程序.switchTo().alert();
}
试一试{
alert.getText();
结果=警报;
}捕获(NullPointerException npe){
//四处走动https://groups.google.com/d/topic/selenium-users/-X2XEQU7hl4/discussion
}
返回结果;
}
}
JavaDoc forFluentWait.until()
重复将此实例的输入值应用于给定函数,直到出现以下情况之一:
NullPointerException
表示错误条件,并且WebDriverWait
仅忽略NotFoundException
,因此只需删除try/catch块。apply()
中抛出的未经检查、未识别的异常
在语义上等同于在现有代码中返回null
private static class AlertAvailable implements ExpectedCondition<Alert> {
@Override
public Alert apply(WebDriver driver) {
Alert result = driver.switchTo().alert();
result.getText();
return result;
}
}
私有静态类AlertAvailable实现了ExpectedCondition{
@凌驾
公共警报应用(WebDriver){
警报结果=driver.switchTo().Alert();
result.getText();
返回结果;
}
}
基于@Joe Coder,此等待的简化版本为:
/**
*如果在几秒钟内没有弹出警报,此方法将抛出
*一种未指定的可丢弃的东西。
*
*@返回警报处理程序
*@see org.openqa.selenium.support.ui.Wait.until(com。谷歌。普通的。基本功能)
*/
公共静态警报waitForAlert(WebDriver驱动程序,整数秒){
Wait Wait=new WebDriverWait(驱动程序,秒)
.ignore(NullPointerException.class);
返回wait.until(new AlertAvailable());
}
私有静态类AlertAvailable实现了ExpectedCondition{
@凌驾
公共警报应用(WebDriver){
Alert Alert=驱动程序.switchTo().Alert();
alert.getText();
返回警报;
}
}
我写了一个简短的测试来证明这个概念:
import com.google.common.base.Function;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.concurrent.TimeUnit;
导入org.junit.Test;
导入org.openqa.selenium.support.ui.FluentWait;
导入org.openqa.selenium.support.ui.Wait;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
公共类TestUntil{
私有静态记录器log=LoggerFactory.getLogger(TestUntil.class);
@试验
公共测试单元(){
Wait w=new FluentWait(new MyObject())
.带超时(30,时间单位。秒)
.忽略(NullPointerException.class);
调试(“等待…”);
w、 直到(新函数(){
@凌驾
公共对象应用(MyObject输入){
返回input.get();
}
});
log.debug(“等待返回”);
}
私有静态类MyObject{
迭代器结果=新的ArrayList(){
{
此参数为.add(null);
此参数为.add(null);
添加(新的NullPointerException(“NPE忽略”);
添加(新的运行时异常(“RTE未被忽略”);
}
}.iterator();
int i=0;
公共对象get(){
调试(“调用{},++i);
Object n=results.next();
if(n instanceof RuntimeException){
RuntimeException rte=(RuntimeException)n;
debug(“在{}调用中引发异常:{}”,i,rte);
投掷rte;
}
debug(“调用的结果{}:{}',i,n);
返回n;
}
}
}
在这段代码中,在中,直到调用了四次MyObject.get()
。第三次,它抛出一个忽略的异常,但最后一次抛出一个未忽略的异常,从而中断等待
输出(为了可读性而简化):
等待,直到。。。
调用1
调用1的结果:“null”
调用2
调用2的结果:“null”
调用3
在3调用中引发异常:java.lang.NullPointerException:NPE已忽略
调用4
在4调用中引发异常:java.lang.RuntimeException:RTE未被忽略
------------- ---------------- ---------------
Testcase:testUnit(org.lila_project.selenium_tests.tmp.TestUntil):导致错误
不可忽视
java.lang.RuntimeException:未忽略RTE
在org.lila_project.selenium_tests.tmp.TestUntil$MyObject$1.(TestUntil.java:42)
在org.lila_project.selenium_tests.tmp.TestUntil$MyObject上(TestUntil.java:37)
在org.lila_project.selenium_tests.tmp.TestUntil$MyObject上(TestUntil.java:36)
位于org.lila_project.selenium_tests.tmp.TestUntil.testUnit(TestUntil.java:22)
请注意,由于未忽略运行时异常
,因此不会打印“从等待中返回”日志。对我来说就是这样做的。非常感谢!你把我指给我看
Waiting until...
Invocation 1
Result of invocation 1: 'null'
Invocation 2
Result of invocation 2: 'null'
Invocation 3
Throwing exception in 3 invocation: java.lang.NullPointerException: NPE ignored
Invocation 4
Throwing exception in 4 invocation: java.lang.RuntimeException: RTE not ignored
------------- ---------------- ---------------
Testcase: testUnit(org.lila_project.selenium_tests.tmp.TestUntil): Caused an ERROR
RTE not ignored
java.lang.RuntimeException: RTE not ignored
at org.lila_project.selenium_tests.tmp.TestUntil$MyObject$1.<init>(TestUntil.java:42)
at org.lila_project.selenium_tests.tmp.TestUntil$MyObject.<init>(TestUntil.java:37)
at org.lila_project.selenium_tests.tmp.TestUntil$MyObject.<init>(TestUntil.java:36)
at org.lila_project.selenium_tests.tmp.TestUntil.testUnit(TestUntil.java:22)