项目地址:https://github.com/zx490336534/java_web_base
截图
package com.zhongxin.demo;
import com.zhongxin.common.BaseCase;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import java.io.File;
public class ScreenShotDemo extends BaseCase {
@Test
public void test() throws Exception {
WebDriver driver = open("chrome");
driver.get("htts://www.baidu.com");
Thread.sleep(2000);
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
System.out.println(file.getAbsoluteFile());
File destFile = new File("aaa.png");
FileUtils.moveFile(file, destFile);
close(driver);
}
}
强制类型转换driver
TakesScreenshot screenshot = (TakesScreenshot) driver;
在执行测试的时候,使用截图的场景为:当执行失败的时候进行截图
需要使用监听
,判断执行是否成功
package com.zhongxin.listeners;
import com.zhongxin.cases.LoginCase;
import com.zhongxin.common.BaseCase;
import com.zhongxin.utils.ScreenShotUtils;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
public class ScreenShotListener implements IHookable {
@Override
public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
// iHookCallBack 用例执行@Test方法的
// iTestResult @Test方法的结果(@Test对象/方法名/是否抛出异常)
iHookCallBack.runTestMethod(iTestResult);
Throwable throwable = iTestResult.getThrowable();
if (throwable != null) {
//throwable不等于异常说明@Test出现异常了,执行截图
Object object = iTestResult.getInstance();
BaseCase logincase = (BaseCase) object;
//获取当前@Test方法名
String methodName = iTestResult.getName();
//获取当前@Test类名
String className = iTestResult.getInstanceName();
String destFilename = className + "_" + methodName + "_" + System.currentTimeMillis() + ".png";
ScreenShotUtils.screenShot(logincase.driver, destFilename);
}
}
}
在testng.xml
中添加
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="futureloan">
<listeners>
<listener class-name="com.zhongxin.listeners.ScreenShotListener"></listener>
</listeners>
<test name="登录测试">
<classes>
<class name="com.zhongxin.cases.LoginCase"></class>
</classes>
</test>
</suite>
将截图展示在Allure报告中
package com.zhongxin.listeners;
import com.zhongxin.common.BaseCase;
import io.qameta.allure.Attachment;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
public class AllureReportListener implements IHookable {
@Attachment(value = "Page screenshot", type = "image/png")
public byte[] saveScreenshot(byte[] screenShot) {
return screenShot;
}
@Override
public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
iHookCallBack.runTestMethod(iTestResult);
Throwable throwable = iTestResult.getThrowable();
if (throwable != null) {
//throwable不等于异常说明@Test出现异常了,执行截图
Object object = iTestResult.getInstance();
BaseCase baseCase = (BaseCase) object;
TakesScreenshot screenshot = (TakesScreenshot) baseCase.driver;
byte[] screenshotAs = screenshot.getScreenshotAs(OutputType.BYTES);
saveScreenshot(screenshotAs);
}
}
}
在testng.xml
中
<listeners>
<listener class-name="com.zhongxin.listeners.AllureReportListener"></listener>
</listeners>
重试
package com.zhongxin.listeners;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class MyRetry implements IRetryAnalyzer {
private int retryCount = 0;//当前重试次数
private static final int maxRetryCount = 3;//最大重试次数
@Override
public boolean retry(ITestResult iTestResult) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
}
在测试代码头部增加
@Test(retryAnalyzer = MyRetry.class)
全部添加重试
package com.zhongxin.listeners;
import com.zhongxin.common.BaseCase;
import io.qameta.allure.Attachment;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
public class AllureReportListener implements IHookable {
@Attachment(value = "Page screenshot", type = "image/png")
public byte[] saveScreenshot(byte[] screenShot) {
return screenShot;
}
@Override
public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
iHookCallBack.runTestMethod(iTestResult);
Throwable throwable = iTestResult.getThrowable();
if (throwable != null) {
//throwable不等于异常说明@Test出现异常了,执行截图
Object object = iTestResult.getInstance();
BaseCase baseCase = (BaseCase) object;
TakesScreenshot screenshot = (TakesScreenshot) baseCase.driver;
byte[] screenshotAs = screenshot.getScreenshotAs(OutputType.BYTES);
saveScreenshot(screenshotAs);
}
}
}
在testng.xml
中
<listener class-name="com.zhongxin.listeners.RetryListener"></listener>