项目地址:https://github.com/zx490336534/java_web_base
截图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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;
在执行测试的时候,使用截图的场景为:当执行失败的时候进行截图
需要使用监听,判断执行是否成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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.runTestMethod(iTestResult); Throwable throwable = iTestResult.getThrowable(); if (throwable != null) { Object object = iTestResult.getInstance(); BaseCase logincase = (BaseCase) object; String methodName = iTestResult.getName(); String className = iTestResult.getInstanceName(); String destFilename = className + "_" + methodName + "_" + System.currentTimeMillis() + ".png"; ScreenShotUtils.screenShot(logincase.driver, destFilename); }
} }
|
在testng.xml中添加
1 2 3 4 5 6 7 8 9 10 11 12
| <?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报告中
截图和报告展示是两步:WebDriver 先捕获当前可见的页面,报告再把图片作为附件。仅把图片存到磁盘,并不会让它出现在 Allure 里。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 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) { Object object = iTestResult.getInstance(); BaseCase baseCase = (BaseCase) object; TakesScreenshot screenshot = (TakesScreenshot) baseCase.driver; byte[] screenshotAs = screenshot.getScreenshotAs(OutputType.BYTES); saveScreenshot(screenshotAs); } } }
|
在testng.xml中
1 2 3
| <listeners> <listener class-name="com.zhongxin.listeners.AllureReportListener"></listener> </listeners>
|
重试
同样是首次失败,重试通过和重试后仍失败是两种结果。前者不能当作从未失败,排查时仍应保留首次失败的信息。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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; } }
|
在测试代码头部增加
1
| @Test(retryAnalyzer = MyRetry.class)
|
全部添加重试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 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) { Object object = iTestResult.getInstance(); BaseCase baseCase = (BaseCase) object; TakesScreenshot screenshot = (TakesScreenshot) baseCase.driver; byte[] screenshotAs = screenshot.getScreenshotAs(OutputType.BYTES); saveScreenshot(screenshotAs); } } }
|
在testng.xml中
1
| <listener class-name="com.zhongxin.listeners.RetryListener"></listener>
|
打开或下载 MP4 ↗
阅读视频文字稿
在纸舟假页面里,一次测试没有出现应有提示。
再试一次看见了。
原文讲截图、报告与重试,本片不截取真实屏幕,只问应该留下哪些证据。
第二次成功不能把第一次抹成从未失败。
我们把每次尝试分别归档,报告能说清先发生了什么,后来观察到了什么,还不能确定什么。
给第一次失败做一张手画证据,里面只有虚构状态区域。
截图的目的,不是给文件多加一张图,而是让读者能定位。
真实项目也应按自己的资料规则控制截图内容,不随手保留整页或其他无关窗口。
截图也不是问题原因本身;例如看不见提示,只说明那个时刻没看到。
需要结合步骤、条件与其他证据判断。
在报告总览加上尝试次数,再给失败行保留具体预期和实际。
这样能知道是哪一项没有成立,不因为后来通过就写页面永远正常。
一个可能原因是等待条件不充分,也可能是产品问题。
但这是待核对的判断,不该写成已经证实。
留下最少必要的信息和运行归属,比攒很多附件而没人能解释更有用。
让纸面模型按约定只重试一次,第二轮出现了提示。
报告要知道这不是首次通过,也不能因此倒推出第一次失败可以忽略。
如果没有合适的重试理由,重复运行只会增加噪声。
不同项目可对重试次数和对象有不同政策,原则是保留原始证据,区分重试后通过与持续失败,并让回溯到每一次尝试成为可能。
最后让同事拿一份摘要回答三个问题:失败在第几步,附件究竟能证明什么,重试有没有把原来结论藏起来。
如果他能找到两次各自的静态证据,同时看不到无关资料,报告才达到目的。
原文工具按具体环境理解,今天不抓取或上传真实画面。
重试是理解不稳定的线索,不是给失败刷绿。
评论与讨论
使用 GitHub 账号留言,评论公开显示。
点评论时间可定位到原评论,在 GitHub 用评论旁的「…」修改或删除自己的留言。