第一步:编写日志监听器
首先项目依赖肯定得要有 testng 依赖,然后我们在 listener 包中编写监听器类,通过继承 testng 的 TestListenerAdapter 接口,代码如下:
package com.test.testng;
import com.test.testng.TestDemo;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DotTestListener extends TestListenerAdapter {
private int m_count = 0;
/**
* Start
* @param tc ITestContext
*/
@Override
public void onStart(ITestContext tc) {
log(String.format("====================%s Test Start====================", tc.getName()));
}
/**
* 测试开始
*
* @param tr ITestResult
*/
@Override
public void onTestStart(ITestResult tr) {
log(String.format("========%s.%s Test Start========", tr.getInstanceName(), tr.getName()));
}
/**
* Test Failure
* @param tr ITestResult
*/
@Override
public void onTestFailure(ITestResult tr) {
log(String.format("========%s.%sTest failure, reason as blew:\n%s========\n", tr.getInstanceName(), tr.getName(), tr.getThrowable()));
/*
*出现异常截图
*/
}
/**
* Test Skip
* @param tr ITestResult
*/
@Override
public void onTestSkipped(ITestResult tr) {
log(String.format("========%s.%sSkip Test========", tr.getInstanceName(), tr.getName()));
}
/**
* Test Success
* @param tr ITestResult
*/
@Override
public void onTestSuccess(ITestResult tr) {
log(String.format("========%s.%sTest Success========", tr.getInstanceName(), tr.getName()));
}
/**
* End
*
* @param tc ITestContext
*/
@Override
public void onFinish(ITestContext tc) {
log(String.format("====================%sTest end====================", tc.getName()));
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
第二步:失败时截图
监听失败时截图,在onTestFailure方法里调用TakeScreenShot方法进行截图,但是ScreenShot类里需要传一个driver进去,使用在定义的新类当中加入getInstance()方法,返回值即为该类的一个对象:
需要在监听类里面定义一个全局静态变量driver
public static WebDriver driver;
在测试类里面将其赋值(在打开了浏览器之后)
new DotTestListener().driver=driver;
if(driver!=null)
this.TakeScreenShot(driver);
public void TakeScreenShot(WebDriver driver){
//获取时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String curTime = sdf.format(new Date());
//获取类名
String curClassName = this.getClass().getName();
//路径
String curPath = System.getProperty("user.dir");
String pngPath = curClassName+"_"+curTime+".png";
File ScrPng = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
try {
//复制内容到指定文件中
FileUtils.copyFile(ScrPng, new File(curPath+"/"+pngPath));
log("Screenshot save path: "+curPath+"/"+pngPath);
//Files.copy(ScrPng,new File(curPath+"/"+pngPath));
} catch (IOException e) {
e.printStackTrace();
}
}
第三步:加入监听
@Listeners(DotTestListener.class)
public class TestDemo {
public WebDriver driver;
@BeforeTest
public void init() {
String url = "http://www.baidu.com";
//新建一个浏览器句柄
driver = new Brower().firefox();
//driver = (WebDriver) new FirefoxDriver();
//打开URL
driver.get(url);
}
@Test
@Parameters("searchWord")
public void search(String searchWord) throws InterruptedException {
//输入搜索字符串
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(searchWord);
//点击[百度一下]按钮
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
Thread.sleep(2000);
}
@Test
public void verifyLastName() {
String m_lastName = "Beust";
assert "Beust".equals(m_lastName) : "Expected name Beust, for" + m_lastName;
}
@Test
public void verify() {
String m_lastName = "Beust1";
assertEquals("Beust", m_lastName);
}
@BeforeMethod
public void beforeMethod(){
System.out.println("beforeMethod");
}
@AfterMethod
public void afterMethod(){
System.out.println("afterMethod");
}
@BeforeClass
public void beforeClass(){
System.out.println("beforeClass");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass");
}
@BeforeSuite
public void beforeSuite(){
System.out.println("beforeSuite");
}
@AfterSuite
public void afterSuite(){
System.out.println("afterSuite");
}
@AfterTest
public void teardown() {
driver.quit();
}
}
第四步:测试结果