章节十七章、2- 给执行失败的case截图
一、案例演示
1、首先我们把截图的方法单独进行封装方便以后调用。
package utilities; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver; public class Screenshots { // 为了方便调用,我们写成静态的方法
// 首先,我们设置在调用该方法时需要传入两个参数:浏览器类型、文件名
public static void takeScreenshots(WebDriver driver,String filename) throws IOException {
// 截图文件名
filename = filename + ".png";
// 截图存放路径
String directory = "C:\\Users\\acer\\Desktop\\dd\\Screenshots\\";
// 截图
// 强制转换driver为TackScreenShot类型,然后调用getScreenshotAs("截完图后的输出类型")方法进行截图
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// 将文件复制到指定目录
FileUtils.copyFile(sourceFile, new File(directory+filename));
}
}
package extenreports; import java.io.File;
import java.util.Date; import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme; public class ExtentFactory {
public static ExtentReports getInstance() { Date date = new Date();
String form = String.format("%tF", date);
String hour = String.format("%tH", date);
String minute = String.format("%tM", date);
String second = String.format("%tS", date);
// 生成的路径以及文件名
final String OUTPUT_FOLDER = "C:\\Users\\acer\\Desktop\\dd\\ExtentReports\\";
final String FILE_NAME = "index" + form + hour + minute + second + ".html"; // 文件夹不存在的话进行创建
File reportDir = new File(OUTPUT_FOLDER);
if (!reportDir.exists() && !reportDir.isDirectory()) {
reportDir.mkdir();
} ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
// 设置静态文件的DNS
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
// 怎么样解决cdn.rawgit.com访问不了的情况
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
htmlReporter.config().setDocumentTitle("标品页面功能自动化测试报告");
htmlReporter.config().setReportName("标品页面功能自动化测试报告");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}");
htmlReporter.config().setEncoding("gbk");
ExtentReports extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
extent.setSystemInfo("Selenium Version", "3.141.59");
extent.setSystemInfo("Platform", "Windows"); return extent;
}
}
2、以qq邮箱登录为例,我们故意输入错误的密码让case执行失败然后进行截图。
package extenreports; import org.testng.annotations.Test; import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status; import utilities.Screenshots; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass; import java.io.IOException;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult; public class TestNG_Screenshots { private WebDriver driver;
private String baseUrl; // 每个方法执行前执行一次
@BeforeMethod
public void tearUp() {
baseUrl = "https://mail.qq.com";
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.get(baseUrl);
} @Test
public void test1_invalidCredentials() {
driver.switchTo().frame("login_frame"); WebElement dl = driver.findElement(By.className("switch_btn"));
dl.click(); WebElement emailField = driver.findElement(By.id("u"));
emailField.sendKeys("12345678"); WebElement passwordField = driver.findElement(By.id("p"));
passwordField.sendKeys("***************"); WebElement goButton = driver.findElement(By.id("login_button"));
goButton.click(); WebElement welcomeText = null;
try {
welcomeText = driver.findElement(By.xpath("//b[text()='********']"));
}
catch (NoSuchElementException e) {
System.out.println(e.getMessage());
}
Assert.assertTrue(welcomeText != null);
} // 每个方法执行结束后执行一次
@AfterMethod
// 使用testng中的ITestResult接口来捕获testcase的运行结果
public void tearDown(ITestResult testResult) throws IOException {
// 如果获取到的testcase的状态等于failure(失败)
if(testResult.getStatus() == ITestResult.FAILURE) {
// 就调用自己封装好的截图方法,以testcase方法的名字给文件命名
Screenshots.takeScreenshots(driver, testResult.getName());
}
driver.quit();
}
}
运行结果:在指定目录成功生成了错误截图文件
如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。
内容具有连惯性,未标注的地方可以看前面的博客,这是一整套关于ava+selenium自动化的内容,从java基础开始。
欢迎关注,转载请注明来源。
章节十七章、2- 给执行失败的case截图的更多相关文章
- Unittest 支持 case 失败后自动截图功能的另外两种方式
原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...
- Gradle 1.12用户指南翻译——第四十七章. Build Init 插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- Gradle 1.12 翻译——第十七章. 从 Gradle 中调用 Ant
有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...
- 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...
- 【C++】《C++ Primer 》第十七章
第十七章 标准库特殊设施 一.tuple类型 tuple是类似pair的模板,每个pair的成员类型都不相同,但每个pair都恰好有两个成员. 不同的tuple类型的成员类型也不相同,一个tuple可 ...
- 【转】mysql触发器的实战(触发器执行失败,sql会回滚吗)
1 引言Mysql的触发器和存储过程一样,都是嵌入到mysql的一段程序.触发器是mysql5新增的功能,目前线上凤巢系统.北斗系统以及哥伦布系统使用的数据库均是mysql5.0.45版本,很多程 ...
- 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...
- [汇编学习笔记][第十七章使用BIOS进行键盘输入和磁盘读写
第十七章 使用BIOS进行键盘输入和磁盘读写 17.1 int 9 中断例程对键盘输入的处理 17.2 int 16 读取键盘缓存区 mov ah,0 int 16h 结果:(ah)=扫描码,(al) ...
- 第十七章——配置SQLServer(3)——配置“对即时负载的优化”
原文:第十七章--配置SQLServer(3)--配置"对即时负载的优化" 前言: 在第一次执行查询或者存储过程时,会创建执行计划并存储在SQLServer的过程缓存内存中.在很多 ...
随机推荐
- Runtime.getRuntime().exec()实现Java调用python程序
使用Runtime.getRuntime().exec()来实现Java调用python,调用代码如下所示: import java.io.BufferedReader; import java.io ...
- SSM框架——详细整合教程
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Jav ...
- SSM框架使用分页插件显示信息
1.在该SSM工程的pom.xml中加入PageHelper的分页插件依赖(若是版本号不对,可以尝试修改一下) <!-- 分页插件 --> <dependency> <g ...
- 前端利器躬行记(5)——Git
Git是一款开源的分布式版本控制系统,它的出现和Linux紧密相关.Linux内核项目组为了能更好地管理和维护Linux内核开发,于2002年开始启用商业的分布式版本控制系统BitKeeper.虽然软 ...
- 进击的 Java ,云原生时代的蜕变
作者| 易立 阿里云资深技术专家 导读:云原生时代的来临,与Java 开发者到底有什么联系?有人说,云原生压根不是为了 Java 存在的.然而,本文的作者却认为云原生时代,Java 依然可以胜任&qu ...
- nexus 私服 低配置服务器启动不能访问的问题
1核1G的渣渣服务器启动无法访问. 请更换更高配置的服务器.
- idea控制台乱码解决方案
第一步:修改intellij idea配置文件: 找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别 ...
- Hibernate 之 @Query查询
注解 @Query 允许在方法上使用 JPQL. 列如: @Query("select u from User u where u.name=?1 and u.department_id= ...
- python+selenium一:对浏览器的常规操作
# 1.打开Firefox浏览器 from selenium import webdriverdriver = webdriver.Firefox()driver.get("https:// ...
- Qt插件热加载-QPluginLoader实现
上一篇C++消息框架-基于sigslot文章中我们讲述了使用sigslot信号槽实现自己的消息框架,这是一个比较粗糙,而且小的框架.当我们的程序逐渐变大时,我们可能就会考虑功能插件化,或者支持某些模块 ...