截图方法

 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——实现截图功能的更多相关文章

  1. Selenium webdriver实现截图功能

    可参考http://www.cnblogs.com/tobecrazy/p/3599568.html Webdriver截图时,需要引入: import java.io.File; import ja ...

  2. selenium提供的截图功能

    get_screenshot_as_file()提供一个截屏功能.在自动化执行过程中,执行失败后只能看到代码的运行错误,而不能直接看到ui上的错误,利用截屏保存下来很容易的进行问题的判断 先来执行一个 ...

  3. Selenium Webdriver 自动化测试开发常见问题(C#版)

    转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver ...

  4. Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能

    之前的博文 020-JavaScript 在 Selenium 自动化中的应用实例之二(获取浏览器显示区域大小) 简述了通过 JavaScript 获取浏览器显示区域大小,此文将简述另一种获取浏览器显 ...

  5. selenium + python实现截图并且保存图片

    webdriver的截图功能十分强悍,无论页面多长,webdriver都能比较完美的截到完整的页面. python代码: # -*- coding: utf-8 -*-from selenium im ...

  6. 封装selenium自动化框架中的截图功能

    对selenium自带的截图功能进行封装: 以下为封装的代码,自定义一个.py文件即可,图片路径自己设置一个. #coding:utf-8 class Screen(object): ''' 封装的截 ...

  7. selenium截图功能

    selenium自动化测试完后需要查看值观的结果,或者查操作过程中是否正确,此时需要使用自带的截图功能. 示例1: from time import sleep from selenium impor ...

  8. selenium web driver 实现截图功能

    在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...

  9. Selenium webdriver 截图 太长截不全的问题

    Selenium webdriver 截图 太长截不全的问题 1.环境 selenium webdriver.net 2.46.0.0 + firefox 37.0.1 + win 8.1 2.问题 ...

随机推荐

  1. Mybatis四种分页方式

    数组分页 查询出全部数据,然后再list中截取需要的部分. mybatis接口 List<Student> queryStudentsByArray(); xml配置文件 <sele ...

  2. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

  3. POJ 1961 循环节

    和POJ 2406 几乎一样.前者是求 该字符串的最小的循环节.也就是最大的循环次数.后者是求该字符串的每个前缀的循环节的最大循环次数.(如果有的话).而且必须大于1.才可以输出.就是POJ 2406 ...

  4. Nginx实践03-配置虚拟主机的3种方式

    基于IP.端口号.名称3种方式 1.基于IP的虚拟主机配置(使用最少) 基于ip的虚拟主机配置,需要配置单个网卡上多个ip地址,这种方式管理比较麻烦,所以用的很少. 1.1 设置单个网卡多个IP 查看 ...

  5. java中break,continue,标签实现goto效果(编程思想)

    goto 编程语言中一开始就有goto关键词了.事实上,goto起源于汇编语言的程序控制:“若条件A成立,则调到这里:否则跳到那里”. goto语句时在源码级别上的跳转,这导致了其不好的名誉.于是go ...

  6. MySQL数据引擎InnoDB和MyISAM互相转换

    MySQL(或者社区开源fork的MariaDB)5.5以上支持InnoDB引擎,并将其作为默认数据库引擎.InnoDB带来很多改进,但是对系统资源占用明显增加,对于还在128MB-512MB内存VP ...

  7. PHP迭代器的小坑

    使用PHP迭代器的时候,需要主要到很多迭代器是对内部迭代器的封装,当外部迭代器移动的时候,实际上也是在移动内部迭代器. 示例一:命令行 &"C:\wamp64\bin\php\php ...

  8. PyalgoTrade 交易(五)

    我们继续采取简单的策略,这次模拟实际交易.这个想法很简单: 如果调整后的收盘价高于SMA(15),我们将进入多头仓位(我们下单买入市价). 如果调整后的收盘价低于SMA(15),我们退出多头头寸(我们 ...

  9. WPF 的 ElementName 在 ContextMenu 中无法绑定成功?试试使用 x:Reference!

    在 Binding 中使用 ElementName 司空见惯,没见它出过什么事儿.不过当你预见 ContextMenu,或者类似 Grid.Row / Grid.Column 这样的属性中设置的时候, ...

  10. 基于http协议的加密传输方案

    最近公司需要通过公网与其它平台完成接口对接,但是基于开发时间和其它因素的考虑,本次对接无法采用https协议实现.既然不能用https协议,那就退而求其次采用http协议吧! 那么问题来了!在对接的过 ...