Skip to content

Commit

Permalink
refactor delayed screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Nov 20, 2017
1 parent e250c08 commit 6a95cda
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `Browsershot` will be documented in this file

### 3.12.0 - 2017-11-20

- add `setDelay`

### 3.11.1 - 2017-11-18

Expand All @@ -12,7 +15,6 @@ All notable changes to `Browsershot` will be documented in this file
- add `setOption`
- refactor internals


### 3.10.0 - 2017-11-13

- add `setProxyServer`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ Browsershot::url('https://example.com')
```

#### Delayed screenshots
You can delay execution of the screenshot by a specified amount of time by using `setDelay()`. This is useful if you need to wait for completion of javascript or if you are attempting to capture lazy-loaded resources. Delay time is measured in milliseconds.
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.

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

Expand Down
11 changes: 2 additions & 9 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Browsershot
protected $temporaryHtmlDirectory;
protected $timeout = 60;
protected $url = '';
protected $delay = 0;
protected $additionalOptions = [];

/** @var \Spatie\Image\Manipulations */
Expand Down Expand Up @@ -237,11 +236,9 @@ public function windowSize(int $width, int $height)
->setOption('viewport.height', $height);
}

public function setDelay(int $delay)
public function setDelay(int $delayInMilliseconds)
{
$this->delay = $delay;

return $this;
return $this->setOption('delay', $delayInMilliseconds);
}

public function setOption($key, $value)
Expand Down Expand Up @@ -329,10 +326,6 @@ public function createScreenshotCommand(string $targetPath): array
$command['options']['omitBackground'] = true;
}

if ($this->delay > 0) {
$command['options']['delay'] = $this->delay;
}

return $command;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ public function it_can_set_arbitrary_options()
public function it_can_add_a_delay_before_taking_a_screenshot()
{
$targetPath = __DIR__.'/temp/testScreenshot.png';
$delay = 5000;
$start = round(microtime(true) * 1000);
$delay = 2000;
$start = round(microtime(true) * 2000);

This comment has been minimized.

Copy link
@WidgetsBurritos

WidgetsBurritos Nov 20, 2017

Contributor

@freekmurze
It's okay that the delay itself changed from 5 to 2 seconds, but these probably need to remain as round(microtime(true) * 1000);

That's converting from seconds to milliseconds. Multiplying by 2000 here changes the meaning of the test. Sorry I probably should have annotated that.

This comment has been minimized.

Copy link
@freekmurze

freekmurze Nov 20, 2017

Author Member

I'll fix this, thanks for letting me know.

This comment has been minimized.

Copy link
@WidgetsBurritos

WidgetsBurritos Nov 20, 2017

Contributor

no prob.

Browsershot::url('https://example.com')
->setDelay($delay)
->save($targetPath);
$end = round(microtime(true) * 1000);
$end = round(microtime(true) * 2000);

$this->assertGreaterThanOrEqual($delay, $end - $start);
}
Expand Down

0 comments on commit 6a95cda

Please sign in to comment.