是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
通过Selenium的Java和Python客户端,有多种方法可以使用Selenium WebDriver进行截图。
Java方法
下面是不同的Java截图方法:
Using getScreenshotAs() from the TakesScreenshot interface: Code block: package screenShot; import java.io.File; import java.io.IOException; 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.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Firefox_takesScreenshot { public static void main(String[] args) throws IOException { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://login.bws.birst.com/login.html/"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Birst")); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Mads_Cruz_screenshot.png")); driver.quit(); } } Screenshot: If the webpage is jQuery enabled, you can use ashot from the pazone/ashot library: Code block: package screenShot; import java.io.File; import javax.imageio.ImageIO; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; public class ashot_CompletePage_Firefox { public static void main(String[] args) throws Exception { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://jquery.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery")); Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver); ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/firefoxScreenshot.png")); driver.quit(); } } Screenshot: Using selenium-shutterbug from assertthat/selenium-shutterbug library: Code block: package screenShot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import com.assertthat.selenium_shutterbug.core.Shutterbug; import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy; public class selenium_shutterbug_fullpage_firefox { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.google.co.in"); Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save("./Screenshots/"); driver.quit(); } } Screenshot:
Python方法
以下是Python中截图的不同方法:
Using save_screenshot() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") driver.save_screenshot('./Screenshots/save_screenshot_method.png') driver.quit() Screenshot: Using the get_screenshot_as_file() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") driver.get_screenshot_as_file('./Screenshots/get_screenshot_as_file_method.png') driver.quit() Screenshot: Using get_screenshot_as_png() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") screenPnG = driver.get_screenshot_as_png() # Crop it back to the window size (it may be taller) box = (0, 0, 1366, 728) im = Image.open(BytesIO(screenPnG)) region = im.crop(box) region.save('./Screenshots/get_screenshot_as_png_method.png', 'PNG', optimize=True, quality=95) driver.quit() Screenshot:
其他回答
Java
在附加TestName和Timestamp的情况下为Selenium中的失败捕获屏幕截图的方法。
public class Screenshot{
final static String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static String imgname = null;
/*
* Method to Capture Screenshot for the failures in Selenium with TestName and Timestamp appended.
*/
public static void getSnapShot(WebDriver wb, String testcaseName) throws Exception {
try {
String imgpath = System.getProperty("user.dir").concat("\\Screenshot\\"+testcaseName);
File f = new File(imgpath);
if(!f.exists()) {
f.mkdir();
}
Date d = new Date();
SimpleDateFormat sd = new SimpleDateFormat("dd_MM_yy_HH_mm_ss_a");
String timestamp = sd.format(d);
imgname = imgpath + "\\" + timestamp + ".png";
// Snapshot code
TakesScreenshot snpobj = ((TakesScreenshot)wb);
File srcfile = snpobj.getScreenshotAs(OutputType.FILE);
File destFile = new File(imgname);
FileUtils.copyFile(srcfile, destFile);
}
catch(Exception e) {
e.getMessage();
}
}
C#
using System;
using OpenQA.Selenium.PhantomJS;
using System.Drawing.Imaging;
namespace example.com
{
class Program
{
public static PhantomJSDriver driver;
public static void Main(string[] args)
{
driver = new PhantomJSDriver();
driver.Manage().Window.Size = new System.Drawing.Size(1280, 1024);
driver.Navigate().GoToUrl("http://www.example.com/");
driver.GetScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);
driver.Quit();
}
}
}
它需要NuGet包:
PhantomJS 2.0.0 硒。支持2.48.2 硒。WebDriver 2.48.2
它是用。net Framework v4.5.2测试的。
C#
public Bitmap TakeScreenshot(By by) {
// 1. Make screenshot of all screen
var screenshotDriver = _selenium as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
IWebElement element = FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
return bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
}
你可以尝试一下shot API。它在GitHub上。
测试示例。
PHP (PHPUnit)
它使用PHPUnit_Selenium扩展版本1.2.7:
class MyTestClass extends PHPUnit_Extensions_Selenium2TestCase {
...
public function screenshot($filepath) {
$filedata = $this->currentScreenshot();
file_put_contents($filepath, $filedata);
}
public function testSomething() {
$this->screenshot('/path/to/screenshot.png');
}
...
}