Skip to content

Instantly share code, notes, and snippets.

@sahajamit
Last active May 31, 2024 07:15
Show Gist options
  • Select an option

  • Save sahajamit/c2d6827736f2b267a8d08c41e559ad24 to your computer and use it in GitHub Desktop.

Select an option

Save sahajamit/c2d6827736f2b267a8d08c41e559ad24 to your computer and use it in GitHub Desktop.
Capture WebSocket Network Calls with WebDriver
package com.sahajamit.selenium.ws;
import org.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.logging.Level;
/**
* Created by amitrawat on 23/9/17.
*/
public class CaptureWSMessages {
private static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chromedriver");
LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.PERFORMANCE, Level.ALL);
DesiredCapabilities cap = new DesiredCapabilities().chrome();
cap.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
driver = new ChromeDriver(cap);
driver.navigate().to("https://web-demo.adaptivecluster.com/");
Thread.sleep(5000);
LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
driver.close();
driver.quit();
logEntries.forEach(entry->{
JSONObject messageJSON = new JSONObject(entry.getMessage());
String method = messageJSON.getJSONObject("message").getString("method");
if(method.equalsIgnoreCase("Network.webSocketFrameSent")){
System.out.println("Message Sent: " + messageJSON.getJSONObject("message").getJSONObject("params").getJSONObject("response").getString("payloadData"));
}else if(method.equalsIgnoreCase("Network.webSocketFrameReceived")){
System.out.println("Message Received: " + messageJSON.getJSONObject("message").getJSONObject("params").getJSONObject("response").getString("payloadData"));
}
});
}
}
@btphucvn

btphucvn commented Aug 1, 2018

Copy link
Copy Markdown

Hi!
Your code is awesome!
but, How can i send websocket message?

@jimmy6

jimmy6 commented Aug 31, 2018

Copy link
Copy Markdown

I am not able to print all msg in websocket frame

@SachinB75

Copy link
Copy Markdown

Do not get the Network Websocket message on the remote webdriver. Are there any configurations that we need to set to get the messages on remote webdriver?

@gerardnorton

gerardnorton commented Mar 21, 2020

Copy link
Copy Markdown

Use this code!

**System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + File.separator + "drivers" + File.separator + "chromedriver.exe");

LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.PERFORMANCE, Level.ALL);

ChromeOptions options = new ChromeOptions();
options.setCapability("goog:loggingPrefs", loggingprefs);
options.addArguments("--enable-devtools-experiments", "--force-devtools-available", "--debug-devtools");

ChromeDriver chromeDriver = new ChromeDriver(options);
DevTools devTools = chromeDriver.getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));`

devTools.addListener(Network.webSocketFrameSent(), webSocketFrameSent -> printData(webSocketFrameSent));
devTools.addListener(Network.webSocketFrameReceived(), webSocketFrameReceived -> printData(webSocketFrameReceived));**

@MosheMadula

Copy link
Copy Markdown

Hi gerardnorton, Im new to coding and have not learned javascript yet. How to do I translate the same thing to python for selenium without using any external web proxy library

@rodrigoslayertech

Copy link
Copy Markdown

@sahajamit
How to translate this to using in Python?

@thaboBD

thaboBD commented Oct 7, 2021

Copy link
Copy Markdown

@sahajamit
Would you be able to help me translate this into C#?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment