成都创新互联网站制作重庆分公司

SeleniumWebdriver实现截图功能的示例-创新互联

前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot)。

创新互联-专业网站定制、快速模板网站建设、高性价比瑞丽网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式瑞丽网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖瑞丽地区。费用合理售后完善,10年实体公司更值得信赖。

废话不多说了,直接上代码

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();
	}
}

文章题目:SeleniumWebdriver实现截图功能的示例-创新互联
本文URL:http://cxhlcq.com/article/pcpgd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部