Skip to content

Commit

Permalink
Added "wait until network idle" option (#154)
Browse files Browse the repository at this point in the history
* Added new waitUntilNetworkIdle option.

* Updated readme with waitUntilNetworkIdle option description.
  • Loading branch information
itsgoingd authored and freekmurze committed Jan 28, 2018
1 parent 01c8a60 commit b8cdf9f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ Browsershot::url('https://example.com')
->save($pathToImage);
```

#### Waiting for lazy-loaded resources
Some websites lazy-load additional resources via ajax or use webfonts, which might not be loaded in time for the screenshot. Using the `waitUntilNetworkIdle()` method you can tell Browsershot to wait for a period of 500 ms with no network activity before taking the screenshot, ensuring all additional resources are loaded.

```php
Browsershot::url('https://example.com')
->waitUntilNetworkIdle()
->save($pathToImage);
```

Alternatively you can use less strict `waitUntilNetworkIdle(false)`, which allows 2 network connections in the 500 ms waiting period, useful for websites with scripts periodically pinging an ajax endpoint.

#### Delayed screenshots
You can delay the taking of screenshot by `setDelay()`. This is useful if you need to wait for completion of javascript or if you are attempting to capture lazy-loaded resources.

Expand Down
2 changes: 2 additions & 0 deletions bin/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const callChrome = async () => {
if (request.options && request.options.networkIdleTimeout) {
requestOptions.waitUntil = 'networkidle';
requestOptions.networkIdleTimeout = request.options.networkIdleTimeout;
} else if (request.options && request.options.waitUntil) {
requestOptions.waitUntil = request.options.waitUntil;
}

await page.goto(request.url, requestOptions);
Expand Down
7 changes: 7 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public function setNetworkIdleTimeout(int $networkIdleTimeout)
return $this;
}

public function waitUntilNetworkIdle(bool $strict = true)
{
$this->setOption('waitUntil', $strict ? 'networkidle0' : 'networkidle2');

return $this;
}

public function setUrl(string $url)
{
$this->url = $url;
Expand Down
40 changes: 40 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,44 @@ public function it_can_save_to_temp_dir_with_background()

$this->assertFileExists($targetPath);
}

/** @test */
public function it_can_wait_until_network_idle()
{
$command = Browsershot::url('https://example.com')
->waitUntilNetworkIdle()
->createScreenshotCommand('screenshot.png');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'screenshot',
'options' => [
'waitUntil' => 'networkidle0',
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);

$command = Browsershot::url('https://example.com')
->waitUntilNetworkIdle($strict = false)
->createScreenshotCommand('screenshot.png');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'screenshot',
'options' => [
'waitUntil' => 'networkidle2',
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}
}

0 comments on commit b8cdf9f

Please sign in to comment.