前幾天在研究中自動化的時候突發奇想,想著能不能來截個圖,以便之后查看,實現的方法其實也不難,畢竟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實現截圖功能的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選