I have been using selenium webdriver and chrome and logs recently. But any timestamp values are coming back in a weird date time stamp format. I've search all over, and I cannot figure what it is. Furthermore, other values besides timestamp (like requestId or walltime) are also in new unknown formats. What format is this and how can I get it into a normal (MM DD YYYY HH:MM:SS..) format?
timestamp was 2484894.662632 around June 23rd 2021, 10:53:23.118 timestamp was 2486019.900761 around June 23rd 2021, 11:12:01.277 timestamp was 2581839.545059 around June 24th 2021, 13:49:09.354
Example:
"requestId":"30432.634","timestamp":87693.142713,"type":"XHR","wallTime":1624556888.229531}
Code snippet:
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
flavorCapability.setCapability("goog:loggingPrefs", logPrefs);
driver.manage().logs().get(LogType.PERFORMANCE).getAll();
There is two way to get the desired result:
1) Simple way:
LogEntries entries = driver.manage().logs().get(LogType.PERFORMANCE);
for(LogEntry entry: entries){
System.out.println(entry.getTimestamp());
System.out.println(entry.getLevel());
System.out.println(entry.getMessage());
System.out.println(entry.toJson());
System.out.println(new Date(entry.getTimestamp()));
}
2) Second way to do it:
import org.json.JSONException;
import org.json.JSONObject;
LogEntries logs = driver.manage().logs().get("performance");
for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
LogEntry entry = it.next();
try {
JSONObject json = new JSONObject(entry.getMessage());
JSONObject message = json.getJSONObject("message");
String method = message.getString("method");
System.out.println(method);
if (method != null && "Network.responseReceived".equals(method)) {
JSONObject params = message.getJSONObject("params");
JSONObject response = params.getJSONObject("response");
JSONObject headers = response.getJSONObject("headers");
String timestamp = headers.getString("date");
String url = response.getString("url");
int status = response.getInt("status");
System.out.println("Response = " + response);
System.out.println("URL = "+ url);
System.out.println("Status Code = "+ status);
System.out.println("headers: " + response.get("headers"));
System.out.println("Timestamp: " + timestamp);
}
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}
Ref: https://chromedevtools.github.io/devtools-protocol/tot/Network/
Note: Please provide the exact requirement, what exactly you want to get?
Subtracting the timestamp as seconds from the 3 datetimes you got these stamps, I could deduce that the timestamp means number of seconds that have passed since 16:38:25 +- 5 sec on the 25th of May 2021. All three timestamps agree that this is the origin.
Don't ask me why the origin is at that time. Maybe the computer booted at that time, or some number overflowed and started from 0 again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With