Selenium可以使用JUnit截取测试失败的截图吗? [英] Can Selenium take a screenshot on test failure with 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.
不,我不想编辑我们的bazillions测试来添加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 规则
在他调用的try / catch块中的测试 Statement
:
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屋!