Skip to content

Commit

Permalink
Fix error in processor.js when using the vision_deficiency option (#128)
Browse files Browse the repository at this point in the history
* Fix error in processor.js when using the vision_deficiency option
* Fixing timing issue with emulation of vision deficiency
  • Loading branch information
julianwegkamp authored Aug 29, 2021
1 parent 33b05b3 commit 445ddc0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/grover/js/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
// Emulate the media features, if specified
const mediaFeatures = options.mediaFeatures; delete options.mediaFeatures;
if (Array.isArray(mediaFeatures)) {
page.emulateMediaFeatures(mediaFeatures);
await page.emulateMediaFeatures(mediaFeatures);
}

// Emulate timezone (if provided)
Expand All @@ -100,28 +100,22 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
await page.emulateTimezone(timezone);
}

// Emulate vision deficiency (if provided)
const visionDeficiency = options.visionDeficiency; delete options.visionDeficiency;
if (visionDeficiency !== undefined) {
page.emulateVisionDeficiency(type);
}

// Bypass CSP (content security policy), if provided
const bypassCSP = options.bypassCSP; delete options.bypassCSP;
if (bypassCSP !== undefined) {
page.setBypassCSP(bypassCSP);
await page.setBypassCSP(bypassCSP);
}

// Add extra HTTP headers (if provided)
const extraHTTPHeaders = options.extraHTTPHeaders; delete options.extraHTTPHeaders;
if (extraHTTPHeaders !== undefined) {
page.setExtraHTTPHeaders(extraHTTPHeaders);
await page.setExtraHTTPHeaders(extraHTTPHeaders);
}

// Set geolocation (if provided)
const geolocation = options.geolocation; delete options.geolocation;
if (geolocation !== undefined) {
page.setGeolocation(geolocation);
await page.setGeolocation(geolocation);
}

const raiseOnRequestFailure = options.raiseOnRequestFailure; delete options.raiseOnRequestFailure;
Expand Down Expand Up @@ -201,6 +195,12 @@ const _processPage = (async (convertAction, urlOrHtml, options) => {
await page.waitForTimeout(waitForTimeout);
}

// Emulate vision deficiency (if provided)
const visionDeficiency = options.visionDeficiency; delete options.visionDeficiency;
if (visionDeficiency !== undefined) {
await page.emulateVisionDeficiency(visionDeficiency);
}

// If specified, focus on the specified selector
const focusSelector = options.focus; delete options.focus;
if (focusSelector !== undefined) {
Expand Down
13 changes: 13 additions & 0 deletions spec/grover/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,19 @@
end
end

# Only test `emulateVisionDeficiency` if the Puppeteer supports it
if puppeteer_version_on_or_after? '3.2.0'
context 'when vision deficiency is set to `deuteranopia`' do
let(:url_or_html) { '<html><body style="background-color: red"></body></html>' }
let(:options) { { 'visionDeficiency' => 'deuteranopia' } }

it { expect(convert.unpack('C*')).to start_with "\x89PNG\r\n\x1A\n".unpack('C*') }
it { expect(image.type).to eq 'PNG' }
it { expect(image.dimensions).to eq [800, 600] }
it { expect(mean_colour_statistics(image)).to eq %w[94 71 0] }
end
end

context 'when specifying type of `png`' do
let(:url_or_html) { '<html><body style="background-color: green"></body></html>' }
let(:options) { { type: 'png' } }
Expand Down

0 comments on commit 445ddc0

Please sign in to comment.