51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1559|回复: 1
打印 上一主题 下一主题

[转贴] selenium webdriver学习 12 – 如何利用selenium-webdriver截图

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-7-18 14:11:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在自动化测试中常常会用到截图功能。最近用了一下selenium-webdriver的截图功能还算不错,可以截取页面全图,不管页面有多长。
下面的代码演示了如何使用webdriver进行截图:
CODE:
  1. <font size="4">import java.io.File;
  2. import java.io.IOException;
  3.   
  4. import org.apache.commons.io.FileUtils;
  5. import org.openqa.selenium.OutputType;
  6. import org.openqa.selenium.TakesScreenshot;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.firefox.FirefoxDriver;
  9. public class ShotScreen {
  10.   
  11.     /**
  12.      * @author gongjf
  13.      * @throws IOException
  14.      * @throws InterruptedException
  15.      */
  16.     publicstaticvoid main(String[] args)throws IOException, InterruptedException {
  17.          
  18.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  19.         WebDriver dr = new FirefoxDriver();
  20.         dr.get("http://www.51.com");
  21.                   
  22.         //这里等待页面加载完成
  23.         Thread.sleep(5000);
  24.         //下面代码是得到截图并保存在D盘下
  25.         File screenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
  26.         FileUtils.copyFile(screenShotFile,new File("D:/test.png"));}}</font>
复制代码
看了一下OutputType接口和TakesScreenshot接口,吐槽一下,貌似这两个接口不是同一个开发写的或者注释没有更新怎么的。在OutputType里面的注释说:
CODE:
  1. <font size="4">/**
  2. * Defines the output type for a screenshot. See org.openqa.selenium.Screenshot for usage and
  3. * examples.
  4. ...</font>
复制代码
然后在那找了半天的org.openqa.selenium.Screenshot 接口,晕,后来想应该是org.openqa.selenium.TakesScreenshot。
在TakesScreenshot里有如下注释:
CODE:
  1. <font size="4">/**
  2. * Capture the screenshot and store it in the specified location.
  3. *
  4. * <p>For WebDriver extending TakesScreenshot, this makes a best effort
  5. * depending on the browser to return the following in order of preference:
  6. * <ul>
  7. *   <li>Entire page</li>
  8. *   <li>Current window</li>
  9. *   <li>Visible portion of the current frame</li>
  10. *   <li>The screenshot of the entire display containing the browser</li>
  11. * </ul>
  12. *
  13. * <p><span style="color: rgb(255, 0, 0);">For WebElement extending TakesScreenshot, this makes a best effort
  14. * depending on the browser to return the following in order of preference:
  15. *   - The entire content of the HTML element
  16. *   - The visisble portion of the HTML element</span>
  17. *
  18. * @param <X> Return type for getScreenshotAs.
  19. * @param target target type, @see OutputType
  20. * @return Object in which is stored information about the screenshot.
  21. * @throws WebDriverException on failure.
  22. */</font>
复制代码
试了一下截取Webelement最终发现WebElement接口没有实现这个类。搞了半天也只是会了截取页面的全图。截取当前的frame也截取的页面全图。难道这个功能没有完善,好吧,这样说自我安慰一下。
selenium-webdriver 面向接口编程,找一个需要的功能还真是挺难的。

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-13 04:33 , Processed in 0.060254 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表