Skip to content

Commit

Permalink
Add waitForSelector()
Browse files Browse the repository at this point in the history
  • Loading branch information
shadoWalker89 authored and freekmurze committed Oct 9, 2023
1 parent 5a1981b commit 1b74e71
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ const callChrome = async pup => {
await page.waitForFunction(request.options.function, functionOptions);
}

if (request.options.waitForSelector) {
await page.waitForSelector(request.options.waitForSelector, request.options.waitForSelectorOptions ?? undefined);
}

output = await getOutput(page, request);

if (!request.options.path) {
Expand Down
11 changes: 11 additions & 0 deletions docs/usage/creating-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ Browsershot::url('https://example.com')
->waitForFunction('window.innerWidth < 100', $pollingInMilliseconds, $timeoutInMilliseconds)
->save($pathToImage);
```

## Waiting for a selector

You can also wait for a selector by using `waitForSelector()`. This is useful if you need to wait for the selector to appear in page.

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

## Adding JS

You can add javascript prior to your screenshot or output using the syntax for [Puppeteer's addScriptTag](https://github.com/puppeteer/puppeteer/blob/v1.9.0/docs/api.md#pageaddscripttagoptions).
Expand Down
11 changes: 11 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ public function waitForFunction(string $function, $polling = self::POLLING_REQUE
return $this->setOption('function', $function);
}

public function waitForSelector(string $selector, array $options = [])
{
$this->setOption('waitForSelector', $selector);

if (! empty($options)) {
$this->setOption('waitForSelectorOptions', $options);
}

return $this;
}

public function setUrl(string $url)
{
if (Helpers::stringStartsWith(strtolower($url), 'file://')) {
Expand Down
43 changes: 43 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,49 @@
], $command);
});

it('can add a wait for selector', function () {
$command = Browsershot::url('https://example.com')
->waitForSelector('.wait_for_me')
->createScreenshotCommand('screenshot.png');

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

it('can add a wait for selector and provide options', function () {
$command = Browsershot::url('https://example.com')
->waitForSelector('.wait_for_me', ['visibile' => true])
->createScreenshotCommand('screenshot.png');

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

it('can input form fields and post and get the body html', function () {
$delay = 2000;

Expand Down

0 comments on commit 1b74e71

Please sign in to comment.