Selenium Webdriver——实现截图功能
截图方法
public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name String currentPath = System.getProperty("user.dir"); //get current work folder
System.out.println(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:"+currentPath+"/"+filename);
FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{ System.out.println("screen shot finished");
}
}
1.使用selenium打开百度,截图;
2.输入selenium关键字
3.搜索,截图
具体代码如下:
import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; public class TestSaveScreen { private WebDriver driver;
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.get("https://www.baidu.com/");
snapshot((TakesScreenshot)driver,"open_baidu.png");
}
@Test
public void test() {
WebElement searchInput= driver.findElement(By.id("kw"));
searchInput.sendKeys("selenium"); WebElement searchButton= driver.findElement(By.id("su"));
searchButton.click();
snapshot((TakesScreenshot)driver,"search_output.png");
} @AfterClass
public void afterClass() {
driver.quit();
} public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name String currentPath = System.getProperty("user.dir"); //get current work folder
System.out.println(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:"+currentPath+"/"+filename);
FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{ System.out.println("screen shot finished");
}
}
}
截图效果如下:


Selenium Webdriver——实现截图功能的更多相关文章
- Selenium webdriver实现截图功能
可参考http://www.cnblogs.com/tobecrazy/p/3599568.html Webdriver截图时,需要引入: import java.io.File; import ja ...
- selenium提供的截图功能
get_screenshot_as_file()提供一个截屏功能.在自动化执行过程中,执行失败后只能看到代码的运行错误,而不能直接看到ui上的错误,利用截屏保存下来很容易的进行问题的判断 先来执行一个 ...
- Selenium Webdriver 自动化测试开发常见问题(C#版)
转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver ...
- Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能
之前的博文 020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小) 简述了通过 JavaScript 获取浏览器显示区域大小,此文将简述另一种获取浏览器显 ...
- selenium + python实现截图并且保存图片
webdriver的截图功能十分强悍,无论页面多长,webdriver都能比较完美的截到完整的页面. python代码: # -*- coding: utf-8 -*-from selenium im ...
- 封装selenium自动化框架中的截图功能
对selenium自带的截图功能进行封装: 以下为封装的代码,自定义一个.py文件即可,图片路径自己设置一个. #coding:utf-8 class Screen(object): ''' 封装的截 ...
- selenium截图功能
selenium自动化测试完后需要查看值观的结果,或者查操作过程中是否正确,此时需要使用自带的截图功能. 示例1: from time import sleep from selenium impor ...
- selenium web driver 实现截图功能
在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...
- Selenium webdriver 截图 太长截不全的问题
Selenium webdriver 截图 太长截不全的问题 1.环境 selenium webdriver.net 2.46.0.0 + firefox 37.0.1 + win 8.1 2.问题 ...
随机推荐
- apache允许列目录
vi /etc/httpd/conf.d/welcom.conf <LocationMatch "^/+$"> #Options -Indexes vi /etc ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- System.ComponentModel.DataAnnotations 命名空间和RequiredAttribute 类
System.ComponentModel.DataAnnotations 命名空间提供定义 ASP.NET MVC 和 ASP.NET 数据控件的类的特性. RequiredAttribute 指定 ...
- 【转】MongoDB 3.0 正式版本即将发布,强力推荐
MongoDB 今天宣布3.0 正式版本即将发布.这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统. MongoDB 3.0 在性能和伸缩性方面都有 ...
- 作业要求 20181030-1 Alpha发布用户使用报告
作业链接[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2325] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维 王玉潘 赵美增 ...
- L193
Real generosity toward the future lies in giving all to the present.将一切奉献给当下,就是对未来的真正慷慨.The wheels o ...
- c#实现对登陆信息的反馈,实现对网站登录密码的扫描
最近发现我们学校的电信上网改密码的页面很简单,没有验证码,于是我就很好奇,后来发现原来是我们学校的电信的那个改密码的页面有漏洞于是就可以通过扫描账号免费上网 原理就是对修改密码的页面进行POST请求 ...
- 2019年微信小程序1月TOP100榜单
- python3文件操作方法
在python3中,我们可以使用open打开一个文件,那么打开文件后,文件有什么操作方法呢?接下来我就记录一下比较常用的方法. 1. close() 关闭打开的文件 2. fileno() 返回文件句 ...
- JavaScript异步编程__“回调地狱”的一些解决方案
异步编程在JavaScript中非常重要.过多的异步编程也带了回调嵌套的问题,本文会提供一些解决“回调地狱”的方法. setTimeout(function () { console.log('延时触 ...