截图是做测试的基本技能,在有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. vuex填坑记录

    vuex是需要等待页面加载完成后才会更新的,如果页面点击刷新有用到vuex的地方,那么vuex会保持旧的数据,等刷新完成后vuex的数据才会重置,所以要在页面加载后再调用vuex的数据才是正确的,如果 ...

  2. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(3):添加一个视图

    3. 添加一个视图 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-vi ...

  3. MoinMoin install in apache (win)

    一:下载环境 xampp:http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.8.1/xampp-win32-1.8.1-VC9 ...

  4. [转]使用 HTML5 WebSocket 构建实时 Web 应用

    HTML5 WebSocket 简介和实战演练 本文主要介绍了 HTML5 WebSocket 的原理以及它给实时 Web 开发带来的革命性的创新,并通过一个 WebSocket 服务器和客户端的案例 ...

  5. 多线程----Thread类,Runnable接口,线程池,Callable接口,线程安全

    1概念 1.1进程 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 任务管理器中: 1.2线程 线程是进程中的一个执行单元 ...

  6. 告别CMD.windows终端神器conemu设置

    前言 一种刘姥姥进大观园的感觉,现在是见啥啥新鲜.因为之前不怎么接触到命令操作,平时偶尔用用cmd也没觉得什么不妥.直到现在经常调试脚本,使用git越发感觉不方便.看见同事使用的terminal绚丽夺 ...

  7. 利用bintray-release插件上传到Bintray- HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]问题解决

    凡是网上教程 有个5678步的总有这样或者那样的坑. 上周撸了一个小工具准备上传到jcenter,方便管理以及以后使用.看了一下教程,短短几步,弄了很久. 按Hongyang的教程http://www ...

  8. Java中类成员变量初始化顺序

    一. 定义处默认初始化vs构造函数中初始化 java中类成员变量支持在声明处初始化,也可以在构造函数中初始化,那么这两者有什么区别呢?看下面例子 public class FieldsInit { p ...

  9. 在广州学习PHP零基础可以学习吗?

    PHP现今作为互联网运用很广泛的编程语言,市场需求量也越来越高,而PHP开发工程师的薪资也是一路水涨船高,更多的人看到了PHP的发展前景,纷纷都想投入到PHP的开发大军中来,那么对于很多转行或者零基础 ...

  10. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...