每当引发异常时如何使硒拍照 [英] How to make selenium take a picture whenever an exception is thrown
问题描述
我正在使用远程连接到网格计算机来运行硒测试.出现错误时,很难分析错误.发生错误时最好有一些图片.
I'm using a remote connect to the grid machine for running selenium tests. When having error, it is difficult to analyze the bug. It is better if we have some pictures when the error occurs.
推荐答案
这里有两个问题需要解决.首先,我们需要一些带有屏幕截图的代码,其次,我们需要让该代码在测试失败时运行.
There are two problems to solve here. Firstly, we need some code that will take a screenshot and secondly, we need to get that code to run when a test fails.
使用
Taking a screenshot is pretty easy in Selenium using the TakesScreenshot
interface. So you'll need something like:
TakesScreenshot ts = (TakesScreenshot)driver;
byte[] image = ts.getScreenshotAs(OutputType.BYTES);
try {
File screenshot = new File("/some/path/myscreenshot.png");
FileOutputStream fos = new FileOutputStream(screenshot);
fos.write(image);
fos.close();
} catch (IOException ex) {
fail("Failed to write screenshot");
}
根据您使用的驱动程序,您可能需要使用也是Augmenter
类.
Depending on the driver you're using you might need to use the Augmenter
class too.
测试失败时运行代码将取决于您使用的测试框架,而不取决于Selenium.例如,如果您使用的是TestNG,则可以编写 ITestListener
收听测试结果,并在测试失败时进行截图.
Running code when tests fail is going to depend on the test framework you use, not on Selenium. For example, if you're using TestNG you can write an instance of ITestListener
to listen to the results of your tests and take a screenshot when one fails.
这篇关于每当引发异常时如何使硒拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!