Patta
Patta

Reputation: 11

Capture HTTP request headers / parameters using BrowserMob Proxy and selenium web driver using perl client

I'm trying to capture http request parameters using Selenium::Remote::Driverand Browsermob::Proxy. The pseudo code is as below. I couldn't get the http headers.

Any help would be appreciated.

use Selenium::Remote::Driver;
use Data::Dumper;

my $proxy = Browsermob::Proxy->new(trace => 2);
my $driver = Selenium::Remote::Driver->new(
browser_name => 'firefox',
proxy        => $proxy->selenium_proxy(1)
);

$proxy->new_har( payload => {
        initialPageRef => '1' }, captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true');

$driver->get('http://www.google.com/');
print Dumper($proxy->har);

UPDATE:

  1. The module Net::HTTP::Spore::Role::Middleware uses a deprecated method (Class::MOP::load_class). I changed it to Class::Load::load_class
  2. The argument 'httpProxy' to Selenium::Remote::Driver has to be specific IP address without URL scheme. If it is $proxy->selenium_proxy, it is not working.

However, I could capture only first request. The subsequent requests that selenium drive makes couldn't be captured. For example, the below code captures only the first GET request not the second one.

use Data::Dumper;
use Browsermob::Proxy;
use Selenium::Remote::Driver;

my $proxy = Browsermob::Proxy->new(
server_port => 8080,
);


$proxy->new_har( payload => {
        initialPageRef => 'payload is optional' }, captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true', port => $proxy->port );

my $driver = Selenium::Remote::Driver->new(
browser_name => 'firefox',
proxy        =>  { 'proxyType' => 'manual', 'httpProxy' =>     '127.0.0.1:'.$proxy->port}, 
);
$driver->get('http://www.google.com/');
$driver->find_element("//input[\@name='q']")->send_keys("selenium");
$driver->find_element("//button[\@name='btnG']")->click;
print Dumper($proxy->har);

Upvotes: 1

Views: 1674

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21666

Use create_new_har instead of new_har.

$proxy->create_new_har(
    payload => {
        initialPageRef => 'payload is optional'
    },
    captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true'
);

Upvotes: 0

Related Questions