Selenium截圖方法一:java
Selenium中截圖類TakeScreenshout,這個類主要是獲取瀏覽器窗體內的內容,不包括瀏覽器的菜單和桌面的任務欄區域,咱們用百度首頁來截圖,看看截圖效果。web
FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png"));「屏幕截圖」是咱們本身建立的文件夾用來存放截圖文件,此文件夾在project(工程)的更目錄;chrome
固然也是能夠設置保存到其餘目錄下:Files.copyFile(srcFile, new File("D:\\資料圖片", time + ".png"));瀏覽器
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.google.common.io.Files;eclipse
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",".\\drivers\\chromedriver.exe");測試
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
/**
* 截屏操做
* 圖片已當前時間命名
*/
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //轉換時間格式
String time = dateFormat.format(Calendar.getInstance().getTime()); //獲取當前時間
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //執行屏幕截取
Files.copyFile(srcFile, new File("屏幕截圖", time + ".png")); //利用FileUtils工具類的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截圖"即時保存截圖的文件夾
Thread.sleep(2000);
driver.quit();
}
}ui
Selenium截圖方法二:google
Robot截屏spa
示例代碼:(示例中的圖片是保存再該工程的根目錄下)
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 com.google.common.io.Files;
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",".\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
robotSnapshot();
Thread.sleep(2000);
driver.quit();
}
/**
* 截屏方法2、Robot實現截屏
* @throws Exception
*/
public static void robotSnapshot() throws Exception {
//調用截圖方法
BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(img, "png", new File("robot_screen01.png"));
}
Selenium截圖方法三:
在測試的過程當中,有時候不須要截取整個屏幕,只須要截取某個元素(或者目標區域)的圖片
示例代碼:
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 com.google.common.io.Files;
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()獲取時間戳的方法
Files.copyFile(elementSnapshot(element), new File("屏幕截圖", System.currentTimeMillis()+".png"));
Thread.sleep(2000);
driver.quit();
}
/**
* 部分截圖(元素截圖)
* 有時候須要元素的截圖,不須要整個截圖
* @throws Exception
*/
public static File elementSnapshot(WebElement element) throws Exception {
//建立全屏截圖
File screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(screen);
//獲取元素的高度、寬度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//建立一個矩形使用上面的高度,和寬度
Rectangle rect = new Rectangle(width, height);
//元素座標
Point p = element.getLocation();
BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
ImageIO.write(img, "png", screen);
return screen;
}
}
//進行元素截圖,並返回圖片路徑
public String PartScreenshotFilePath(WebElement element) throws Exception {
//建立時間格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
//建立全屏截圖
File fullScreenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(fullScreenShot);
//獲取元素的高度、寬度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//建立一個矩形使用上面的高度,和寬度
Rectangle rect = new Rectangle(width, height);
//元素座標
Point p = element.getLocation();
BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
File partScreenShot = new File("ScreenShots", simpleDateFormat.format(date)+".png");
ImageIO.write(img, "png", partScreenShot);
//獲取截圖文件路徑
String ScreenshotFilePath = partScreenShot.getPath();
return ScreenshotFilePath;
}
//對兩次的截圖進行對比
public boolean ScreenshotCompare(String oldScreenshotPath, String newScreenshotPath) throws IOException {
//實例化截圖文件
File oldFile = new File(oldScreenshotPath);
File newFile = new File(newScreenshotPath);
//讀取截圖文件
BufferedImage bOldFile = ImageIO.read(oldFile);
BufferedImage bNewFile = ImageIO.read(newFile);
//存儲截圖文件數據
DataBuffer dOldFile = bOldFile.getData().getDataBuffer();
DataBuffer dNewFile = bNewFile.getData().getDataBuffer();
//獲取截圖文件尺寸
int oldFileSize = dOldFile.getSize();
int newFileSize = dNewFile.getSize();
//截圖文件進行對比
boolean matchFlag = true;
if (oldFileSize == newFileSize) {
for (int i = 0; i < oldFileSize; i++) {
if (dOldFile.getElem(i) != dNewFile.getElem(i)) {
matchFlag=false;
break;
}
}
} else {
matchFlag=false;
}
return matchFlag;
}