-
-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add semicolon for safety * Add page creation patch
- Loading branch information
Showing
5 changed files
with
171 additions
and
35 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/puppeteer-extra-plugin-anonymize-ua/test/popup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
|
||
const { test } = require('ava') | ||
|
||
const PUPPETEER_ARGS = ['--no-sandbox', '--disable-setuid-sandbox'] | ||
|
||
const waitEvent = function (emitter, eventName) { | ||
return new Promise(resolve => emitter.once(eventName, resolve)) | ||
} | ||
|
||
test.beforeEach(t => { | ||
// Make sure we work with pristine modules | ||
delete require.cache[require.resolve('puppeteer-extra')] | ||
delete require.cache[require.resolve('puppeteer-extra-plugin-anonymize-ua')] | ||
}) | ||
|
||
test('known issue: will not remove headless from implicitly created popup pages', async (t) => { | ||
const puppeteer = require('puppeteer-extra') | ||
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')()) | ||
const browser = await puppeteer.launch({ args: PUPPETEER_ARGS }) | ||
|
||
const pages = await Promise.all( | ||
[...Array(10)].map(slot => browser.newPage()) | ||
) | ||
for (const page of pages) { | ||
// Works | ||
const ua = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(!ua.includes('HeadlessChrome')) | ||
|
||
// Works | ||
await page.goto('about:blank') | ||
const ua2 = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(!ua2.includes('HeadlessChrome')) | ||
|
||
// Does NOT work: | ||
// https://github.com/GoogleChrome/puppeteer/issues/2669 | ||
page.evaluate(url => window.open(url), 'about:blank') | ||
const popupTarget = await waitEvent(browser, 'targetcreated') | ||
const popupPage = await popupTarget.page() | ||
const ua3 = await popupPage.evaluate(() => window.navigator.userAgent) | ||
// Test against the problem until it's fixed | ||
t.true(ua3.includes('HeadlessChrome')) // should be: !ua3.includes('HeadlessChrome') | ||
|
||
// Works: The bug only affects newly created popups, subsequent page navigations are fine. | ||
await popupPage.goto('about:blank') | ||
const ua4 = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(!ua4.includes('HeadlessChrome')) | ||
} | ||
|
||
await browser.close() | ||
t.true(true) | ||
}) |
89 changes: 89 additions & 0 deletions
89
packages/puppeteer-extra-plugin-anonymize-ua/test/stresstest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict' | ||
|
||
const { test } = require('ava') | ||
|
||
const PUPPETEER_ARGS = ['--no-sandbox', '--disable-setuid-sandbox'] | ||
|
||
test.beforeEach(t => { | ||
// Make sure we work with pristine modules | ||
delete require.cache[require.resolve('puppeteer-extra')] | ||
delete require.cache[require.resolve('puppeteer-extra-plugin-anonymize-ua')] | ||
}) | ||
|
||
test('will remove headless from the user-agent on multiple browsers', async (t) => { | ||
const puppeteer = require('puppeteer-extra') | ||
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')()) | ||
const browser = await puppeteer.launch({ args: PUPPETEER_ARGS }) | ||
|
||
const browsers = await Promise.all( | ||
[...Array(5)].map(slot => puppeteer.launch({ args: PUPPETEER_ARGS })) | ||
) | ||
for (const browser of browsers) { | ||
const page = await browser.newPage() | ||
const ua = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(ua.includes('Windows NT 10.0')) | ||
t.true(!ua.includes('HeadlessChrome')) | ||
} | ||
|
||
await browser.close() | ||
t.true(true) | ||
}) | ||
|
||
test('will remove headless from the user-agent on many pages', async (t) => { | ||
const puppeteer = require('puppeteer-extra') | ||
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')()) | ||
const browser = await puppeteer.launch({ args: PUPPETEER_ARGS }) | ||
|
||
const pages = await Promise.all( | ||
[...Array(30)].map(slot => browser.newPage()) | ||
) | ||
for (const page of pages) { | ||
const ua = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(ua.includes('Windows NT 10.0')) | ||
t.true(!ua.includes('HeadlessChrome')) | ||
} | ||
|
||
await browser.close() | ||
t.true(true) | ||
}) | ||
|
||
test('will remove headless from the user-agent on many incognito pages', async (t) => { | ||
const puppeteer = require('puppeteer-extra') | ||
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')()) | ||
const browser = await puppeteer.launch({ args: PUPPETEER_ARGS }) | ||
|
||
// Requires puppeteer@next currrently | ||
if (browser.createIncognitoBrowserContext) { | ||
const contexts = await Promise.all( | ||
[...Array(30)].map(slot => browser.createIncognitoBrowserContext()) | ||
) | ||
for (const context of contexts) { | ||
const page = await context.newPage() | ||
const ua = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(ua.includes('Windows NT 10.0')) | ||
t.true(!ua.includes('HeadlessChrome')) | ||
} | ||
} | ||
|
||
await browser.close() | ||
t.true(true) | ||
}) | ||
|
||
test('will remove headless from the user-agent on many pages in parallel', async (t) => { | ||
const puppeteer = require('puppeteer-extra') | ||
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')()) | ||
const browser = await puppeteer.launch({ args: PUPPETEER_ARGS }) | ||
|
||
const testCase = async () => { | ||
const page = await browser.newPage() | ||
const ua = await page.evaluate(() => window.navigator.userAgent) | ||
t.true(ua.includes('Windows NT 10.0')) | ||
t.true(!ua.includes('HeadlessChrome')) | ||
} | ||
await Promise.all( | ||
[...Array(30)].map(slot => testCase()) | ||
) | ||
|
||
await browser.close() | ||
t.true(true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters