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

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. Openjudge-计算概论(A)-DNA排序

    描述: 给出一系列基因序列,由A,C,G,T四种字符组成.对于每一个序列,定义其逆序对如下: 序列中任意一对字符X和Y,若Y在X的右边(不一定相邻)且Y < X,则称X和Y为一个逆序对. 例如G ...

  2. Openjudge-计算概论(A)-计算三角形面积

    描述: 平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少. 输入输入仅一行,包括6个单精度浮点数,分别对应x1, y1 ...

  3. why do we need virtual methods in C++?

    http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c Basic idea: when mark ...

  4. iOS中的项目新特性页面的处理

    一般项目中都会出现新特性页面,比如第一次使用应用的时候,或者在应用设置里查看新特性的时候会出现. 这里,选择新建一个专门处理项目新特性的控制器,来完成功能. 首先是 NewFeaturesViewCo ...

  5. Urbanization

    Urbanization time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. php 分页类(1)

    inter.php <head> <meta http-equiv="Content-Type" content="text/html; charset ...

  7. 基于ZooKeeper的分布式Session实现

    1.   认识ZooKeeper ZooKeeper—— “动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而不是像走在原始丛林里,心惊胆颤的被 ...

  8. win8下使用eclipse进行hadoop2.6.2开发

    最近在win平台下使用eclipse Mars做在远程linux上的hadoop2.6开发,出现很多问题,让人心力交瘁,在经过不懈努力后,终于解决了,让人欢欣雀跃. 1.安装JDK 在做hadoop2 ...

  9. MFC实现登录对话框连接access数据库方式

    编写一个简单的登录对话框 大家好,我们利用MFC编写一个简单的登录对话框.主窗体是单文档界面.程序运行的时候,先弹出一个简单的登录对话框,输入用户名和密码后主窗体显示出来. 1.开打VC++6.0.点 ...

  10. Chapter 1 First Sight——27

    Throughout all this conversation, my eyes flickered again and again to the table where the strange f ...