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

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. genymotion模拟器配置Genymotion-ARM-Translation 兼容包

    前提是你的adb的环境已经配置正确,不知道怎么配置的可参考http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html 如果还不成功的话 ...

  2. tomcat安装完设定用户名和密码

    vi conf/tomcat-user.xml<tomcat-users> <role rolename="manager"/> <role role ...

  3. dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解

    http://blog.sina.com.cn/s/blog_8b5bb24f01016y3o.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指 ...

  4. 获取集合、数组后要判断为空的必要性以及根据构造器创建对象后不是null的证实

    在开发过程中,凡是获取到一个集合对象,在利用或者说遍历这个集合对象之前,都要进行是否为null以及size()>0的判断,但是如果size()>0的话,不是就已经可以说明此集合对象不为nu ...

  5. 查询mysql中经纬度判断坐标范围

    先上代码,稍后附上说明: 1. 从mysql中取出记录,打印有效经纬度: import json import MySQLdb # lines = c.fetchall() #所有的记录,一个tupl ...

  6. HDU--1301--Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  7. Chapter 2 Open Book——16

    By Friday I was perfectly comfortable entering my Biology class, nolonger worried that Edward would ...

  8. thinkphp的model模型的设计经验总结

    关于模型:跟上篇文章thinkphp的目录结构设计经验总结写控制器一个道理:为了尽量避免改动到框架: 首先我们是要有一个BaseModel.class.php作为我们的基础model: 我会在Base ...

  9. Apache Shiro 认证过程

    3.1.1    示例 Shiro验证Subjects 的过程中,可以分解成三个不同的步骤: 1. 收集Subjects 提交的Principals(身份)和Credentials(凭证): 2. 提 ...

  10. APP页面设计