java - 在 webdriver 中使用 log.error 捕获没有此类元素?

标签 java design-patterns selenium logging try-catch

我将 log4j(apache 软件)作为记录器添加到我的 webdriver 测试中。

我想知道 log.error 语句是否应该放在 catch block 中作为良好实践

我们不是应该使用异常 e 对象并像这样使用 e 吗: e.printStackTrace()?

将 log.error 添加到 catch block 中是否是正确的做法?

​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("消息", e);

关于java - 在 webdriver 中使用 log.error 捕获没有此类元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29101416/

相关文章:

java - 为什么我的物体朝错误的方向移动?

c# - 在不创建新类的情况下添加更多行为

python - Selenium通过Python使用find_element时找不到元素

jquery - 确定 JQuery 效果是否仍在执行

java - 在 Java 中为实体 Bean 定义常量值变量的最佳位置

java - SLF4J 在不同的appender中以不同的级别记录相同的类

c++ - C++中的内存管理模式

java - java selenium 如何获取下一个元素的值

java - Spring Boot中等待数据库连接无一异常(exception)

c - C 语言的设计原理 DRY?