一般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 断言失败自动截图 (四)的更多相关文章

  1. selenium2入门 断言失败自动截图 (四)

    一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...

  2. Selenium2+python自动化67-用例失败自动截图【转载】

    前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...

  3. testng 失败自动截图

    testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...

  4. TestNG监听器实现用例运行失败自动截图、重运行功能

    注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...

  5. QTP场景恢复之用例失败自动截图

    以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...

  6. Webdriver+Testng实现测试用例失败自动截图功能

    testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...

  7. TestNG实现用例运行失败自动截图(转载)

    转载自:https://blog.csdn.net/galen2016/article/details/70193684 重写Listener的onTestFailure方法 package com. ...

  8. appium自动化,失败自动截图

    1.创建监听器类TestNGListener,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package utils; import cases.Appi ...

  9. TestNG失败自动截图

    转自:https://www.cnblogs.com/tobecrazy/p/4814813.html

随机推荐

  1. 使用LNMP环境安装typecho博客的全程记录

    虽然我是搞asp.net的 但是十分欣赏php,php有很多开源的博客程序 比如大名鼎鼎的Wordpress.还有各种独立博客大牛使用的z-blog,以及短小精悍的emblog. wordpress臃 ...

  2. BaseCommand

    import java.io.Serializable; import android.util.Log; public class BaseCommand implements Serializab ...

  3. 如何将u盘、移动硬盘转化为活动分区--绝招

    https://jingyan.baidu.com/article/e75057f2a6a18aebc91a893e.html

  4. bzoj 1725 Corn Fields

    Written with StackEdit. Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行\((1<=M<=12; 1<=N< ...

  5. UVA10652 Board Wrapping

    题意 PDF 分析 就是一个裸的凸包. 如何确定点?就用中心向四边连垂直的向量然后旋转,加上中心点,即可得出旋转后的点. 时间复杂度\(O(T n \log n)\) 代码 #include<i ...

  6. fn project 私有镜像发布

    1. 说明 fnproject 默认的docker registry 是 dockerhub 对于企业应用还是不太方便的 还好系统系统了配置参数方便我们进行配置,与开源harbor 进行集成 2. 使 ...

  7. 软件开发中IT用语-日文和英文对照版

    開発工程 † 要件定義…Requirement Definition (Analyze) 外部設計…External Design 内部設計…Internal Design 開発…Coding テスト ...

  8. 在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解决,谢谢!

    在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解 ...

  9. 根据wsdl文件生成WebService客户端代码

    有时候在项目中,一个项目可能有好几个公司在做.系统之间难免会出现互相调用接口的现象,这时候有一种办法就是使用webService.本篇文章将介绍如何将对接系统提供的WebService接口,根据对方提 ...

  10. 校赛热身赛 Problem D. Unsolved Mystery

    Problem D. Unsolved MysteryThe world famous mathematician Wang Hao published a paper recently in the ...