diff --git a/src/Browser.php b/src/Browser.php index 52ebc350..1abfd54c 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -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])); diff --git a/src/Browser/BrowserProcess.php b/src/Browser/BrowserProcess.php index 08d2187a..d64f230f 100644 --- a/src/Browser/BrowserProcess.php +++ b/src/Browser/BrowserProcess.php @@ -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']; diff --git a/src/BrowserFactory.php b/src/BrowserFactory.php index 8be66a63..d0b135d0 100644 --- a/src/BrowserFactory.php +++ b/src/BrowserFactory.php @@ -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) * diff --git a/src/Page.php b/src/Page.php index c62b8256..7ec580f4 100644 --- a/src/Page.php +++ b/src/Page.php @@ -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); + } } diff --git a/test/suites/BrowserFactoryTest.php b/test/suites/BrowserFactoryTest.php index 80fa26d2..6f99dc8a 100644 --- a/test/suites/BrowserFactoryTest.php +++ b/test/suites/BrowserFactoryTest.php @@ -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); + } } diff --git a/test/suites/PageTest.php b/test/suites/PageTest.php index 029e8852..a4c341bd 100644 --- a/test/suites/PageTest.php +++ b/test/suites/PageTest.php @@ -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); + } }