diff --git a/CHANGELOG.md b/CHANGELOG.md index 06b079bc..a644980b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,12 +14,16 @@ ## 0.2.2 -> *20xx-xx-xx* (not released) - -> Added input controls +> *2018-08-28* * Features: * Added mouse api (move, click) + * Page info are now in sync with the browser + * Added a shortcut to get current page url: ``Page::getCurrentUrl`` + * Added ability to get and set cookies from a page: ``Page.setCookies``, ``Page.readCookies`` , ``Page.readAllCookies`` + * improved some error reporting + * fixed a bug with directory creation for screenshots + * added ability to set custom user agent: ``Page::setUserAgent`` or via factory option ``userAgent`` * Bug fixes: * none diff --git a/README.md b/README.md index 226f8b10..1b18d7c5 100644 --- a/README.md +++ b/README.md @@ -127,14 +127,15 @@ API Here are the options available for the browser factory: -| Option name | Default | Description | -|--------------------|-----------------------|---------------------------------------------------------------------------------| -| connectionDelay | 0 | Delay to apply between each operation for debugging purposes | +| Option name | Default | Description | +|--------------------|-----------------------|-----------------------------------------------------------------------------------| +| connectionDelay | 0 | Delay to apply between each operation for debugging purposes | | debugLogger | null | A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages | -| enableImages | true | Toggles loading of images | -| headless | true | Enable or disable headless mode | -| userDataDir | none | chrome user data dir (default: a new empty dir is generated temporarily) | -| startupTimeout | 30 | Maximum time in seconds to wait for chrome to start | +| enableImages | true | Toggles loading of images | +| headless | true | Enable or disable headless mode | +| userAgent | none | User agent to use for the whole browser (see page api for alternative) | +| userDataDir | none | chrome user data dir (default: a new empty dir is generated temporarily) | +| startupTimeout | 30 | Maximum time in seconds to wait for chrome to start | | windowSize | - | Size of the window. usage: ``[$width, $height]`` - see also Page::setViewportSize | ### Browser API @@ -299,6 +300,74 @@ The mouse API is dependent on the page instance and allows you to control the mo $page->waitForReload(); ``` +### Cookie API + +You can set and get cookies for a page: + +#### Set Cookie + +```php + use HeadlessChromium\Cookies\Cookie; + + $page = $browser->createPage(); + + // example 1: set cookies for a given domain + + $page->setCookies([ + Cookie::create('name', 'value', [ + 'domain' => 'example.com', + 'expires' => time() + 3600 // expires in 1 day + ]) + ])->await(); + + + // example 2: set cookies for the current page + + $page->navigate('http://example.com')->waitForNavigation(); + + $page->setCookies([ + Cookie::create('name', 'value', ['expires']) + ])->await(); + +``` + +#### Get Cookies + +```php + use HeadlessChromium\Cookies\Cookie; + + $page = $browser->createPage(); + + // example 1: get all cookies for the browser + + $cookies = $page->getAllCookies(); + + // example 2: get cookies for the current page + + $page->navigate('http://example.com')->waitForNavigation(); + $cookies = $page->getCookies(); + + // filter cookies with name == 'foo' + $cookiesFoo = $cookies->filterBy('name', 'foo'); + + // find first cookie with name == 'bar' + $cookieBar = $cookies->findOneBy('name', 'bar'); + if ($cookieBar) { + // do something + } + +``` + +### Set user agent + +You can set an user agent per page : + +```php +$page->setUserAgent('my user agent'); +``` + +See also BrowserFactory option ``userAgent`` to set it for the whole browser. + ------------------------------------------------------------------------------------------------------------------------