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.

hoang nguyen
  • 2,119
  • 5
  • 21
  • 20

3 Answers3

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.

David Webb
  • 190,537
  • 57
  • 313
  • 299
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.

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
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

Community
  • 1
  • 1