Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vitaliiSmokov/89d13f023387358bb2f708d9a6f0949e to your computer and use it in GitHub Desktop.
Save vitaliiSmokov/89d13f023387358bb2f708d9a6f0949e to your computer and use it in GitHub Desktop.
chrome-dev-tools-02.java
String wsURL = this.getWebSocketDebuggerUrl();
private String getWebSocketDebuggerUrl() throws IOException {
String webSocketDebuggerUrl = "";
File file = new File(System.getProperty("user.dir") + "/target/chromedriver.log");
try {
Scanner sc = new Scanner(file);
String urlString = "";
while (sc.hasNextLine()) {
String line = sc.nextLine();
if(line.contains("DevTools request: http://localhost")){
urlString = line.substring(line.indexOf("http"),line.length()).replace("/version","");
break;
}
}
sc.close();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader reader =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
String json = org.apache.commons.io.IOUtils.toString(reader);
JSONArray jsonArray = new JSONArray(json);
for(int i=0; i<jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
if(jsonObject.getString("type").equals("page")){
webSocketDebuggerUrl = jsonObject.getString("webSocketDebuggerUrl");
break;
}
}
}
catch (FileNotFoundException e) {
throw e;
}
if(webSocketDebuggerUrl.equals(""))
throw new RuntimeException("webSocketDebuggerUrl not found");
return webSocketDebuggerUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment