[英]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.我正在使用 java+selenium 自动化框架,我想尝试一些不同的东西——在现有的 @Test 中使用 javascript 捕获一些网络流量。 Javascript because I had far too many problems with browsermob.
Javascript 因为我在 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.
然后最终将其发送/存储在 S3 之类的地方,以便由于传播延迟而进行进一步的数据库验证。
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.我已经将它作为列表 object 获得,并且可以使用 if 语句循环遍历它以获得我想要的内容。
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;
我什至发现我可以从以下位置更新 js:
return network;
to: return JSON.stringify(network);
to:
return JSON.stringify(network);
While I am comfortable with this so far, I feel I should be using json here.虽然到目前为止我对此感到满意,但我觉得我应该在这里使用 json。 So I can get to this implementation which gives prettier output:
所以我可以得到这个实现,它给出了更漂亮的 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?但是我不太舒服地遍历 json 与列表,并挑选出我需要的东西(例如,名称:foo),即使它应该更容易?
What is the best approach, and if it is json, how do I iterate and capture what pieces I needs.什么是最好的方法,如果是 json,我如何迭代和捕获我需要的部分。
TIA. TIA。
See https://stackoverflow.com/a/18998203/1387701见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.基本上,我使用 JSON 路径从 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在这里您将获得更多示例用法https://github.com/json-path/JsonPath
To get the JSON path I used this tools which is flexible for me为了获得 JSON 路径,我使用了这个对我来说很灵活的工具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.