截图是做测试的基本技能,在有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. js页面可视区域懒加载

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 绕过UAC以管理员身份启动程序

    写这篇文章主要是看到了:http://www.7tutorials.com/use-task-scheduler-launch-programs-without-uac-prompts文章中所用到的方 ...

  3. CentOS查找文件命令

    [root@VM_147_255_centos ~]# find / -name aa.jpg 在根目录输入 如上命令,在全部目录中查找名字为aa.jpg的文件( *.jpg查找以.jpg结尾的所有文 ...

  4. web APP到底和跨平台APP开发有什么区别?

    什么是web app? WebApp是指基于Web的系统和应用,其作用是向广大的最终用户发布一组复杂的内容和功能. 说的浅显易懂点儿就是因为移动互联网特别火爆,很多企业公司也都想拥有一个属于自己的ap ...

  5. App测试流程及测试点

    1 APP测试基本流程 1.1流程图 接收版本 尽快申请到正式环境下测试 不符 App测试版本送测规范 用户行为统计测试 后台订单统计测试 尽快申请到正式环境下测试 兼容性测试.性能压力测试 功能测试 ...

  6. 运行JavaWeb项目报错Access denied for user 'root'@'localhost' (using password: YES)

    问题重现:(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000):  ...

  7. SingletonLoginUser

    package cn.com.jgt.view{ import flash.errors.IllegalOperationError; /** * actionscript类的构造方法不能是priva ...

  8. jquery.restrictFieldLength.js

    1.参考资料 http://www.cnblogs.com/aarond/archive/2013/08/02/3234042.html 2.使用举例 //字符控制 $(function () { $ ...

  9. 关于vcpkg的一些技术细节

    1.如果你启用了vcpkg integrate install,将默认采用vcpkg里安装的源而不是nuget中的 2.一般而言xxx-uwp不能用xxx-windows代替,否则回捣乱其它包 3.卸 ...

  10. 【转】Deactivating your reflector

    原文:http://blog.csdn.net/cxwl3sxl/article/details/8072195 背景: 因为想破解一个.net写的程序,需要在visual studio 2010中使 ...