12

I am currently working on automating my tests using Selenium with TestNg and Java. I am able to take screenshot while the tests are running, but there are some situations where the test passes when ideally it should have failed.

So, Is there any java tool that can help in recording the running Selenium tests?

Basically, I want to add screen-cast to my framework. I searched a lot on web/SO but could not find any relevant resources. Any help or suggestion is welcome.

Manu
  • 2,251
  • 19
  • 30
  • ok 2 questons. Do you mean record a video by screencast ? Are you using grid ? – Shamik Nov 20 '15 at 16:03
  • Yes. Record the video. Not using the grid, but using an in-house framework. – Manu Nov 23 '15 at 06:31
  • 1
    Not quite an answer to your question, but sort of an alternative idea. From my experience, even seeing test run interactively may not help, because you don't see why something happened or didn't happen (i.e. you see UI, but not DOM of the HTML). And secondly, it's hard to synch up what script is doing with screen recording, you can only assume. So what I found more helpful is 1 - saving contents of the HTML at various problematic stages (`driver.getPageSource()`); and 2 - investing into logging that makes it clear what test is doing at every moment and why it takes this or the other path – timbre timbre Nov 30 '15 at 18:56

6 Answers6

4

You can use java code record your Test video, for this code to run you also need to add jar file to your project : Reference : Road to automation

@BeforeSuite
        public void startRecording() throws Exception {    

           GraphicsConfiguration gc = GraphicsEnvironment
                   .getLocalGraphicsEnvironment()
                   .getDefaultScreenDevice()
                   .getDefaultConfiguration();

               this.screenRecorder = new ScreenRecorder(gc,
                   new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),
                   new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                        CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                        DepthKey, 24, FrameRateKey, Rational.valueOf(15),
                        QualityKey, 1.0f,
                        KeyFrameIntervalKey, 15 * 60),
                   new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black",
                        FrameRateKey, Rational.valueOf(30)),
                   null);
       this.screenRecorder.start();

    }

    @AfterSuite
    public void stopRecording() throws Exception {
      this.screenRecorder.stop();
    }
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Keshav
  • 415
  • 2
  • 12
3

An another option would be to run your tests remotely on BrowserStack or Sauce Labs - both services have test run Video Recording available.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • We already used Saucelabs to run tests remotely, but they end up being costly for regular test suite runs. – Manu Nov 25 '15 at 07:24
2

One approach would be to do screenshot after each step and then combining them into a video. The answers to this questions provide a couple of candidate libraries for this task.

Another idea would be to actually do a screencast while performing the test, using some browser plugin. But I'm not sure how one would start the recording process. It might be possible to send the short cut for start/stop recording with selenium, but I'm not sure if that would work. For such plugins I can't offer more than a google search

Community
  • 1
  • 1
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thanks for the link. I used Xuggler to create a video, but it is not integrating well with my framework due to multiple screenshot taken. Can you also suggest some tool that itself record video? – Manu Nov 25 '15 at 07:23
  • Updated my answer to reflect what little I can offer in that direction – Jens Schauder Nov 25 '15 at 07:41
2

You could check out Selenium-Grid-Extras, created and used by Groupon. They've build a framework which is capable of doing what your describing using Selenium Grid. I tried it myself once and seemed to work fine. Maybe it's something that would suit your needs.

thebobblob
  • 138
  • 1
  • 1
  • 7
  • I would give it a try. But, I am looking for a screencast tool, not exactly a new framework. Thanks anyways. – Manu Nov 26 '15 at 06:31
1

I will take thebobblob's answer little further. I recetly configured selenium grid-hub environment with groupon's Selenium-Grid_Extras and it works like charm. There are few questions that you need to answer while setting up the nodes/hub for the first time. After running java -jar Selenium-Grid-Extras-Jar.jar at some point it asks you the number of video you want to record which is by default 20. After, the test run all the recording are accessible through hub:port as shown here

Taking it to little further, you can control the recording from your test using groupon api. You have the flexibility. There is an open issue but it gives you the option to manipulate that as well through rest service

Saifur
  • 16,081
  • 6
  • 49
  • 73
0

You can use a Robot class to take a screen shot automaticaly from your java code when you need it. It is part of java standard library.

Create the Robot at the beginnig of your test:

Rectangle screen =  new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();

Then when you need to take a screenshot (every step in your test) just call:

    BufferedImage capture = robot.createScreenCapture(screen);
    ImageIO.write(capture, "jpg", new File("c:/some_distinct_name_designating_test_phase_and_number.jpg"));
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32