Skip to content

Commit

Permalink
fix(tests): background page tests should wait for the page (#4769)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored and aslushnikov committed Jul 29, 2019
1 parent 62f5dc7 commit 7a60746
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions test/headful.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ const mkdtempAsync = helper.promisify(fs.mkdtemp);

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, puppeteer, defaultBrowserOptions}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
Expand All @@ -63,14 +49,14 @@ module.exports.addTests = function({testRunner, expect, puppeteer, defaultBrowse
it('background_page target type should be available', async() => {
const browserWithExtension = await puppeteer.launch(extensionOptions);
const page = await browserWithExtension.newPage();
const backgroundPageTarget = await waitForBackgroundPageTarget(browserWithExtension);
const backgroundPageTarget = await browserWithExtension.waitForTarget(target => target.type() === 'background_page');
await page.close();
await browserWithExtension.close();
expect(backgroundPageTarget).toBeTruthy();
});
it('target.page() should return a background_page', async({}) => {
const browserWithExtension = await puppeteer.launch(extensionOptions);
const backgroundPageTarget = await waitForBackgroundPageTarget(browserWithExtension);
const backgroundPageTarget = await browserWithExtension.waitForTarget(target => target.type() === 'background_page');
const page = await backgroundPageTarget.page();
expect(await page.evaluate(() => 2 * 3)).toBe(6);
expect(await page.evaluate(() => window.MAGIC)).toBe(42);
Expand Down

0 comments on commit 7a60746

Please sign in to comment.