Skip to content

Commit

Permalink
Add waitForSelectorOptions support
Browse files Browse the repository at this point in the history
  • Loading branch information
andmcgregor committed Jun 22, 2020
1 parent fd0f109 commit 4db6ba1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## Unreleased
- None
- [#64](https://github.com/Studiosity/grover/pull/64) Add waitForSelector support ([@andmcgregor][])

## [0.12.1](releases/tag/v0.12.1) - 2020-05-12
### Fixed
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ page content and devtools.
## Configuration
Grover can be configured to adjust the layout of the resulting PDF/image.

For available PDF options, see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
For available PDF options, see https://github.com/GoogleChrome/puppeteer/blob/main/docs/api.md#pagepdfoptions

Also available are the `emulate_media`, `cache`, `viewport`, `timeout` and `launch_args` options.

Expand All @@ -109,12 +109,12 @@ Grover.configure do |config|
end
```

For available PNG/JPEG options, see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions
For available PNG/JPEG options, see https://github.com/GoogleChrome/puppeteer/blob/main/docs/api.md#pagescreenshotoptions

Note that by default the `full_page` option is set to false and you will get a 800x600 image. You can either specify
the image size using the `clip` options, or capture the entire page with `full_page` set to `true`.

For `viewport` options, see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport
For `viewport` options, see https://github.com/GoogleChrome/puppeteer/blob/main/docs/api.md#pagesetviewportviewport

For `launch_args` options, see http://peter.sh/experiments/chromium-command-line-switches/
Launch parameter args can also be provided using a meta tag:
Expand All @@ -124,9 +124,9 @@ Launch parameter args can also be provided using a meta tag:
```

For `wait_until` option, default for URLs is `networkidle2` and for HTML content `networkidle0`.
For available options see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options
For available options see https://github.com/GoogleChrome/puppeteer/blob/main/docs/api.md#pagegotourl-options

The `wait_for_selector` option can also be used to wait until an element appears on the page.
The `wait_for_selector` option can also be used to wait until an element appears on the page. Additional waiting parameters can be set with the `wait_for_selector_options` options hash. For available options, see: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagewaitforselectorselector-options.

The Chrome/Chromium executable path can be overridden with the `executable_path` option.

Expand All @@ -144,7 +144,7 @@ Grover.new('<some URI with basic authentication', username: 'the username', pass
#### Adding cookies
To set request cookies when requesting a URL, pass an array of hashes as such
_N.B._ Only the `name` and `value` properties are required.
See [page.setCookies](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagesetcookiecookies) documentation for more details.
See [page.setCookies](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagesetcookiecookies) documentation for more details.

```ruby
myCookies = [
Expand Down
2 changes: 1 addition & 1 deletion lib/grover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Grover
#
# @param [String] url URL of the page to convert
# @param [Hash] options Optional parameters to pass to PDF processor
# see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
# see https://github.com/GoogleChrome/puppeteer/blob/main/docs/api.md#pagepdfoptions
#
def initialize(url, options = {})
@url = url
Expand Down
3 changes: 2 additions & 1 deletion lib/grover/js/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {

// If specified, wait for selector
const waitForSelector = options.waitForSelector; delete options.waitForSelector;
const waitForSelectorOptions = options.waitForSelectorOptions; delete options.waitForSelectorOptions;
if (waitForSelector !== undefined) {
await page.waitForSelector(waitForSelector)
await page.waitForSelector(waitForSelector, waitForSelectorOptions)
}

// If we're running puppeteer in headless mode, return the converted PDF
Expand Down
29 changes: 28 additions & 1 deletion spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
<script>
setTimeout(function() {
document.body.innerHTML = '<h1>Hey there</h1>'
document.body.innerHTML = '<h1>Hey there</h1>';
}, 100);
</script>
</html>
Expand All @@ -411,6 +411,33 @@

it { expect(pdf_text_content).to eq "#{date} Hey there http://www.example.net/foo/bar 1/1" }
end

context 'when wait for selector option is specified with options' do
let(:url_or_html) do
<<-HTML
<html>
<body>
<p id="loading">Loading</p>
</body>
<script>
setTimeout(function() {
document.getElementById('loading').remove()
}, 100);
</script>
</html>
HTML
end
let(:options) do
basic_header_footer_options.merge(
'waitForSelector' => '#loading',
'waitForSelectorOptions' => { 'hidden' => true }
)
end
let(:date) { Date.today.strftime '%-m/%-d/%Y' }

it { expect(pdf_text_content).to eq "#{date} http://www.example.net/foo/bar 1/1" }
end
end

context 'when converting to an image' do
Expand Down

0 comments on commit 4db6ba1

Please sign in to comment.