Skip to content

Commit

Permalink
Add click feature (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
artggd authored and freekmurze committed Apr 12, 2018
1 parent 76b42aa commit afe94d8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,22 @@ Browsershot::url('https://example.com')
...
```


#### Clicking on the page

You can specify clicks on the page.

```php
Browsershot::url('https://example.com')
->click('#selector1')
->click('#selector2', 'right', 5, 200) // Right click 5 times on #selector2, each click lasting 200 milliseconds.
```

## Related packages

* Laravel wrapper: [laravel-browsershot](https://github.com/verumconsilium/laravel-browsershot)


## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
11 changes: 11 additions & 0 deletions bin/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ const callChrome = async () => {

await page.goto(request.url, requestOptions);

if (request.options && request.options.clicks) {
for (let i = 0, len = request.options.clicks.length; i < len; i++) {
let clickOptions = request.options.clicks[i];
await page.click(clickOptions.selector, {
'button': clickOptions.button,
'clickCount': clickOptions.clickCount,
'delay': clickOptions.delay,
});
}
}

if (request.options.delay) {
await page.waitFor(request.options.delay);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public function setExtraHttpHeaders(array $extraHTTPHeaders)
return $this;
}

public function click(string $selector, string $button = 'left', int $clickCount = 1, int $delay = 0)
{
$clicks = ($this->additionalOptions['clicks'] ?? []);

$clicks[] = compact('selector', 'button', 'clickCount', 'delay');

$this->setOption('clicks', $clicks);

return $this;
}

/**
* @deprecated This option is no longer supported by modern versions of Puppeteer.
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,40 @@ public function it_can_send_extra_http_headers()
],
], $command);
}

/** @test */
public function it_can_click_on_the_page()
{
$command = Browsershot::url('https://example.com')
->click('#selector1')
->click('#selector2', 'right', 2, 1)
->createScreenshotCommand('screenshot.png');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'screenshot',
'options' => [
'clicks' => [
[
'selector' => '#selector1',
'button' => 'left',
'clickCount' => 1,
'delay' => 0,
],
[
'selector' => '#selector2',
'button' => 'right',
'clickCount' => 2,
'delay' => 1,
],
],
'path' => 'screenshot.png',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}
}

0 comments on commit afe94d8

Please sign in to comment.