Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add waitForSelector support #64

Merged
merged 3 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Added
- [#63](https://github.com/Studiosity/grover/pull/63) Ensure cookies from incoming request are passed to Grover via Middleware ([@braindeaf][])
- [#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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ 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/puppeteer/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. 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.

Javascript can be executed on the page (after render and before conversion to PDF/image)
Expand Down
7 changes: 7 additions & 0 deletions lib/grover/js/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
await page.evaluate(executeScript);
}

// 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, waitForSelectorOptions)
}

// If we're running puppeteer in headless mode, return the converted PDF
if (debug === undefined || (typeof debug === 'object' && (debug.headless === undefined || debug.headless))) {
return await page[convertAction](options);
Expand Down
47 changes: 47 additions & 0 deletions spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,53 @@

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

context 'when wait for selector option is specified' do
let(:url_or_html) do
<<-HTML
<html>
<body></body>
<script>
setTimeout(function() {
document.body.innerHTML = '<h1>Hey there</h1>';
}, 100);
</script>
</html>
HTML
end
let(:options) { basic_header_footer_options.merge('waitForSelector' => 'h1') }
let(:date) { Date.today.strftime '%-m/%-d/%Y' }

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