gpt4 book ai didi

exception-handling - Selenium 2 和 JUnit4 : How to capture screenshot on exception?

转载 作者:行者123 更新时间:2023-12-03 23:46:41 24 4
gpt4 key购买 nike

我只想在意外异常时捕获屏幕截图。

最佳答案

注意。-此答案可能已过时。答案基于 Selenium 2.15

使用 TestWatcher有没有技巧(单元测试必须在 BaseTest 之后扩展):

public abstract class BaseTest {
// ...
protected WebDriver driver;


@Rule
public TestRule testWatcher = new TestWatcher() {
@Override
public void starting(Description desc) {
LOG.info("Launching browser...");
driver = Utils.getFirefoxDriver();
}

@Override
public void finished(Description desc) {
LOG.info("Quitting driver...");
driver.quit();
}

@Override
public void failed(Throwable e, Description d) {
LOG.debug("Creating screenshot...");
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(
OutputType.FILE);
String scrFilename = "Screenshot.png";
File outputFile = new File(SCREEN_SHOTS_RESULTS_PATH, scrFilename);
LOG.info(scrFilename + " screenshot created.");
try {
org.​apache.​commons.​io.FileUtils.copyFile(scrFile, outputFile);
} catch (IOException ioe) {
LOG.error("Error copying screenshot after exception.", ioe);
}
}
};
}

注意
Utils.getFirefoxDriver()返回定制的 WebDriver .类似的东西:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Utils {
// ...
public static WebDriver getFirefoxDriver() {
FirefoxProfile firefoxProfile = new FirefoxProfile();

// Profile customization. For example:
// firefoxProfile.addExtension("firebug-1.8.4-fx.xpi");
// firefoxProfile.setPreference("extensions.firebug.currentVersion","1.8.4");

FirefoxBinary firefox = new FirefoxBinary();

// Firefox customization. For example:
// firefox.setEnvironmentProperty("DISPLAY", display);

WebDriver driver = new FirefoxDriver(firefox, firefoxProfile);

// WebDriver customizations. For example:
// driver.manage().timeouts().implicitlyWait(SHORT_TIMEOUT_S, TimeUnit.SECONDS);
return driver;
}
}

关于exception-handling - Selenium 2 和 JUnit4 : How to capture screenshot on exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6520974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com