Moving With Automation

Capturing WebSocket Messages Using Selenium WebDriver

Tags: java selenium webdriver websocket

Recently, I was testing a web application which was using WebSocket as a networking layer to talk to the backend server. The messages are serialized using Avro and being sent over the web socket layer to the backend jetty server.

For those who are not familiar with WebSocket messages, please go to this link in chrome “https://web-demo.adaptivecluster.com/". Once the page is loaded, open the chrome devtool and navigate to the WS sub-tab under the network tab. Refer the below screenshot:

Example image

The avro messages are quite complex and there was lot of logic involved to construct these messages. The UI code was written in Reactjs and Typescript to build these messages and it was essential to test the correctness of these messages.

As all our UI automation was developed using Selenium, so we decided to use the same selenium code to validate these messages.

The whole problem boils down to the fact that:

“How to capture WebSocket messages from your Selenium code ?”

We did not want to involve any external web proxy library like BrowserMob/Fiddler to address this problem so we decided to explore if ChromeDriver can do this task without any help of external libraries.

After searching few links on google, we found this link where it is shown, how to capture the browser network data using chromedriver.

We tried this solution and it worked. Below is the final code where I have shown how we captured both inbound/outbound messages on one of the demo site “https://web-demo.adaptivecluster.com/

The code uses Java 8 and following maven dependencies

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
    </dependencies>

Code:

comments powered by Disqus