【发布时间】:2013-07-18 09:09:31
【问题描述】:
您好,我正在使用 selelium 和 spock 编写冒烟测试,我想在测试失败时截屏。试试这个:
public class ScreenshotTestRule implements MethodRule {
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate()
} catch (Throwable t) {
captureScreenshot(frameworkMethod.getName().replaceAll(" ", "-"))
throw t
}
}
public void captureScreenshot(String fileName) {
try {
new File("target/surefire-reports/").mkdirs()
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)
Files.copy(screenshot, new File("/target/surefire-reports/"+fileName+".png"))
} catch (Exception e) {
print "Error while creating screenshot " + fileName
throw new RuntimeException(e)
}
}
}
}
}
但我收到此错误:
java.lang.RuntimeException: groovy.lang.MissingFieldException: No such field: driver for class: lt.inventi.apollo.system.test.SmokeTest$ScreenshotTestRule
【问题讨论】:
-
那么
driver来自哪里?它是在哪里创建的?您确定不想将其实际传递给captureScreenshot方法吗? -
driver 是 SmokeTest 类字段,ScreenShotTestRule 类是内部的,所以我想它应该在这里可见
标签: selenium groovy webdriver spock