Java selenium截圖操作的實(shí)現(xiàn)
方法一:Selenium中截圖類TakeScreenshout,這個(gè)類主要是獲取瀏覽器窗體內(nèi)的內(nèi)容,不包括瀏覽器的菜單和桌面的任務(wù)欄區(qū)域,我們用百度首頁來截圖,看看截圖效果。
FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png"));“屏幕截圖”是我們自己創(chuàng)建的文件夾用來存放截圖文件,此文件夾在project(工程)的更目錄
;
當(dāng)然也是可以設(shè)置保存到其他目錄下:FileUtils.copyFile(srcFile, new File("D:\\資料圖片", time + ".png"));
示例代碼如下:
package com.sandy; import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; 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.chrome.ChromeDriver; public class ScreenShot { private static WebDriver driver; public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); /** * 截屏操作 * 圖片已當(dāng)前時(shí)間命名 */ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //轉(zhuǎn)換時(shí)間格式 String time = dateFormat.format(Calendar.getInstance().getTime()); //獲取當(dāng)前時(shí)間 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //執(zhí)行屏幕截取 FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png")); //利用FileUtils工具類的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截圖"即時(shí)保存截圖的文件夾 Thread.sleep(2000); driver.quit(); } }
方法二:Robot截屏
示例代碼:(示例中的圖片是保存再該工程的根目錄下)
package com.sandy; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Point; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.internal.WrapsDriver; public class ScreenShot { private static WebDriver driver; public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); robotSnapshot(); Thread.sleep(2000); driver.quit(); } /** * 截屏方法二、Robot實(shí)現(xiàn)截屏 * @throws Exception */ public static void robotSnapshot() throws Exception { //調(diào)用截圖方法 BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(img, "png", new File("robot_screen01.png")); }
方法三:在測(cè)試的過程中,有時(shí)候不需要截取整個(gè)屏幕,只需要截取某個(gè)元素(或者目標(biāo)區(qū)域)的圖片
示例代碼:
package com.sandy; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Point; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.internal.WrapsDriver; public class ScreenShot { private static WebDriver driver; public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.id("su")); elementSnapshot(element); //System.currentTimeMillis()、Calendar.getInstance().getTimeInMillis()獲取時(shí)間戳的方法 FileUtils.copyFile(elementSnapshot(element), new File("屏幕截圖", System.currentTimeMillis()+".png")); Thread.sleep(2000); driver.quit(); } /** * 部分截圖(元素截圖) * 有時(shí)候需要元素的截圖,不需要整個(gè)截圖 * @throws Exception */ public static File elementSnapshot(WebElement element) throws Exception { //創(chuàng)建全屏截圖 WrapsDriver wrapsDriver = (WrapsDriver)element; File screen = ((TakesScreenshot)wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE); BufferedImage image = ImageIO.read(screen); //獲取元素的高度、寬度 int width = element.getSize().getWidth(); int height = element.getSize().getHeight(); //創(chuàng)建一個(gè)矩形使用上面的高度,和寬度 Rectangle rect = new Rectangle(width, height); //元素坐標(biāo) Point p = element.getLocation(); BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height); ImageIO.write(img, "png", screen); return screen; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:沒有了
欄 目:Java編程
下一篇:沒有了
本文標(biāo)題:Java selenium截圖操作的實(shí)現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/8322.html
您可能感興趣的文章
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒有被捕獲的異常
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)器
- 01-10Java中的浮點(diǎn)數(shù)分析
- 01-10面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒有被捕獲的異常
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery