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

Use Page.setDefaultTimeout with timeout option #244

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ jobs:

- name: Run tests
run: bundle exec rspec
env:
PUPPETEER_VERSION: ${{ matrix.puppeteer-version }}

- name: Run tests with remote browser
run: bundle exec rspec --tag remote_browser
env:
PUPPETEER_VERSION: ${{ matrix.puppeteer-version }}

- name: Test & publish code coverage
uses: paambaati/[email protected]
Expand Down
8 changes: 6 additions & 2 deletions lib/grover/js/processor.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {

page = await browser.newPage();

// Apply global page timeout
const timeout = options.timeout; delete options.timeout
if (timeout !== undefined) {
page.setDefaultTimeout(timeout);
}

// Basic auth
const username = options.username; delete options.username
const password = options.password; delete options.password
Expand All @@ -100,10 +106,8 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
}

// Setup timeout option (if provided)
if (options.timeout === null) delete options.timeout;
let requestOptions = {};
let requestTimeout = options.requestTimeout; delete options.requestTimeout;
if (requestTimeout === undefined) requestTimeout = options.timeout;
if (requestTimeout !== undefined) {
requestOptions.timeout = requestTimeout;
}
Expand Down
41 changes: 37 additions & 4 deletions spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@
<<-HTML
<html>
<head><link rel='icon' href='data:;base64,iVBORw0KGgo='></head>
#{' '}

<body>
<p id="loading">Loading</p>
<p id="content" style="display: none">Loaded</p>
Expand Down Expand Up @@ -833,16 +833,19 @@
end
end

shared_examples 'raises navigation timeout error' do
shared_examples 'raises navigation timeout error' do |timeout: 1|
if puppeteer_version_on_or_after? '2.0.0'
it do
expect { convert }.to raise_error Grover::JavaScript::TimeoutError, 'Navigation timeout of 1 ms exceeded'
expect { convert }.to raise_error(
Grover::JavaScript::TimeoutError,
"Navigation timeout of #{timeout} ms exceeded"
)
end
else
it do
expect { convert }.to raise_error(
Grover::JavaScript::TimeoutError,
'Navigation Timeout Exceeded: 1ms exceeded'
"Navigation Timeout Exceeded: #{timeout}ms exceeded"
)
end
end
Expand All @@ -862,6 +865,36 @@

it { is_expected.to start_with "%PDF-1.4\n" }
end

context 'when the waitForSelector option is provided' do
let(:url_or_html) do
<<-HTML
<html>
<body></body>

<script>
setTimeout(function() {
document.body.innerHTML = '<h1>Hey there</h1>';
}, 200);
</script>
</html>
HTML
end
let(:options) { basic_header_footer_options.merge('timeout' => timeout, 'waitForSelector' => 'h1') }

context 'when the timeout is less than the page content load' do
let(:timeout) { 100 }

it_behaves_like 'raises navigation timeout error', timeout: 100
end

context 'when the timeout is greater than the page content load' do
let(:timeout) { 2000 }

it { is_expected.to start_with "%PDF-1.4\n" }
it { expect(pdf_text_content).to eq "#{date} Hey there #{protocol}://www.example.net/foo/bar 1/1" }
end
end
end

context 'when requestTimeout option is specified' do
Expand Down
9 changes: 7 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
MiniMagick.validate_on_create = false

def puppeteer_version_on_or_after?(version)
puppeteer_version = ENV.fetch('PUPPETEER_VERSION', '')
puppeteer_version.empty? || Gem::Version.new(puppeteer_version) >= Gem::Version.new(version)
end

def puppeteer_version_on_or_before?(version)
puppeteer_version = ENV.fetch('PUPPETEER_VERSION', '')
puppeteer_version.empty? || Gem::Version.new(puppeteer_version) <= Gem::Version.new(version)
end

def puppeteer_version
@puppeteer_version ||= begin
version = `node -p "require('puppeteer/package.json').version"`.strip
version if version.match?(/\A\d+\.\d+\.\d+\z/)
end
end

def linux_system?
uname = `uname -s`
uname.start_with? 'Linux'
Expand Down
Loading