Last active
June 21, 2019 08:10
-
-
Save sahajamit/eb34541f3eef83ef4e8e3ce853cf2311 to your computer and use it in GitHub Desktop.
chrome-dev-tools-02.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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