diff --git a/CHANGELOG.md b/CHANGELOG.md index b1b7808..ee4bc56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 4dc326d..1928b72 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/grover/js/processor.js b/lib/grover/js/processor.js index 97fa735..3a9232a 100644 --- a/lib/grover/js/processor.js +++ b/lib/grover/js/processor.js @@ -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); diff --git a/spec/grover/processor_spec.rb b/spec/grover/processor_spec.rb index 98ebada..ce4ed4b 100644 --- a/spec/grover/processor_spec.rb +++ b/spec/grover/processor_spec.rb @@ -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 + 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 + + +

Loading

+ + + + + 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