I am running selenium tests written in Java as a JUnit task. On a headless
Linux machine with the use of FirefoxDriver and Xvfb. I would like to take
a screenshot when the test fails. There is the following piece of code to
take the screenshots:
@Rule
public TestName name = new TestName();
/**
* TestWatchmen used to take screenshots of browser window if a test
fails.
*/
@Rule
public MethodRule watchman = new TestWatchman() {
@Override
public void failed(Throwable e, FrameworkMethod method) {
System.err.println("TAKING SCREENSHOT");
File scrFile = ((TakesScreenshot)
driver).getScreenshotAs(OutputType.FILE);
Calendar now = new GregorianCalendar();
File outDir = new File(OUT_DIR);
outDir.mkdir();
System.err.println("Output directory: " +
outDir.getAbsolutePath());
//String scrFilename = e.getStackTrace()[0].toString() + ".png";
String scrFilename = name.getMethodName() + ".png";
System.err.println("Taking screenshot to " + scrFilename);
File outputFile = new File(outDir, scrFilename);
try {
FileUtils.copyFile(scrFile, outputFile);
} catch (IOException ioe) {
System.err.println("Failed.");
}
}
};
And here there is a part of the testing function that generates an error:
help.clickDefault("Subnet mask:");
assertTrue(help.isParameterEnabled("Subnet mask:"));
* assertEquals("255.255.255.0", help.getText("Subnet mask:"));*
help.getText("Subnet mask:") returns "255.255.255.255" so the assert fails.
In the stderr of the test I see the following:
INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText {"id":"{65097ec4-ae14-4cff-b99f-d76f2281f608}"}]
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, getElementText {"id":"{dbc96864-ba6e-42de-a8d9-e5a479e0c370}"}]
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [a26c92eb-36ac-4602-a1bf-5d0561771292, quit {}]
TAKING SCREENSHOT
Jul 12, 2012 9:12:05 AM org.openqa.selenium.remote.RemoteWebDriver execute
INFO: Executing: [null, screenshot {}]
And in the XML report of the junit test there's this message:
Error MessageError communicating with the remote browser. It may have died.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18' Driver info:
driver.version: FirefoxDriverStacktraceorg.openqa.selenium.remote.UnreachableBrowserException:
Error communicating with the remote browser. It may have died.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28'
System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
Driver info: driver.version: FirefoxDriver
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:421)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:428)
at
org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:204)
at validation$1.failed(validation.java:60)
Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot
be used after quit() was called.
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08
09:57:28' System info: os.name: 'Linux', os.arch: 'amd64', os.version:
'2.6.26-2-openvz-amd64', java.version: '1.6.0_18'
Driver info: driver.version: FirefoxDriver at
org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:235)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:401)
It looks like the @After function was called before the screenshot and
there is no firefox running. Is there a way to make Junit execute the
TestWatchman before the @After function?
Best regards
Bogdan Katyński
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/QvBq8o1VZbkJ.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.