selenium - 如何在 Selenium 黄瓜框架中通过测试用例的范围报告中添加屏幕截图
问题描述
如何在 Selenium 黄瓜框架中通过测试用例的范围报告中添加屏幕截图。
@After(order = 1)
public void after(Scenario scenario) throws IOException {
TransferFiles files = new TransferFiles();
String buildPath;
String finalFile;
if (scenario.isFailed()) {
String screenshotName = scenario.getName().replaceAll(" ", "_") + "_" + String.valueOf(random);
try {
//This takes a screenshot from the driver at save it to the specified location
File sourcePath = (((TakesScreenshot) TestBase.driver).getScreenshotAs(OutputType.FILE));
File fileTempImg = new File("C:\\ScreenShot0011\\001temp001.png");
FileUtils.copyFile(sourcePath, fileTempImg);
InetAddress addr;
addr = InetAddress.getLocalHost();
buildPath = String.valueOf(fileTempImg).replaceAll("C:", addr.getHostName());
finalFile = "//" + buildPath.replaceAll("\\\\", "/");
//finalFile = "\\\\" + buildPath;
files.transferFiles(finalFile, screenshotName, "png");
//This attach the specified screenshot to the test
addScreenCaptureFromPath("path/" + screenshotName + ".png");
fileTempImg.delete();
} catch (Exception e) {
System.out.println("The specified file have not been found on the local machine:-" + e.getMessage());
}
}
解决方案
使用@After 注释,您将能够在您的范围报告中嵌入屏幕截图。你可以有 if else 阻止。一个块用于失败的测试和其他用于通过的测试用例,在这里您编写代码以将屏幕截图嵌入报告中。
@After
public void afterScenario(Scenario scenario){
try{
if(scenario.isFailed()){
// More code goes here.
}else {
//------------------------- Attaching Screen shot in the Report -------------------------
byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
ExtentManager.getReporter().flush();
}
catch(Exception e){
scenario.write("WARNING. Failed to take screenshot with following exception : "+e.getMessage());
}
}
推荐阅读
- ffmpeg - 如何获取输入视频的尺寸和纵横比并将其作为参数传递给同一命令中的另一个输入?
- php - Magento 2 - 如何在“添加到购物车”按钮下方的产品视图页面上显示自定义添加属性?
- javascript - 如何使用 async/await 在 Express 中完成这项工作
- php - 检查IP是否访问页面
- python-3.x - 使用图像进行情感分析
- probability - 使用 SGDclassifier 的 predict_proba 进行多类分类的多类分类置信度得分
- python - 识别 XML python 中的特殊字符
- emacs - Doom Emacs 中 Agda-mode 2 的语法高亮显示
- r - 如何在 R 中创建函数?
- kubernetes - 如何等到在 jelastic 清单安装中创建 appid 的 env?