Hello.

I'm using Selenium WebDriver and Firefox, with the HAR Export Trigger add-on. I'm trying to use Seleniums execute_script function to return the value/data from the HAR test page, but I don't know JS at all, and I'm not a Perl expert. Basically, I don't know how to do a callback in the JS.

I started out as simple as possible. I'm using the HAR Content API Test Page (http://janodvarko.cz/har/tests/har-export-trigger/har-export-api.html). I can do a simple call to get the title of the page, that's not what I'm after though.

I think the best way is to just post the code below and it'll hopefully illustrate more clearly what I'm trying to accomplish. I commented out some profile settings to try and conform to the HAR test page recommendations.

I've included some inline comments in the code explaining script_1, script_2, script_3 JS strings.

Any assistance greatly appreciated. Thanks for your time.

use strict; use warnings; use Data::Dumper; use Selenium::Remote::Driver; use Selenium::Remote::Driver::Firefox::Profile; use WWW::Selenium; $Data::Dumper::Sortkeys = 1; binmode(STDOUT, ":utf8"); my $log = 'log'; my $errors_log = 'errors_log'; open STDOUT, '>', $log or die "Can't write to $!"; open STDERR, '>', $errors_log or die "Can't write to $!"; my $profile = Selenium::Remote::Driver::Firefox::Profile->new(); $profile->add_extension('c:/selenium server/har_export_trigger-0.5.0-b +eta.7-fx.xpi'); $profile->set_preference( "browser.extensions.netmonitor.har.contentAPIToken" => "test", + # set to a token that is consequently passed +into all HAR API calls to verify the user. #"browser.devtools.netmonitor.har.defaultFileName" => "Autoexp +ort_%y%m%d_%H%M%S", # default name of the target HAR file. The de +fault file name supports formatters #"browser.devtools.netmonitor.har.defaultLogDir" => "c:/hardir +", # default log directory for generate HAR files. If + empty all automatically generated HAR + # files are stored in <FF-profile>/har/logs #"browser.devtools.netmonitor.har.pageLoadedTimeout" => "2500", + # Amount of time [ms] the auto-exporter should wait + after the last finished request before + # exporting the HAR file. ); $profile->set_boolean_preference( "browser.extensions.netmonitor.har.enableAutomation" => 1, # e +nable the automation without having a new HAR file created for every +loaded page. "browser.extensions.netmonitor.har.autoConnect" => 1, # se +t if you want to have the HAR object available without the developer +toolbox being open. #"browser.devtools.netmonitor.enabled" => 1, # ena +ble netmonitor #"browser.devtools.netmonitor.har.compress" => 0, # If + set to true the final HAR file is zipped. This might represents grea +t disk-space optimization especially # if H +TTP response bodies are included. #"browser.devtools.netmonitor.har.enableAutoExportToFile" => 1, # +If true, a new HAR file is created for every loaded page automaticall +y. #"browser.devtools.netmonitor.har.forceExport" => 1, # The + result HAR file is created even if there are no HTTP requests. #"browser.devtools.netmonitor.har.includeResponseBodies" => 0, # I +f set to true, HTTP response bodies are also included in the HAR file + (can produce significantly bigger amount of data). #"browser.devtools.netmonitor.har.jsonp" => 0,# If set to true the + export format is HARP (support for JSONP syntax that is easily trans +ferable cross domains) #"browser.devtools.netmonitor.har.jsonpCallback" => 0 # Default na +me of JSONP callback (used for HARP format) ); my $driver = Selenium::Remote::Driver->new( 'firefox_profile' => $profile ); my $elem = Selenium::Remote::WebElement->new(); # create our web eleme +nt $driver->get('http://janodvarko.cz/har/tests/har-export-trigger/har-ex +port-api.html'); sleep 10; if ( $elem = $driver->find_element('xhr1', 'id') ) { print "found xhr1\n"; $elem->click(); # maybe I'm clicking too early, or should do the c +lick with a JS action? But how... my $script_1 = q{ var options = { token: "test", getData: true, }; HAR.triggerExport(options).then(result => { console.log(result.data); }); }; # the above throws an exception: org.openqa.selenium.WebDriverExce +ption: HAR is not defined # after some searching, I found a potential workaround # https://github.com/firebug/har-export-trigger/issues/13 # and tried it as script_2 below: # but I don't know how to do a callback... my $script_2 = q{ function triggerExport() { HAR.triggerExport({'token':'test', 'getData':true}) .then((result) => { /* return har via callback */ })); }; if (typeof HAR === 'undefined') { addEventListener('har-api-ready', triggerExport, false); } else { triggerExport(); }; }; # the above script has some errors? I modified it best I could to + include the conditional, but still need a callback somewhere... # script_3: my $script_3 = q{ function triggerExport() { var options = { token: "test", getData: true, }; HAR.triggerExport(options).then(result => { console.log(result.data); }); }; if (typeof HAR === 'undefined') { addEventListener('har-api-ready', triggerExport, false); } else { triggerExport(); }; }; my $v = $driver->execute_script($script_3, 'myId'); # myId? xhr1? print Dumper $v; }

In reply to Selenium WebDriver, execute_script, HAR export trigger by Perluro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.