1 目录结构
2 详细解析
- 导入log4j-1.2.17.jar包
- 配置log4j.properties文件
#可以设置级别:debug>info>error #debug:显示debug、info、error #info: 显示info、error #error: 只error log4j.rootLogger = error,E ### 输出ERROR 级别以上的日志到=当前工程的logs文件夹中 ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =${user.dir}/logs/error.log log4j.appender.E.DatePattern = '.'yyyy-MM-dd log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %m%n
- 编写测试代码
package com.selenium.test2; import org.apache.log4j.Logger; public class TestLoger { private static Logger logger = Logger.getLogger(TestLoger.class); public static void main(String arg[]){ logger.error("This is error message."); } }
3 封装代码重构
public class MyBrowser2 { private WebDriver driver =null; private Logger logger = null; public MyBrowser2(String browsr) { this.driver = InitWebDriver.myDriver(browsr); this.logger = Logger.getLogger(MyBrowser2.class); } //页面导航 public void navigateTo(String url) { driver.navigate().to(url); } //输入框 public WebElement webEdit(String xpath){ try{ WebElement webEdit = driver.findElement(By.xpath(xpath)); webEdit.clear(); return webEdit; }catch(NoSuchElementException e){ logger.error("XPath无法定位到输入框:" + xpath); return null; }catch (ElementNotVisibleException e) { logger.error("XPath定位到多个输入框:" + xpath); return null; } } }