selenium webdriver学习(十二)------------如何利用selenium-webdriver截图
2013-05-30 16:06
387 查看
在自动化测试中常常会用到截图功能。最近用了一下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
*/
publicstaticvoid 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><span style="color: rgb(255, 0, 0);">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</span>
*
* @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 面向接口编程,找一个需要的功能还真是挺难的。
下面的代码演示了如何使用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
*/
publicstaticvoid 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"));}}
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.
...
/** * 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><span style="color: rgb(255, 0, 0);">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</span>
*
* @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.
*/
/** * 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 面向接口编程,找一个需要的功能还真是挺难的。
相关文章推荐
- selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
- selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
- selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
- selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)
- selenium webdriver学习(十四)------------如何处理table
- selenium webdriver学习(十五)------------如何处理FirefoxProfile
- 【Selenium学习笔记】webdriver如何定位一组对象
- selenium webdriver学习(十五)------------如何处理FirefoxProfile
- selenium webdriver学习(八)------------如何操作select下拉框(转)
- selenium webdriver学习:如何操作select下拉框
- selenium webdriver学习(八)------------如何操作select下拉框
- selenium webdriver学习(九)------------如何操作cookies(转)
- selenium webdriver学习--利用POI实现数据驱动
- Selenium:如何利用webdriver uncheck checkbox
- selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框
- selenium webdriver学习(九)------------如何操作cookie
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
- selenium webdriver学习(六)------------如何得到弹出窗口
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面
- selenium webdriver学习(一)---如何处理alert、confirm、prompt对话框