selenium2截图ScreenShot的使用
截图是做测试的基本技能,在有BUG的地方,截个图,保留失败的证据,也方便去重现BUG。所以,在自动化的过程中,也要能截图,也要能在我们想要截取的地方去截图,且能在错误产生时,自动的截图。
示例:

脚本中的调用:

错误时截图:
提示:结合testng的监听器,来实现错误时截图,在之后的博客中会做介绍。
具体代码如下:
ScreenShot.java文件:
package com.selenium.utils; 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) {
this.driver = driver;
} public void takeScreenShot(String screenPath) {
try {
File srcFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(screenPath));
} catch (IOException e) {
System.out.println("Screen shot error: " + screenPath);
}
} public void takeScreenShot() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String screenName = String.valueOf(sdf.format(new Date().getTime())
+ ".jpg");
File dir = new File("test-output/snapshot");
if (!dir.exists()) {
dir.mkdir();
}
String screenPath = dir.getAbsolutePath() + File.separator + screenName;
this.takeScreenShot(screenPath);
}
}
代码中的使用方式:
package com.selenium.test; import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import com.selenium.utils.Assertion;
import com.selenium.utils.Log;
import com.selenium.utils.ScreenShot; public class testAssertion {
public static WebDriver Driver; @Test(dataProvider = "name")
public void testBaidu(String text) {
startFireFox("http://baidu.com");
System.out.println(text);
Driver.findElement(By.id("kw")).sendKeys(text);
ScreenShot image = new ScreenShot(Driver);
image.takeScreenShot();
closeFireFox();
} @DataProvider
public Object[][] name() {
return new Object[][] { { "zhangSan" }, { "liSi" } };
} public static void startFireFox(String url) {
Driver = new FirefoxDriver();
Driver.manage().window().maximize();
Driver.navigate().to(url);
} public static void closeFireFox() {
Driver.close();
}
}
最后打个广告,不要介意哦~
最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!
链接:http://www.dataguru.cn/invite.php?invitecode=G863
selenium2截图ScreenShot的使用的更多相关文章
- selenium3 + python - 异常处理截图 screenshot
一.截图方法 1.get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现IOError时候返回False,截图成功返回True. ...
- C#爬虫(03):使用Selenium
一.介绍: Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样. 1.Selenium Webdriver(也就是Selenium2 ...
- [转]vim ruby等的ide设置
使用vim做rails开发,推荐这个 https://github.com/carlhuda/janus 1. vim下的Rails常用插件 首先列出我比较常用的vim插件,基本都是网上提到的哪些.必 ...
- Msf的一些常用操作
payload的几个常用生成 生成windows下的反弹木马 msfvenom -p windows/meterpreter/reverse_tcp LHOST=60.205.212.140 LPOR ...
- ubuntu14.04 VIM for python 一键配置
# 超强vim配置文件 [](https://travis-ci. ...
- 2017-2018-2 20155315《网络对抗技术》Exp2:后门原理与实践
实验目的 学习建立一个后门连接. 教程 实验内容 使用netcat获取主机操作Shell,cron启动. 使用socat获取主机操作Shell, 任务计划启动. 使用MSF meterpreter(或 ...
- metasploit msfconsole 命令
metasploit msfconsole 命令 msf > help db_autopwn Commands =================== Command Description - ...
- java selenium 自动化笔记-不是0基础,至少有java基础
本来今天要学GitHub的,但是在群里问了下小伙伴时被暴击.说我学的东西太多太杂,不是很深入,都是皮毛.哎~自己早深有意识到,因个人能力吧,找的资料都不是很全,加上实际工作没有应用到.所以写一篇sel ...
- requests-html库render的使用
一.render的使用 from requests_html import HTMLSession session =HTMLSession() response = session.get('htt ...
随机推荐
- 微服务实践分享(2)api网关
1.作用[http://chuansong.me/n/465796751848]: 一个完整的.「面向接入」的API GW需要包含以下功能: 面向运行期 对客户端实现身份认证 通信会话的秘钥协商,报文 ...
- c#基础_ArrayList
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); //list.AddRange(list); //list.Clear();清空所有元素 ...
- Java程序运行参数
Java主函数形式:public static void main(String[] args){......} 也就是说可以向Java程序传递一个String[]. 1.在IDEA中debug.ru ...
- MFC制作简单通讯录程序
学习c++和MFC一段时间了,苦于没有项目实战,所以自己写了一个简单的简单通讯录程序,以前用c#写简单很多,例程是这本书上的实例,我的第一个winform程序也是从这本书上学的,总结c#写的话更简单, ...
- 关于纠正《Hive权威指南》中的结论~“hive在使用set自定义变量时,hivevar命名空间是可选的”~的论证
背景: 根据<Hive权威指南>上讲,在hive-0.8.0以后可以使用--define key=value命令定义用户自定义的变量以便在Hive脚本中引用.当用户使用这个功能时,Hive ...
- Hadoop 2.7.0模拟分布式实验环境搭建[亲测]
实验目的: 本实验通过在PC电脑上同时运行3个虚拟机,一个为master节点,两个slave节点. 搭建环境: 主机:mac os 10.10 OS:CenOS 6.5 虚拟机:VMware ...
- Locust安装教程与使用
Locust安装教程与使用官网地址:https://github.com/locustio/locust 如果是python3+以上的环境,需要下载locust项目源码进行安装 因Centos7.2环 ...
- UVA 674 Coin Change 硬币转换(完全背包,常规)
题意:有5种硬币,个数无限的,组成n元的不同方案有多少种? 思路:常规完全背包.重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数.状态转移方程dp[j+coin[i]]+=dp[j]. ...
- C++ 值传递&引用&地址
- springboot超详细笔记
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...