是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
/**
* Take a screenshot and move to the given folder location.
*
* @param driver
* @param folderLocation
* @return screenShotFilePath
*/
public static String captureScreenshot(WebDriver driver, String folderLocation) {
// Variable to store screenshot's file path.
String screenShotFilePath = null;
// Generate unique id for screen shot name.
String uniqueId = UUID.randomUUID().toString().substring(31);
if (driver != null) {
// Generate screenshot as a file
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// New screenshot file path with having file name
screenShotFilePath = folderLocation + File.separator + uniqueId + ".png";
// Move file to the destination location.
FileUtils.moveFile(scrFile, new File(screenShotFilePath));
}
return screenShotFilePath;
}
其他回答
Java
public void captureScreenShot(String obj) throws IOException {
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("Screenshots\\" + obj + "" + GetTimeStampValue() + ".png"));
}
public String GetTimeStampValue()throws IOException{
Calendar cal = Calendar.getInstance();
Date time = cal.getTime();
String timestamp = time.toString();
System.out.println(timestamp);
String systime = timestamp.replace(":", "-");
System.out.println(systime);
return systime;
}
使用这两种方法,您还可以拍摄带有日期和时间的屏幕截图。
机器人框架
下面是一个使用Robot Framework和Selenium2Library的解决方案:
*** Settings ***
Library Selenium2Library
*** Test Cases ***
Example
Open Browser http://localhost:8080/index.html firefox
Capture Page Screenshot
这将在工作空间中保存一个截图。也可以为关键字Capture Page截图提供一个文件名来改变这种行为。
你可以尝试一下shot API。它在GitHub上。
测试示例。
PowerShell
Set-Location PATH:\to\selenium
Add-Type -Path "Selenium.WebDriverBackedSelenium.dll"
Add-Type -Path "ThoughtWorks.Selenium.Core.dll"
Add-Type -Path "WebDriver.dll"
Add-Type -Path "WebDriver.Support.dll"
$driver = New-Object OpenQA.Selenium.PhantomJS.PhantomJSDriver
$driver.Navigate().GoToUrl("https://www.google.co.uk/")
# Take a screenshot and save it to filename
$filename = Join-Path (Get-Location).Path "01_GoogleLandingPage.png"
$screenshot = $driver.GetScreenshot()
$screenshot.SaveAsFile($filename, [System.Drawing.Imaging.ImageFormat]::Png)
其他司机……
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
$driver = New-Object OpenQA.Selenium.IE.InternetExplorerDriver
$driver = New-Object OpenQA.Selenium.Opera.OperaDriver
public static void getSnapShot(WebDriver driver, String event) {
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage originalImage = ImageIO.read(scrFile);
//int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
jpeg.setAlignment(Image.MIDDLE);
PdfPTable table = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Paragraph("\n"+event+"\n"));
PdfPCell cell2 = new PdfPCell(jpeg, false);
table.addCell(cell1);
table.addCell(cell2);
document.add(table);
document.add(new Phrase("\n\n"));
//document.add(new Phrase("\n\n" + event + "\n\n"));
//document.add(jpeg);
fileWriter.write("<pre> " + event + "</pre><br>");
fileWriter.write("<pre> " + Calendar.getInstance().getTime() + "</pre><br><br>");
fileWriter.write("<img src=\".\\img\\" + index + ".jpg\" height=\"460\" width=\"300\" align=\"middle\"><br><hr><br>");
++index;
}
catch (IOException | DocumentException e) {
e.printStackTrace();
}
}