test: eliminate racy pattern in multiple tests (#2202)

This patch eliminates a race pattern we've been using in a bunch
of tests.
This commit is contained in:
Andrey Lushnikov 2018-03-15 16:53:34 -07:00 committed by GitHub
parent 309cafb876
commit 150e5a9bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -852,30 +852,20 @@ describe('Page', function() {
it('should immediately resolve promise if node exists', async({page, server}) => { it('should immediately resolve promise if node exists', async({page, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
const frame = page.mainFrame(); const frame = page.mainFrame();
let added = false; await frame.waitForSelector('*');
await frame.waitForSelector('*').then(() => added = true);
expect(added).toBe(true);
added = false;
await frame.evaluate(addElement, 'div'); await frame.evaluate(addElement, 'div');
await frame.waitForSelector('div').then(() => added = true); await frame.waitForSelector('div');
expect(added).toBe(true);
}); });
it('should resolve promise when node is added', async({page, server}) => { it('should resolve promise when node is added', async({page, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
const frame = page.mainFrame(); const frame = page.mainFrame();
let added = false; const watchdog = frame.waitForSelector('div');
const watchdog = frame.waitForSelector('div').then(() => added = true);
// run nop function..
await frame.evaluate(() => 42);
// .. to be sure that waitForSelector promise is not resolved yet.
expect(added).toBe(false);
await frame.evaluate(addElement, 'br'); await frame.evaluate(addElement, 'br');
expect(added).toBe(false);
await frame.evaluate(addElement, 'div'); await frame.evaluate(addElement, 'div');
await watchdog; const eHandle = await watchdog;
expect(added).toBe(true); const tagName = await eHandle.getProperty('tagName').then(e => e.jsonValue());
expect(tagName).toBe('DIV');
}); });
it('should work when node is added through innerHTML', async({page, server}) => { it('should work when node is added through innerHTML', async({page, server}) => {
@ -890,12 +880,11 @@ describe('Page', function() {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE); await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
const otherFrame = page.frames()[1]; const otherFrame = page.frames()[1];
let added = false; const watchdog = page.waitForSelector('div');
page.waitForSelector('div').then(() => added = true);
await otherFrame.evaluate(addElement, 'div'); await otherFrame.evaluate(addElement, 'div');
expect(added).toBe(false);
await page.evaluate(addElement, 'div'); await page.evaluate(addElement, 'div');
expect(added).toBe(true); const eHandle = await watchdog;
expect(eHandle.executionContext().frame()).toBe(page.mainFrame());
}); });
it('should run in specified frame', async({page, server}) => { it('should run in specified frame', async({page, server}) => {
@ -903,13 +892,11 @@ describe('Page', function() {
await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE); await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
const frame1 = page.frames()[1]; const frame1 = page.frames()[1];
const frame2 = page.frames()[2]; const frame2 = page.frames()[2];
let added = false; const waitForSelectorPromise = frame2.waitForSelector('div');
const waitForSelectorPromise = frame2.waitForSelector('div').then(() => added = true);
expect(added).toBe(false);
await frame1.evaluate(addElement, 'div'); await frame1.evaluate(addElement, 'div');
expect(added).toBe(false);
await frame2.evaluate(addElement, 'div'); await frame2.evaluate(addElement, 'div');
await waitForSelectorPromise; const eHandle = await waitForSelectorPromise;
expect(eHandle.executionContext().frame()).toBe(frame2);
}); });
it('should throw if evaluation failed', async({page, server}) => { it('should throw if evaluation failed', async({page, server}) => {
@ -1029,13 +1016,11 @@ describe('Page', function() {
await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE); await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
const frame1 = page.frames()[1]; const frame1 = page.frames()[1];
const frame2 = page.frames()[2]; const frame2 = page.frames()[2];
let added = false; const waitForXPathPromise = frame2.waitForXPath('//div');
const waitForXPathPromise = frame2.waitForXPath('//div').then(() => added = true);
expect(added).toBe(false);
await frame1.evaluate(addElement, 'div'); await frame1.evaluate(addElement, 'div');
expect(added).toBe(false);
await frame2.evaluate(addElement, 'div'); await frame2.evaluate(addElement, 'div');
await waitForXPathPromise; const eHandle = await waitForXPathPromise;
expect(eHandle.executionContext().frame()).toBe(frame2);
}); });
it('should throw if evaluation failed', async({page, server}) => { it('should throw if evaluation failed', async({page, server}) => {
await page.evaluateOnNewDocument(function() { await page.evaluateOnNewDocument(function() {