Extend Selenium Grid Video Recording Capability

Hello, in the previous article, I described how to extend Selenium Grid. You can reach the tutorial in this link. Today, I am going to add  Video Recording capability into our Selenium Grid. Let’s get started Selenium Grid Video Recording Capability.

Create a Recorder for Selenium Grid Video Recording Capability

I am using monte-repack library to record videos. You can take a look at the source code in our repository. The implementation might differ from your need. That’s why I don’t want to get into its details. Maven dependency that I used is:

<dependency>
 <groupId>com.pojosontheweb</groupId>
 <artifactId>monte-repack</artifactId>
 <version>1.0.1</version>
</dependency>

Changing the Proxy Implementation

I want my proxy to start recording in case there are any custom desired capabilities about recording. So I add a String value for recording request. I will check its value and start the recording.

private static String RECORD_VIDEO = "recordVideo";

The code below will get the value of RECORD_VIDEO capability from the TestSession object.

record = (Boolean)session.getRequestedCapabilities().get(RECORD_VIDEO);

Then recording will start.

@Override
public void beforeSession(TestSession session){
    record = (Boolean)session.getRequestedCapabilities().get(RECORD_VIDEO);
    if (record) {
        screenRecorder = new Recorder();
        screenRecorder.startScreenRecorder();
        System.out.println("Video Recording value is "+record);
    }
}

After the session ends, we need to stop recording by using the below code.

@Override
public void afterSession(TestSession session){
    System.out.println("Selenium Extending Grid - After Session");
    if(record)
        screenRecorder.stopScreenRecorder(session.getInternalKey());
}

How to Modify Your Desired Capabilities?

Add your desired capabilities in DesiredCapabilities object as below. Then run your test, and see your Grid records video like a charm.

We create the RemoteWebDriver with our newly added Desired capability in Before Annotation. By this way, every time a driver is created a new session will be created as well.

@Before
public  void beforeClass(){
   DesiredCapabilities caps =  DesiredCapabilities.chrome();
   caps.setCapability("recordVideo",true);
   try {
       driver = new RemoteWebDriver(
               new URL("http://localhost:4444/wd/hub"),
               caps);
   } catch (MalformedURLException e) {
       e.printStackTrace();
   }
}

You can implement anything in the test.

@Test
public void test(){

//Test Implementation
}

We should use quit() method of WebDriver in JUnit’s After function. By doing so Selenium terminates the session and video recording is terminated.

@After
public  void afterClass(){
   driver.quit();
}

Important Note:

Selenium Default Proxy doesn’t know if a new test is started or not. It only knows if there’s a new session or not. You should implement your new functions according to this fact.

[fusion_widget_area name=”avada-custom-sidebar-seleniumwidget” title_size=”” title_color=”” background_color=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””][/fusion_widget_area]

Full Code can be found in https://github.com/canb0/extendedSeleniumGrid

Thanks.
Canberk Akduygu

2 thoughts on “Extend Selenium Grid Video Recording Capability”

  1. Selam, testin başlayıp başlamadığını junit testleri için @Rule annotation ile verilmiş bir nesneye TestWatcher interface’ini uygulayarak alabilirsiniz.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.