Java Selenium - 如果@AfterMethod 出现问题,是否可以截图?

Java Selenium - Is it possible to take a screenshot if something fails in @AfterMethod?

一般来说,如果@AfterMethod 出现问题,我会在关闭浏览器等之前截屏

我运行测试通过,但是改变了一些数据,在@AfterMethod中这个数据恢复到基本状态,但有时会失败,有时堆栈不够弄清楚到底是什么引起了问题。由于上述原因,如果@AfterMethod 失败,我想(如果可能的话)截图。

“@AfterMethod 出现问题”我的意思是,例如遇到异常。

每个失败错误都来自 AssertionError Exception 如果你想截图你应该捕获 AssertionError

怎么样?像这样尝试捕获块

try{
 Assert.assertTrue(false) // this throws thread into the catch block
}catch(AssertionError error){
   driver.takeSreenshot() // I don't remember method name

  //and in the end throw again to fail it in reality
  throw error
}