1

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.

3 Answers 3

5

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");
}

Depending on the driver you're using you might need to use the Augmenter class too.

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.

0

Google says there are several methods possible:

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

https://addons.mozilla.org/en-us/firefox/addon/screenshot-on-fail-selenium/

Take a screenshot with Selenium WebDriver

Please, describe additional requirements if they don't fit.

0

Take a screenshot

  1. a link
  2. link2 for java. It's pretty straightforward. Copy pasting code from this link will probably work.

and then you can save it as blob in database sql server info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.