在我们的项目中,我们将Scala Specs2与Selenium一起使用。 我试图实施故障屏幕截图机制" in a classic way (link)"对于我的测试,使用JUnit注释,但是,该规则根本没有调用测试失败。
测试结构如下:
class Tests extends SpecificationWithJUnit{
trait Context extends LotsOfStuff {
@Rule
val screenshotOnFailRule = new ScreenshotOnFailRule(driver)
}
"test to verify stuff that will fail" should {
"this test FAILS" in new Context {
...
}
}
ScreenshotOnFailRule如下所示:
class ScreenshotOnFailRule (webDriver: WebDriver) extends TestWatcher {
override def failed(er:Throwable, des:Description) {
val scrFile = webDriver.asInstanceOf[TakesScreenshot].getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(scrFile, new File(s"/tmp/automation_screenshot${Platform.currentTime}.png"))
}
}
据我所知,它现在可能无法正常工作,因为测试没有使用@Test注释进行注释。 是否可以使用JUnit @Rule注释来注释Specs2测试?
答案 0 :(得分:2)
根据this question,似乎不支持JUnit规则。但您可以尝试使用AroundExample
特征:
import org.specs2.execute.{AsResult, Result}
import org.specs2.mutable._
import org.specs2.specification.AroundExample
class ExampleSpec extends Specification with AroundExample {
// execute tests in sequential order
sequential
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size (10)
}
// more tests..
}
override protected def around[T](t: => T)(implicit ev: AsResult[T]): Result = {
try {
AsResult.effectively(t)
} catch {
case e: Throwable => {
// take screenshot here
throw e
}
}
}
}