Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get Har Log as java object from selenium #9

Open
joshhe opened this issue Oct 31, 2015 · 7 comments
Open

get Har Log as java object from selenium #9

joshhe opened this issue Oct 31, 2015 · 7 comments
Assignees

Comments

@joshhe
Copy link

joshhe commented Oct 31, 2015

Hi,

I am wondering if there is way to get the har log as a string or object using selenium in java? Currently I would have to read from the saved har file and read that in as a string. I know we can trigger the har export through java script, but I don't know how to get it in the java code...

Something like this?
driver.get("https://google.com");
if(driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript(
" var options = { token: "mySpecialValue", getData: true, title: "Title for the HAR log", jsonp: false,};" +
"HAR.triggerExport(options).then(result => { var har = JSON.parse(result.data); console.log("HAR log: ", har.log);}, err => { console.error(err);});");
}

then, how do I get it in java code...
Thanks!

@janodvarko
Copy link
Member

I don't have much experience with Java and Selenium, but could you just store the HAR log (as a text) inside an element on the page:

  var options = {
    token: "test",
    getData: true,
  };

  HAR.triggerExport(options).then(result => {
    var harNode = document.getElementById("content");
    harNode.textContent = result.data;
  });

...and then do something like as follows in Java:

WebElement harNode = driver.findElement(By.id("content"));
String harLog = harNode.getText();

Honza

@joshhe
Copy link
Author

joshhe commented Nov 3, 2015

That's awesome! Thanks for the tip. It works!

@janodvarko
Copy link
Member

Excellent, out of curiosity, can you paste the full example here?
Honza

@joshhe
Copy link
Author

joshhe commented Nov 4, 2015

Here is code I have, it uses sessionStorage to store the har.
String harLog = null;
if(driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript(
" console.log("here"); var options = { token: "test", getData: true, jsonp: false}; console.log("here3"); " +
"HAR.triggerExport(options).then(result => { console.log(result.data); sessionStorage.har = result.data; });");
}

for(int i = 0; i < 20 && harLog == null; i++) {
  if (driver instanceof JavascriptExecutor) {
    harLog = (String) ((JavascriptExecutor) driver).executeScript(
            "return sessionStorage.har;");
  }
  Thread.sleep(1000);
}

This works for most of the websites I tested. But I ran into two edge cases.

  1. http://www.asdfalsdfjkl.com
    When the host name cannot be resolved, no har is generated. Even though I have the "devtools.netmonitor.har.forceExport" set to true. An exception is thrown "HAR is not defined" when I try to get it through the API. However, when I have the ""devtools.netmonitor.har.enableAutoExportToFile" set to true, there is actually a .har file been exported. But there is not error indicating that the hostname cannot be resolved in the har.

  2. http://cnet.com
    This site is loaded twice on a single driver.get(url) call, at the end, when I try to get the har string, it's always null through the API call. However, when I have the "devtools.netmonitor.har.enableAutoExportToFile" set to true, I actually see two .har files been exported. I don't know why it doesn't work when I call it using the API.

Do you know how to resolve those two edge cases when I try to get har through the API? autoExportToFile works for all cases, but I don't really want to read har file in as string if I don't have to.

Thanks!

@janodvarko janodvarko self-assigned this Nov 4, 2015
@janodvarko
Copy link
Member

Both these cases look like a bug. I'll investigate it

Honza

@tobli
Copy link

tobli commented Nov 30, 2015

@chemicwepn - it should work if you use executeAsyncScript. I'm using a similar setup, but from node.js.
My async script is:

const script = `
var callback = arguments[arguments.length - 1];
function triggerExport() {
  HAR.triggerExport({'token':'test', 'getData':true})
  .then((result) => callback(result.data))
  .catch((e) => callback(e));
  };
if (typeof HAR === 'undefined') {
  addEventListener('har-api-ready', triggerExport, false);
} else {
  triggerExport();
}`;
return driver.executeAsyncScript(script);

I guess in you case replace the callback(result.data) with

{
  sessionStorage.har = result.data;
  callback();
}

to indicate that the async process of generating and storing the har is done.

@batmantest7
Copy link

Hi Team,

Is the issue for the case 2] solved?
2) http://cnet.com
This site is loaded twice on a single driver.get(url) call, at the end, when I try to get the har string, it's always null through the API call. However, when I have the "devtools.netmonitor.har.enableAutoExportToFile" set to true, I actually see two .har files been exported. I don't know why it doesn't work when I call it using the API.

My test case is:
1] Get url1
2] Do something
3] Get url1
4] Do something else

Result generated:

Only the step 3] and step 4] har logs are captured in the .har file

The step 1] and step 2] har logs are not captured.

Is there any tracking bug for this?

When I set "devtools.netmonitor.har.enableAutoExportToFile" set to true, logs are generated for all steps.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants