截图是做测试的基本技能,在有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的使用的更多相关文章

  1. selenium3 + python - 异常处理截图 screenshot

    一.截图方法 1.get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现IOError时候返回False,截图成功返回True. ...

  2. C#爬虫(03):使用Selenium

    一.介绍: Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样. 1.Selenium Webdriver(也就是Selenium2 ...

  3. [转]vim ruby等的ide设置

    使用vim做rails开发,推荐这个 https://github.com/carlhuda/janus 1. vim下的Rails常用插件 首先列出我比较常用的vim插件,基本都是网上提到的哪些.必 ...

  4. Msf的一些常用操作

    payload的几个常用生成 生成windows下的反弹木马 msfvenom -p windows/meterpreter/reverse_tcp LHOST=60.205.212.140 LPOR ...

  5. ubuntu14.04 VIM for python 一键配置

    # 超强vim配置文件 [![Build Status](https://travis-ci.org/ma6174/vim.png?branch=master)](https://travis-ci. ...

  6. 2017-2018-2 20155315《网络对抗技术》Exp2:后门原理与实践

    实验目的 学习建立一个后门连接. 教程 实验内容 使用netcat获取主机操作Shell,cron启动. 使用socat获取主机操作Shell, 任务计划启动. 使用MSF meterpreter(或 ...

  7. metasploit msfconsole 命令

    metasploit msfconsole 命令 msf > help db_autopwn Commands =================== Command Description - ...

  8. java selenium 自动化笔记-不是0基础,至少有java基础

    本来今天要学GitHub的,但是在群里问了下小伙伴时被暴击.说我学的东西太多太杂,不是很深入,都是皮毛.哎~自己早深有意识到,因个人能力吧,找的资料都不是很全,加上实际工作没有应用到.所以写一篇sel ...

  9. requests-html库render的使用

    一.render的使用 from requests_html import HTMLSession session =HTMLSession() response = session.get('htt ...

随机推荐

  1. c# 类名不同,字段相同,如何快速给类赋值

    1,相关的两个类如下:现在我们知道短信的值,如何给微信来赋值呢,需要用到我们的反射,当然字段一一赋值也可以,但是如果字段有100个,这个方法就不行啦 /// <summary> /// 短 ...

  2. 【转】LINQ to SQL语句(1)之Where

    Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形 ...

  3. spring项目后台中打印sql语句

    配置方法很简单,只要在配置文件application.yml加入以下语句 logging: level: main.java.iReview.dao: debug 其中包的位置应为dao层的包位置,不 ...

  4. css3的过渡、动画、2D、3D效果

    浏览器的内核: 谷歌的内核是:webkit 火狐的内核是:gecko Ie的内核是:trident 欧鹏的内核是:presto 国内浏览器的内核:webkit css3针对同一样式在不同的浏览器的兼容 ...

  5. 编译错误you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)

    解决方法: export FORCE_UNSAFE_CONFIGURE=1

  6. git如何强制用远程分支更新本地

    git本地即使有修改如何强制更新: 本地有修改和提交,如何强制用远程的库更新本地.我尝试过用git pull -f,总是提示 You have not concluded your merge. (M ...

  7. 洛谷 P3019 [USACO11MAR]会见点Meeting Place

    题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Bessie and Jonell are great friends. Since Farmer John ...

  8. stixel world论文总结

    1.The Stixel World - A Compact Medium Level Representation of the 3D-World:http://pdfs.semanticschol ...

  9. ASP.NET WebApi 路由配置

    ASP.NET Web API路由是整个API的入口.我们访问某个资源就是通过路由映射找到对应资源的URL.通过URL来获取资源的. 对于ASP.NET Web API内部实现来讲,我们的请求最终将定 ...

  10. HTML5<header>元素

    HTML5<header>元素 1.header元素描述了文档的头部区域,主要用于定义内容的介绍展示区域. 2.实例: <header> <h2>heder元素描述 ...