例: 打开百度首页 ,进行截图

01 packagecom.example.tests; 
02 importjava.io.File; 
03 importorg.apache.commons.io.FileUtils; 
04 importorg.junit.*; 
05 importorg.openqa.selenium.*; 
06 importorg.openqa.selenium.ie.InternetExplorerDriver; 
07 public classSelenium2 { 
08     @Test 
09     public voidtestTakesScreenshot() { 
10         WebDriver driver = newInternetExplorerDriver(); 
11         driver.get("http://www.baidu.com"); 
12         try
13             File srcFile = ((TakesScreenshot)driver). 
14                     getScreenshotAs(OutputType.FILE); 
15             FileUtils.copyFile 
16             (srcFile,newFile("d:\\screenshot.png")); 
17         catch(Exception e) { 
18             e.printStackTrace(); 
19         }  
20           driver.close(); 
21         
22 }

TakesScreenshot接口提供了getScreenshotAs()方法来捕捉屏幕。上面的例子中,我们指定了OutputType.FILE作为参数传递给getScreenshoAs()方法,告诉它将截取的屏幕以文件形式返回。

如果使用的是RemoteWebDriver() ,则方法应该如下

首先启动selenium java -jar selenium-server-standalone-2.25.0.jar

01 packagecom.example.tests; 
02 importjava.io.File; 
03 importjava.io.IOException; 
04 importjava.net.MalformedURLException; 
05 importjava.net.URL; 
06 importorg.apache.commons.io.FileUtils; 
07 importorg.junit.*; 
08 importorg.openqa.selenium.*; 
09 importorg.openqa.selenium.remote.*; 
10 public classSelenium2 { 
11     @Test 
12     public voidtestRemoteWebDriverScreenShot() { 
13         //指定使用的浏览器 
14         DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); 
15         WebDriver driver = null
16         try
17             driver = newRemoteWebDriver( //我使用localhost来测试 
18                     newURL("http://localhost:4444/wd/hub"), capability); 
19         catch(MalformedURLException e) { 
20             e.printStackTrace(); 
21         
22         driver.get("http://www.sina.com.cn"); 
23         //对远程系统进行截图 
24         driver = newAugmenter().augment(driver);  
25         File scrFile =   
26           ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
27         try
28             FileUtils.copyFile(scrFile, newFile("D:\\screenshot.png")); 
29         catch(IOException e) { 
30             e.printStackTrace(); 
31         
32     
33 }

转:WebDriver进行屏幕截图的更多相关文章

  1. JavaBeginnersTutorial 中文系列教程·翻译完成

    原文:JavaBeginnersTutorial 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 学习资源 目录 ...

  2. Selenium WebDriver屏幕截图(C#版)

    Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...

  3. webdriver高级应用- 测试过程中发生异常或断言失败时进行屏幕截图

    封装了三个类来实现这个功能: 1.DataUtil.py 用于获取当前的日期以及时间,用于生成保存截图文件的目录名,代码如下: #encoding=utf-8 import time from dat ...

  4. webdriver屏幕截图(python)

    webdriver对当前页面进行截图,截取的是当前页面的全图,不论页面有多长,有两种截图方法 1.get_screenshot_as_file(XXX) 2.save_screenshot(XXX) ...

  5. 5.6 WebDriver API实例讲解(31-35)

    31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...

  6. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  7. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  8. Python实现屏幕截图的两种方式

    Python实现屏幕截图的两种方式 使用windows API 使用PIL中的ImageGrab模块 下面对两者的特点和用法进行详细解释. 一.Python调用windows API实现屏幕截图 好处 ...

  9. Python+Selenium笔记(十六)屏幕截图

    (一) 方法 方法 简单说明 save_screenshot(filename)   获取当前屏幕截图并保存为指定文件 filename:路径/文件名 get_screenshot_as_base64 ...

随机推荐

  1. mysql灵活分页存储过程

    ), -- 表名 ), -- 排序字段(必须!支持多字段不用加order by) IN _PageIndex int, -- 指定当前为第几页 IN _PageSize int, -- 每页多少条记录 ...

  2. 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

    //接口Compute package jieKou; public interface Compute { int Computer(int n,int m); } //加 package jieK ...

  3. php 好用的函数

    extract — 从数组中将变量导入到当前的符号表,数组的键将作为新的变量,数组的值将最为新变量的值

  4. wc的用法

    wc -c filename:显示一个文件的字节数 wc -m filename:显示一个文件的字符数 wc -l filename:显示一个文件的行数 wc -L filename:显示一个文件中的 ...

  5. Microsoft SqlSever 数据库--软谋1

    百度百科--Microsoft SqlSever SQL是英文Structured Query Language的缩写,意思为结构化查询语言.SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按 ...

  6. mysql出现Can't connect to MySQL server on 'localhost' (10061)的解决方法

    网上搜索的一: 今天把mysql数据库拷贝到另外一台机上,结果连不上,报“Can't connect to MySQL server on 'localhost' (10061)“错误 到网上sear ...

  7. Chapter 1 First Sight——37

    "Never mind, then," he said hastily in a voice like velvet. 别介意,他用天鹅绒般的声音急切的说道 "I can ...

  8. 让AutoMapper在你的项目里飞一会儿(转)

    出处:http://www.cnblogs.com/WeiGe/p/3835523.html 先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说 ...

  9. Entity Framework技巧系列之八 - Tip 29 – 34

    提示29. 怎样避免延迟加载或Load()阅读器问题 如果你有如下这样的代码: 1 var results = from c in ctx.Customers 2 where c.SalesPerso ...

  10. WisKey的眼神

    WisKey的眼神 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...