一、案例演示

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截图的更多相关文章

  1. Unittest 支持 case 失败后自动截图功能的另外两种方式

    原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...

  2. Gradle 1.12用户指南翻译——第四十七章. Build Init 插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  3. Gradle 1.12 翻译——第十七章. 从 Gradle 中调用 Ant

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  4. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...

  5. 【C++】《C++ Primer 》第十七章

    第十七章 标准库特殊设施 一.tuple类型 tuple是类似pair的模板,每个pair的成员类型都不相同,但每个pair都恰好有两个成员. 不同的tuple类型的成员类型也不相同,一个tuple可 ...

  6. 【转】mysql触发器的实战(触发器执行失败,sql会回滚吗)

    1   引言Mysql的触发器和存储过程一样,都是嵌入到mysql的一段程序.触发器是mysql5新增的功能,目前线上凤巢系统.北斗系统以及哥伦布系统使用的数据库均是mysql5.0.45版本,很多程 ...

  7. 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记

    第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...

  8. [汇编学习笔记][第十七章使用BIOS进行键盘输入和磁盘读写

    第十七章 使用BIOS进行键盘输入和磁盘读写 17.1 int 9 中断例程对键盘输入的处理 17.2 int 16 读取键盘缓存区 mov ah,0 int 16h 结果:(ah)=扫描码,(al) ...

  9. 第十七章——配置SQLServer(3)——配置“对即时负载的优化”

    原文:第十七章--配置SQLServer(3)--配置"对即时负载的优化" 前言: 在第一次执行查询或者存储过程时,会创建执行计划并存储在SQLServer的过程缓存内存中.在很多 ...

随机推荐

  1. 为什么说 Java 程序员必须掌握 Spring Boot ?(转)

    Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,那么, Spring Boot 诞生的背景是什么?Spring 企业又是基于什么样的考虑创建 Spring Boot ...

  2. Java中各种引用(Reference)解析

    目录 1,引用类型 2, FinalReference 2.1, Finalizer 3, SoftReference 4, WeakReference 5, PhantomReference 6, ...

  3. java8 把List<Object> 根据某字段去重

      import java.util.ArrayList;import java.util.List;import org.apache.shiro.SecurityUtils;import org. ...

  4. Oracle SQL调优之绑定变量用法简介

    目录 一.SQL执行过程简介 二.绑定变量典型用法 2.1.在SQL中绑定变量 2.2.在PL/SQL中使用绑定变量 2.3.PL/SQL批量绑定变量 2.4.Java代码里使用绑定变量 最近在看&l ...

  5. 39 (OC) 瀑布流、不规则UI

    39  (OC)  瀑布流.不规则UI

  6. 03 (OC)* UITableView优化

    一:cell注册和初始化 1:不注册cell 2:注册类 3:注册nib 4:storyboard 二:核心思想 1:UITableView的核心思想是:cell的重用机制.UITbleView只会创 ...

  7. OAuth2.0 RFC 6749 中文

    英文原版:https://tools.ietf.org/html/rfc6749 转自:https://github.com/jeansfish/RFC6749.zh-cn 一.简介 在传统的客户端- ...

  8. [Advanced Python] 11 - Implement a Class

    基础概念:[Python] 08 - Classes --> Objects 进阶概念:[Advanced Python] 11 - Implement a Class 参考资源:廖雪峰,面向对 ...

  9. Django模板语言 标签整理

    Django模板语言 标签 内置标签引用 1. autoescape 控制自动转义是否可用. 这种标签带有任何 on 或 off 作为参数的话,他将决定转义块内效果. 该标签会以一个endautoes ...

  10. jenkins导致磁盘占满问题

    背景 今天登陆jenkins提示磁盘空间不足,且构建发生错误 排查问题 cd到jenkins 安装目录 执行df -h 发现root目录沾满 执行 du -ah --max-depth=1 发现是.j ...