Selenium Webdriver实现截图功能的示例
更新时间:2018年05月21日 09:42:29 作者:Monkey大圣
今天小编就为大家分享一篇Selenium Webdriver实现截图功能的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot)。
废话不多说了,直接上代码
package com.wch;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.sun.jna.platform.FileUtils;
public class TestTakesScreenshot {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //讲截取的图片以文件的形式返回
try {
org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot.png")); //使用copyFile()方法保存获取到的截图文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.quit();
}
}
还有其他方法的话希望各位也能提供下,互相学习。
以上这篇Selenium Webdriver实现截图功能的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
详解mybatis-plus的 mapper.xml 路径配置的坑
这篇文章主要介绍了详解mybatis-plus的 mapper.xml 路径配置的坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08
Mybatis中 mapper-locations和@MapperScan的作用
这篇文章主要介绍了Mybatis中 mapper-locations和@MapperScan的作用,mybatis.mapper-locations在SpringBoot配置文件中使用,作用是扫描Mapper接口对应的XML文件,需要的朋友可以参考下2023-05-05
SpringBoot项目Maven下载依赖速度慢问题的解决方法
在使用Maven构建项目时,有时会遇到下载依赖包速度慢的问题,为了提高下载速度,我们可以将默认的仓库地址替换为国内镜像源,所以本文介绍了SpringBoot项目Maven下载依赖速度慢问题的解决方法,需要的朋友可以参考下2024-08-08


最新评论