Log4j日志集成
核心代码10.1在pom.xml导入log4j依赖10.2在src/test/resources下添加日志配置命名:log4j.properties10.3在BasePage类里实例化日志对象,所有继承它的PO类均可使用到,针对想输出日志得操作添加日志代码即可10.4在WebDriverUtils类中实例化日志对象,打印浏览器操作日志
核心代码
10.1在pom.xml导入log4j依赖
<dependency
>
<groupId
>log4j
</groupId
>
<artifactId
>log4j
</artifactId
>
<version
>1.2.17</version
>
</dependency
>
10.2在src/test/resources下添加日志配置命名:log4j.properties
log4j
.rootLogger
= INFO,console
,file
log4j
.appender
.console
= org
.apache
.log4j
.ConsoleAppender
log4j
.appender
.console
.Target
= System
.out
log4j
.appender
.console
.layout
= org
.apache
.log4j
.PatternLayout
log4j
.appender
.console
.layout
.ConversionPattern
= [%p
] %d
{yyyy
-MM-dd
HH:mm
:ss
} method
: %l
----%m
%n
log4j
.appender
.file
= org
.apache
.log4j
.DailyRollingFileAppender
log4j
.appender
.file
.File
= log
/web_auto
.log
log4j
.appender
.file
.Append
= true
log4j
.appender
.file
.layout
= org
.apache
.log4j
.PatternLayout
log4j
.appender
.file
.layout
.ConversionPattern
= %d
{yyyy
-MM-dd
HH:mm
:ss
} method
: %l
- [ %p
]----%m
%n
10.3在BasePage类里实例化日志对象,所有继承它的PO类均可使用到,针对想输出日志得操作添加日志代码即可
private Logger logger
= Logger
.getLogger(BasePage
.class);
public void type(By by
,String inputData
) {
waitElementVisible(by
).sendKeys(inputData
);
logger
.info("给元素【"+by
+"】输入数据【"+inputData
+"】");
}
10.4在WebDriverUtils类中实例化日志对象,打印浏览器操作日志
public class WebDriverUtils {
public static WebDriver driver
;
public static Logger logger
= Logger
.getLogger(WebDriverUtils
.class);
public static void openBrower(String browerName
) {
if(browerName
.equals("chrome")) {
System
.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver chromedriver
= new ChromeDriver();
driver
= chromedriver
;
logger
.info("打开【chrome】浏览器");
}
}
}