在 webdriver 中用 log.error 捕获没有这样的元素?
Catch NoSuch Element with log.error in webdriver?
我添加了 log4j(apache 软件)作为我的 webdriver 测试的记录器。
我想知道 log.error 语句 是否应该作为好的做法放在 catch 块中 ?
难道我们不应该使用异常 e 对象并像这样使用 e 吗:
e.printStackTrace()?
将 log.error 添加到 catch 块中是否是正确的做法?
public static WebElement destinationTextBox(WebDdriver driver){
try{
element = driver.findElement(by...) ; //this can raise exception if no element found
}
//it will be caught here
catch(NoSuchElementException e){
log.error( "no element found") ;
}
// if element found and no exception, just log 'element found'
log.info(Destination text box element found) ;
为什么不呢?但最好连同消息一起提供原始异常,例如log.error("Message", e);
在生产代码的情况下,日志记录确实是一个好主意。但是,由于您已经提到这是一个测试代码,我建议捕获一个已检查的异常并将其作为未检查的异常重新抛出:
catch(NoSuchElementException e){
throw new RuntimeException(e);
}
这样您就不需要为可能的测试失败调查任何日志文件,但您的测试框架会明确告诉您问题(如果有的话)。
我添加了 log4j(apache 软件)作为我的 webdriver 测试的记录器。
我想知道 log.error 语句 是否应该作为好的做法放在 catch 块中 ?
难道我们不应该使用异常 e 对象并像这样使用 e 吗: e.printStackTrace()?
将 log.error 添加到 catch 块中是否是正确的做法?
public static WebElement destinationTextBox(WebDdriver driver){
try{
element = driver.findElement(by...) ; //this can raise exception if no element found
}
//it will be caught here
catch(NoSuchElementException e){
log.error( "no element found") ;
}
// if element found and no exception, just log 'element found'
log.info(Destination text box element found) ;
为什么不呢?但最好连同消息一起提供原始异常,例如log.error("Message", e);
在生产代码的情况下,日志记录确实是一个好主意。但是,由于您已经提到这是一个测试代码,我建议捕获一个已检查的异常并将其作为未检查的异常重新抛出:
catch(NoSuchElementException e){
throw new RuntimeException(e);
}
这样您就不需要为可能的测试失败调查任何日志文件,但您的测试框架会明确告诉您问题(如果有的话)。