Selenium 2和JUnit4:如何捕获截图的异常? [英] Selenium 2 and JUnit4: How to capture screenshot on exception?
本文介绍了Selenium 2和JUnit4:如何捕获截图的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
注意:这个答案可能已经过时了。答案是基于Selenium 2.15
使用 TestWatcher 执行技巧(单元测试必须在 BaseTest
之后扩展):
public abstract class BaseTest {
// ...
protected WebDriver driver;
@Rule
public TestRule testWatcher = new TestWatcher(){
@Override
public void starts(Description desc){
LOG .info(启动浏览器...);
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 ...);
文件scrFile =((TakesScreenshot)驱动程序).getScreenshotAs(
OutputType.FILE);
String scrFilename =Screenshot.png;
文件outputFile =新文件(SCREEN_SHOTS_RESULTS_PATH,scrFilename);
LOG.info(scrFilename +截图创建);
try {
org。apache。commons。io.FileUtils.copyFile(scrFile,outputFile);
} catch(IOException ioe){
LOG.error(异常复制截图后出错,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();
//个人资料定制。例如:
// firefoxProfile.addExtension(firebug-1.8.4-fx.xpi);
// firefoxProfile.setPreference(extensions.firebug.currentVersion,1.8.4);
FirefoxBinary firefox = new FirefoxBinary();
// Firefox自定义。例如:
// firefox.setEnvironmentProperty(DISPLAY,显示);
WebDriver驱动程序=新的FirefoxDriver(firefox,firefoxProfile);
// WebDriver自定义。例如:
// driver.manage().timeouts()。implicitlyWait(SHORT_TIMEOUT_S,TimeUnit.SECONDS);
返回驱动;
}
}
I want to capture a screenshot only upon unexpected exception.
解决方案
Note.- This answer could be outdated. The answer is based on Selenium 2.15
Using TestWatcher does the trick (the unit test must extend following 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);
}
}
};
}
Note
Utils.getFirefoxDriver()
returns a customized WebDriver
. Something like:
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;
}
}
这篇关于Selenium 2和JUnit4:如何捕获截图的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文