【发布时间】:2012-10-19 08:07:15
【问题描述】:
我正在使用远程连接到网格机器来运行 selenium 测试。当出现错误时,很难分析错误。出错的时候最好有一些图片。
【问题讨论】:
我正在使用远程连接到网格机器来运行 selenium 测试。当出现错误时,很难分析错误。出错的时候最好有一些图片。
【问题讨论】:
这里有两个问题需要解决。首先,我们需要一些可以截取屏幕截图的代码,其次,我们需要在测试失败时运行该代码。
使用 the TakesScreenshot interface 在 Selenium 中截屏非常简单。所以你需要这样的东西:
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");
}
根据您使用的驱动程序,您可能还需要使用the Augmenter class。
测试失败时运行代码将取决于您使用的测试框架,而不是 Selenium。例如,如果您使用 TestNG,您可以编写一个 ITestListener 实例来监听您的测试结果,并在测试失败时截取屏幕截图。
【讨论】:
【讨论】: