Skip to content

Commit

Permalink
Set user agent
Browse files Browse the repository at this point in the history
fix #33
  • Loading branch information
gsouf committed Aug 28, 2018
1 parent 7128a57 commit 3a08fd2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public function createPage(): Page
// Page.enable
$page->getSession()->sendMessageSync(new Message('Page.enable'));

// Network.enable
$page->getSession()->sendMessageSync(new Message('Network.enable'));

// Page.setLifecycleEventsEnabled
$page->getSession()->sendMessageSync(new Message('Page.setLifecycleEventsEnabled', ['enabled' => true]));

Expand Down
4 changes: 4 additions & 0 deletions src/Browser/BrowserProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ private function getArgsFromOptions(array $options)
$args[] = '--window-size=' . implode(',', $options['windowSize']) ;
}

if (array_key_exists('userAgent', $options)) {
$args[] = '--user-agent=' . $options['userAgent'];
}

// add user data dir to args
$args[] = '--user-data-dir=' . $options['userDataDir'];

Expand Down
1 change: 1 addition & 0 deletions src/BrowserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(string $chromeBinaries = null)
* - enableImages: toggle the loading of images (default: true)
* - headless: whether chrome should be started headless (default: true)
* - startupTimeout: maximum time in seconds to wait for chrome to start (default: 30 sec)
* - userAgent: user agent to use for the browser
* - userDataDir: chrome user data dir (default: a new empty dir is generated temporarily)
* - windowSize: size of the window, ex: [1920, 1080] (default: none)
*
Expand Down
21 changes: 20 additions & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,28 @@ public function setCookies($cookies)
}

// send cookies
$response = $this->getSession()->sendMessage(new Message('Network.setCookies', ['cookies' => $browserCookies]));
$response = $this->getSession()
->sendMessage(
new Message('Network.setCookies', ['cookies' => $browserCookies])
);

// return async helper
return new ResponseWaiter($response);
}

/**
* Set user agent for the current page
* @param string $userAgent
* @return ResponseWaiter
* @throws CommunicationException
*/
public function setUserAgent(string $userAgent)
{
$response = $this->getSession()
->sendMessage(
new Message('Network.setUserAgentOverride', ['userAgent' => $userAgent])
);

return new ResponseWaiter($response);
}
}
15 changes: 15 additions & 0 deletions test/suites/BrowserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ public function testWindowSizeOption()

$this->assertEquals([333, 1212], $response);
}

public function testUserAgentOption()
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser([
'userAgent' => 'foobarbaz'
]);

$page = $browser->createPage();

$response = $page->evaluate('navigator.userAgent')->getReturnValue();

$this->assertEquals('foobarbaz', $response);
}
}
22 changes: 22 additions & 0 deletions test/suites/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,26 @@ public function testSetViewport()

$this->assertEquals([100, 300], $response);
}

public function testSetUserAgent()
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser();

$pageFooBar = $browser->createPage();
$pageBarBaz = $browser->createPage();

$pageFooBar->setUserAgent('foobar')->await();
$pageBarBaz->setUserAgent('barbaz')->await();

$pageFooBar->navigate('http://requestbin.fullcontact.com/uhunfhuh')->waitForNavigation();
$pageBarBaz->navigate('http://requestbin.fullcontact.com/uhunfhuh')->waitForNavigation();

$value1 = $pageFooBar->evaluate('navigator.userAgent')->getReturnValue();
$value2 = $pageBarBaz->evaluate('navigator.userAgent')->getReturnValue();

$this->assertEquals('foobar', $value1);
$this->assertEquals('barbaz', $value2);
}
}

0 comments on commit 3a08fd2

Please sign in to comment.