是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
硒化/ 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})
其他回答
C#
public static void TakeScreenshot(IWebDriver driver, String filename)
{
// Take a screenshot and save it to filename
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(filename, ImageFormat.Png);
}
Jython
import org.openqa.selenium.OutputType as OutputType
import org.apache.commons.io.FileUtils as FileUtils
import java.io.File as File
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver
self.driver = FirefoxDriver()
tempfile = self.driver.getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(tempfile, File("C:\\screenshot.png"))
Java
我想我应该给出完整的解决方案,因为有两种不同的方法获得截图。一个来自本地浏览器,另一个来自远程浏览器。我甚至将图像嵌入到HTML报告中:
@After()
public void selenium_after_step(Scenario scenario) throws IOException, JSONException {
if (scenario.isFailed()){
scenario.write("Current URL = " + driver.getCurrentUrl() + "\n");
try{
driver.manage().window().maximize(); // Maximize window to get full screen for chrome
}
catch (org.openqa.selenium.WebDriverException e){
System.out.println(e.getMessage());
}
try {
if(isAlertPresent()){
Alert alert = getAlertIfPresent();
alert.accept();
}
byte[] screenshot;
if(false /*Remote Driver flow*/) { // Get a screenshot from the remote driver
Augmenter augmenter = new Augmenter();
TakesScreenshot ts = (TakesScreenshot) augmenter.augment(driver);
screenshot = ts.getScreenshotAs(OutputType.BYTES);
}
else { // Get a screenshot from the local driver
// Local webdriver user flow
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
scenario.embed(screenshot, "image/png"); // Embed the image in reports
}
catch (WebDriverException wde) {
System.err.println(wde.getMessage());
}
catch (ClassCastException cce) {
cce.printStackTrace();
}
}
//seleniumCleanup();
}
Java
使用RemoteWebDriver,在增加了节点的截图功能后,我将像这样存储截图:
void takeScreenShotMethod(){
try{
Thread.sleep(10000);
long id = Thread.currentThread().getId();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
+ id + "/screenshot.jpg"));
}
catch( Exception e ) {
e.printStackTrace();
}
}
你可以在任何需要的地方使用这种方法。然后,我假设您可以在surefire-reports/html/custom.css中定制maven-surefire-report-plugin的样式表,以便您的报告包含到每个测试的正确截图的链接。
Java
我无法得到可接受的答案,但根据当前的WebDriver文档,以下内容对我来说在OS X v10.9 (Mavericks)上的Java 7工作得很好:
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest() throws Exception {
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriver does not implement the TakesScreenshot class
// if the driver does have the Capabilities to take a screenshot
// then Augmenter will add the TakesScreenshot methods to the instance
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}