是否可以使用Selenium WebDriver进行截图?

(注:不含硒遥控器)


当前回答

你可以使用Webdriverclass对象创建一个webdriver支持的selenium对象,然后你可以截屏。

其他回答

Java

public String captureScreen() {
    String path;
    try {
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
        path = "./target/screenshots/" + source.getName();
        FileUtils.copyFile(source, new File(path)); 
    }
    catch(IOException e) {
        path = "Failed to capture screenshot: " + e.getMessage();
    }
    return path;
}

Java

String yourfilepath = "E:\\username\\Selenium_Workspace\\foldername";

// Take a snapshort
File snapshort_file = ((TakesScreenshot) mWebDriver)
        .getScreenshotAs(OutputType.FILE);
// Copy the file into folder

FileUtils.copyFile(snapshort_file, new File(yourfilepath));

通过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:

PHP

public function takescreenshot($event)
  {
    $errorFolder = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "ErrorScreenshot";

    if(!file_exists($errorFolder)){
      mkdir($errorFolder);
    }

    if (4 === $event->getResult()) {
      $driver = $this->getSession()->getDriver();
      $screenshot = $driver->getWebDriverSession()->screenshot();
      file_put_contents($errorFolder . DIRECTORY_SEPARATOR . 'Error_' .  time() . '.png', base64_decode($screenshot));
    }
  }

硒化/ Java

以下是Selenide项目是如何做到这一点的,这比任何其他方式都要简单:

import static com.codeborne.selenide.Selenide.screenshot;    
screenshot("my_file_name");

JUnit:

@Rule
public ScreenShooter makeScreenshotOnFailure = 
     ScreenShooter.failedTests().succeededTests();

美国小妞TestNG:

import com.codeborne.selenide.testng.ScreenShooter;
@Listeners({ ScreenShooter.class})