`

selenium webdriver学习(十二)------------如何利用selenium-webdriver截图

 
阅读更多

在自动化测试中常常会用到截图功能。最近用了一下selenium-webdriver的截图功能还算不错,可以截取页面全图,不管页面有多长。

 

下面的代码演示了如何使用webdriver进行截图:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ShotScreen {

	/**
	 * @author gongjf
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		
		System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
		WebDriver dr = new FirefoxDriver();
		dr.get("http://www.51.com");
				
		//这里等待页面加载完成
		Thread.sleep(5000);
		//下面代码是得到截图并保存在D盘下
		File screenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
		FileUtils.copyFile(screenShotFile, new File("D:/test.png"));}}

 

 看了一下OutputType接口和TakesScreenshot接口,吐槽一下,貌似这两个接口不是同一个开发写的或者注释没有更新怎么的。在OutputType里面的注释说:

/**
 * Defines the output type for a screenshot. See org.openqa.selenium.Screenshot for usage and
 * examples.
...

 

然后在那找了半天的org.openqa.selenium.Screenshot 接口,晕,后来想应该是org.openqa.selenium.TakesScreenshot。

在TakesScreenshot里有如下注释:

  /**
   * Capture the screenshot and store it in the specified location.
   *
   * <p>For WebDriver extending TakesScreenshot, this makes a best effort
   * depending on the browser to return the following in order of preference:
   * <ul>
   *   <li>Entire page</li>
   *   <li>Current window</li>
   *   <li>Visible portion of the current frame</li>
   *   <li>The screenshot of the entire display containing the browser</li>
   * </ul>
   *
   * <p>For WebElement extending TakesScreenshot, this makes a best effort
   * depending on the browser to return the following in order of preference:
   *   - The entire content of the HTML element
   *   - The visisble portion of the HTML element
   *
   * @param <X> Return type for getScreenshotAs.
   * @param target target type, @see OutputType
   * @return Object in which is stored information about the screenshot.
   * @throws WebDriverException on failure.
   */

 试了一下截取Webelement最终发现WebElement接口没有实现这个类。搞了半天也只是会了截取页面的全图。截取当前的frame也截取的页面全图。难道这个功能没有完善,好吧,这样说自我安慰一下。

selenium-webdriver 面向接口编程,找一个需要的功能还真是挺难的。

分享到:
评论
5 楼 qincaihong 2012-08-13  
qincaihong 写道
    FileUtils.copyFile(screenShotFile, new File("D:/test.png"));}} 
请教一下在我的电脑上上面这句话一直报错(unhandled exception type IOException),请问是什么原因呢?

问题已经解决:抛出异常即可!
4 楼 qincaihong 2012-08-11  
    FileUtils.copyFile(screenShotFile, new File("D:/test.png"));}} 
请教一下在我的电脑上上面这句话一直报错(unhandled exception type IOException),请问是什么原因呢?
3 楼 shine22fmf 2012-07-13  
运行竟然报错了

Exception in thread "main" java.io.IOException: 设备未就绪。
at java.io.WinNTFileSystem.canonicalize0(Native Method)
at java.io.Win32FileSystem.canonicalize(Unknown Source)
at java.io.File.getCanonicalPath(Unknown Source)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:772)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:731)
at com.example.tests.ShotScreen.main(ShotScreen.java:28)
设备未就绪,是不是要设置什么呢?老师
2 楼 qi_ling2005 2012-07-05  
shine22fmf 写道
可以加个QQ,仔细的询问下么,老师,我停留在用html录制回放脚本的状态,但是转化成java,在eclipse里面我就一点辙都没有了,老师你这边的代码我是都输入到eclipse里面运行了都是可以的,就是怎么把webdriver跟selenium IDE结合起来啊,非常感谢,2466694171,我的QQ


IDE:options -> Format ->...(选自己的编程语言)。
可以直接加这个Q群:128972436,群里人多。
1 楼 shine22fmf 2012-07-04  
可以加个QQ,仔细的询问下么,老师,我停留在用html录制回放脚本的状态,但是转化成java,在eclipse里面我就一点辙都没有了,老师你这边的代码我是都输入到eclipse里面运行了都是可以的,就是怎么把webdriver跟selenium IDE结合起来啊,非常感谢,2466694171,我的QQ

相关推荐

    ruby+selenium-webdriver测试--第一个例子源代码

    Ruby+Selenium-Webdriver是一个强大的自动化测试工具组合,用于模拟真实用户在浏览器...通过实践和理解这些基本概念,你将能够利用Ruby+Selenium-Webdriver构建起自己的自动化测试框架,有效地测试Web应用的各个功能。

    Selenium WebDriver Practical Guide-Code

    通过深入研究这个压缩包中的代码示例,学习者不仅可以理解Selenium WebDriver的工作原理,还能掌握如何在实际项目中应用这些知识,提升自动化测试的技能。同时,这也有助于理解和调试测试脚本,解决在自动化测试过程...

    selenium webdriver 学习

    1.1 selenium webdriver学习(一)------------快速开始 . . . . . . . . . ....1.2 selenium webdriver...1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . . . . . . .38

    Selenium.WebDriver.4.0.0-alpha02.nupkg

    Selenium.WebDriver.4.0.0-alpha02.nupkg,Selenium.WebDriver 4.0,Selenium is a set of different software tools each with a different approach to supporting browser automation. These tools are highly ...

    ruby+selenium-webdriver测试--多个测试用例源代码

    在Ruby编程语言中,Selenium-Webdriver是一个强大的工具,用于进行自动化Web浏览器测试。它允许开发者编写脚本来模拟用户与网页的交互,从而验证应用的功能和行为。本篇文章将深入探讨如何使用Ruby和Selenium-...

    selenium-webdriver介绍PPT

    selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创

    python +selenium webdriver 学习借鉴

    Python + Selenium WebDriver 学习借鉴 Python 是一种广泛使用的编程语言,Selenium 是一个自动化测试工具,WebDriver 是 Selenium 的一个组件,用于自动化浏览器操作。在本文档中,我们将学习如何使用 Python 和 ...

    selenium-webdriver-2.24.0.gem

    安装selenium webdriver 安装ruby1.8.7或1.9.2。注意selenium-webdriver只支持1.8.7以上的ruby版本; 使用gem安装selenium-webdriver;打开命令行,输入下列代码完成安装。注意,如果你的开发环境需要http proxy...

    Python库 | webdriver_manager-1.5.1-py2.py3-none-any.whl

    它是一个便捷的解决方案,用于自动管理和更新Selenium WebDriver。这个压缩包文件`webdriver_manager-1.5.1-py2.py3-none-any.whl`包含了适用于Python 2和3版本的库,无需用户手动下载或配置WebDriver的特定版本。 ...

    ruby2.0.0+selenium-webdriver+watir-webdriver+rails离线备份

    ruby2.0.0+selenium-webdriver+watir-webdriver+rails离线备份,设置环境变量后可使用(未测试)

    Selenium WebDriver实战宝典(吴晓华)

    本书是一本从入门到精通模式的Selenium WebDriver实战经验分享书籍。全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用...

    selenium-webdriver-2.5.0

    selenium-webdriver-2.5.0.gem

    selenium WebDriver原理介绍

    Selenium WebDriver 是一款广泛使用的自动化测试工具,专为Web应用程序设计。...了解其工作原理对于优化自动化测试脚本和...掌握这些原理,可以帮助我们更好地利用Selenium WebDriver 进行测试开发,提升测试质量和效率。

    selenium webdriver+chrome插件.zip

    【标题】"selenium webdriver+chrome插件.zip" 涉及的核心知识点是Selenium WebDriver,特别是它在Chrome浏览器中的应用以及与Firefox的交互。这个压缩包包含了Selenium IDE的Chrome插件,以及对应的WebDriver驱动...

    selenium-webdriver-2.45.0.gem

    selenium-webdriver-2.45.0.gem

    selenium-webdriver-api文档

    **Selenium WebDriver API...综上所述,`selenium-API-2.12.chm`文档是学习和掌握Selenium WebDriver API的重要资源,它涵盖了从基本操作到高级特性的全面内容,对于任何进行Web自动化测试的人来说都是不可或缺的参考。

    Selenium WebDriver 所需jar包

    Selenium WebDriver是一款强大的自动化测试工具,它允许程序员模拟真实用户在浏览器上的操作,进行Web应用程序的功能测试和验收测试。在Java环境下,Selenium WebDriver通常需要引入相应的jar包才能正常工作。...

Global site tag (gtag.js) - Google Analytics