selenium2 断言失败自动截图 (四)
一般web应用程序出错过后,会抛出异常。这个时候能截个图下来,当然是极好的。
selenium自带了截图功能。
//获取截图file
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//将图片移动到指定位置
FileUtils.moveFile(scrFile, new File(newFilePath));
当然在截图之前 需要判断新路径是否合法。下面贴出整体的代码。
package common;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class ScreenShot {
public WebDriver driver;
public ScreenShot(WebDriver _driver){
driver=_driver;
}
private void takeScreenshot(String screenPath){
try {
//获取截图file
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//将图片移动到指定位置
FileUtils.moveFile(scrFile, new File(screenPath));
} catch (IOException e) {
e.printStackTrace();
}
}
public void takeScreenshot(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd(hh_mm_ss)SSS");
String fileName=dateFormat.format( new Date());
//创建新的文件名
String screenName=fileName+".jpg";
//创建文件夹
File dir = new File("test-output/snapshot");
if (!dir.exists()){
dir.mkdirs();
}
//获取新文件名的绝对路径
String screenPath= dir.getAbsolutePath()+"/"+screenName;
//截图
this.takeScreenshot(screenPath);
}
}
调用
ScreenShot ss = new ScreenShot(Driver);
selenium2 断言失败自动截图 (四)的更多相关文章
- selenium2入门 断言失败自动截图 (四)
一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...
- Selenium2+python自动化67-用例失败自动截图【转载】
前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...
- testng 失败自动截图
testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...
- TestNG监听器实现用例运行失败自动截图、重运行功能
注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...
- QTP场景恢复之用例失败自动截图
以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...
- Webdriver+Testng实现测试用例失败自动截图功能
testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...
- TestNG实现用例运行失败自动截图(转载)
转载自:https://blog.csdn.net/galen2016/article/details/70193684 重写Listener的onTestFailure方法 package com. ...
- appium自动化,失败自动截图
1.创建监听器类TestNGListener,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package utils; import cases.Appi ...
- TestNG失败自动截图
转自:https://www.cnblogs.com/tobecrazy/p/4814813.html
随机推荐
- c++ 基础知识 0001 const 知识1
1. C++ const用法 尽可能使用const 2. C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就 ...
- python学习之输出与文件读写
#1. 打印字符串print ("His name is %s"%("Aviad")) #2.打印整数print ("He is %d years o ...
- poj 1952 最长公共子序列计数
看代码就懂了 不解释 3 1 1 1 1 2 2 2 1 1 1 3 第一个3 和最后一个 3 只需要一个就够了,,, #include<iostream> #include< ...
- 【抽时间 试验】hibernate集合映射inverse和cascade详解
http://www.cnblogs.com/amboyna/archive/2008/02/18/1072260.html
- 【转载】Allegro Auto Rename器件反标注教程
Cadence设计时一般不主张在PCB文件中更改Logic(PADS的ECO更改),不过Auto Rename仍然是非常实用的功能,按照布局重排位号,可以让PCB的丝印标识更清晰,容易检查,位号易找, ...
- weblogic控制台的启动与禁用
在一些安全漏洞扫描中,经常会扫描发现,使用weblogic管理控制台,会有个中危的漏洞. http://192.168.10.46:7001/console/login/LoginForm.jsp W ...
- Nginx 教程示例
https://www.cnblogs.com/jingmoxukong/p/5945200.html
- HTTP/HTTPS原理详解
简介 HTTP(Hypertext Transfer Protocal,超文本传输协议)是WWW(World Wide Web,万维网)数据传输的基础,规定如何传输超文本.HTTP协议存在多个版本:H ...
- Oracle中遇到的错误
1. ORA-00937: 不是单组分组函数 和 不是group by表达式 --select count(corp_tn),state_code from t_oa_main where cor ...
- PL/SQL 训练08--触发器
--什么是触发器呢?--一触即发,某个事件发生时,执行的程序块?--数据库触发器是一个当数据库发生某种事件时作为对这个事件的响应而执行的一个被命名的程序单元 --适合场景--对表的修改做验证--数据库 ...