目录
异常截图场景
实现原理
实现代码
public class ScreenTest {
public static WebDriver driver;
void saveScreen() throws IOException {
// 生成时间戳
LocalDateTime tmNow = LocalDateTime.now();
// 保存文件
File currentScreen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(currentScreen, new File("./files/" + tmNow + ".png"));
// 添加allure附件
Allure.addAttachment("add picture", "image/png",
new FileInputStream("./files/" + tmNow + ".png"), ".jpg");
}
@Test
void sogou() throws IOException {
try {
driver = new ChromeDriver();
driver.get("https://vip.ceshiren.com/");
// 错误代码
driver.findElement(By.name("adb"));
} catch (NoSuchElementException e) {
saveScreen();
fail("NoSuchElementException:" + e);
}
}
}