简体   繁体   中英

Parse response of javascript that was executed in Selenium

I am working in a java+selenium automation framework, and I want try something different- capture some network traffic with javascript within an existing @Test. Javascript because I had far too many problems with browsermob. I want to capture a piece, or two, of the network traffic response; then eventually send/store it some where like S3 for further db validation due to propagation delays.

I am blocked after getting the network response.

I have gotten it as a List object, and can loop through it with an if statement to get what I want.

    JavascriptExecutor js = (JavascriptExecutor) driver;
    String javaScriptScript = "var performance = window.performance || window.mozPerformance || "
                              + "window.msPerformance || window.webkitPerformance || "
                              + "{}; var network = performance.getEntries() || {}; return JSON.stringify(network);";

    List<Object> results = (List<Object>) js.executeScript(javaScriptScript)


    for (Object result : results) {
        if (result.toString().contains("foo")) {
            System.out.println("foo=" + result.toString()); // foo = "name": "foo"
        }
        if (result.toString().contains("bar")) { // bar = "name": "bar"
            System.out.println("bar=" + result.toString());
        }
    }`

I even found out I can update the js from: return network; to: return JSON.stringify(network);

While I am comfortable with this so far, I feel I should be using json here. So I can get to this implementation which gives prettier output:

    String results = (String) js.executeScript(javaScriptScript);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonParser jp = new JsonParser();
    JsonElement je = jp.parse(results);
    String prettyJsonString = gson.toJson(je);
    System.out.println(prettyJsonString);`

But I am less comfortable iterating through the json vs the List, and picking out what I need (eg, name:foo) even though it should be easier?

What is the best approach, and if it is json, how do I iterate and capture what pieces I needs.

TIA.

See https://stackoverflow.com/a/18998203/1387701

Example code below:

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

Basically, I used the JSON path to get the specific value from the JSON.

Here is an example usage

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.5.0</version>
</dependency>



 String json = "...";
    Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
    
    String author0 = JsonPath.read(document, "$.store.book[0].author");
    String author1 = JsonPath.read(document, "$.store.book[1].author");

Here you will get more example usage https://github.com/json-path/JsonPath

To get the JSON path I used this tools which is flexible for me

https://jsonpathfinder.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM