Selenium 可以在 JUnit 测试失败时截取屏幕截图吗? [英] Can Selenium take a screenshot on test failure with JUnit?

查看:22
本文介绍了Selenium 可以在 JUnit 测试失败时截取屏幕截图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的测试用例失败时,尤其是在我们的构建服务器上,我想拍一张屏幕的图片/截图来帮助我调试后来发生的事情.我知道如何截取屏幕截图,但我希望在 JUnit 中有一种方法可以在浏览器关闭之前在测试失败时调用我的 takeScreenshot() 方法.

When my test case fails, especially on our build server, I want to take a picture / screenshot of the screen to help me debug what happened later on. I know how to take a screenshot, but I was hoping for a way in JUnit to call my takeScreenshot() method if a test fails, before the browser is closed.

不,我不想编辑我们无数的测试来添加 try/catch.我想,我可能,只是可能被谈论成一个注释.我所有的测试都有一个共同的父类,但我想不出我能做些什么来解决这个问题.

No, I don't want to go edit our bazillions of tests to add a try/catch. I could maybe, just possibly be talked into an annotation, I suppose. All of my tests have a common parent class, but I can't think of anything I can do there to solve this.

想法?

推荐答案

几次快速搜索使我找到了这个:

A few quick searches led me to this:

http://blogs.steeplesoft.com/posts/2012/grabbing-screenshots-of-failed-selenium-tests.html

基本上,他建议创建一个 JUnit4 Rule 将测试 Statement 包装在他调用的 try/catch 块中:

Basically, he recommends creating a JUnit4 Rule that wraps the test Statement in a try/catch block in which he calls:

imageFileOutputStream.write(
    ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));

这对您的问题有用吗?

这篇关于Selenium 可以在 JUnit 测试失败时截取屏幕截图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆