test: fix flakiness of certain extension tests (#3011)

This commit is contained in:
Andrey Lushnikov 2018-08-01 14:57:19 -07:00 committed by GitHub
parent 95d867aaac
commit 9c96a9283f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,20 @@ const os = require('os');
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-'); const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
function waitForBackgroundPageTarget(browser) {
const target = browser.targets().find(target => target.type() === 'background_page');
if (target)
return Promise.resolve(target);
return new Promise(resolve => {
browser.on('targetcreated', function listener(target) {
if (target.type() !== 'background_page')
return;
browser.removeListener(listener);
resolve(target);
});
});
}
module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions}) { module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
@ -42,19 +56,17 @@ module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBro
}); });
describe('HEADFUL', function() { describe('HEADFUL', function() {
it('background_page target type should be available', async({browser}) => { it('background_page target type should be available', async() => {
const browserWithExtension = await puppeteer.launch(extensionOptions); const browserWithExtension = await puppeteer.launch(extensionOptions);
const page = await browserWithExtension.newPage(); const page = await browserWithExtension.newPage();
const targets = await browserWithExtension.targets(); const backgroundPageTarget = await waitForBackgroundPageTarget(browserWithExtension);
const backgroundPageTarget = targets.find(target => target.type() === 'background_page');
await page.close(); await page.close();
await browserWithExtension.close(); await browserWithExtension.close();
expect(backgroundPageTarget).toBeTruthy(); expect(backgroundPageTarget).toBeTruthy();
}); });
it('target.page() should return a background_page', async({browser}) => { it('target.page() should return a background_page', async({}) => {
const browserWithExtension = await puppeteer.launch(extensionOptions); const browserWithExtension = await puppeteer.launch(extensionOptions);
const targets = await browserWithExtension.targets(); const backgroundPageTarget = await waitForBackgroundPageTarget(browserWithExtension);
const backgroundPageTarget = targets.find(target => target.type() === 'background_page');
const page = await backgroundPageTarget.page(); const page = await backgroundPageTarget.page();
expect(await page.evaluate(() => 2 * 3)).toBe(6); expect(await page.evaluate(() => 2 * 3)).toBe(6);
await browserWithExtension.close(); await browserWithExtension.close();