Fixed page.waitFor (#81)

The original implementation didn't account for frames.
This commit is contained in:
Aleksey 2017-07-14 15:55:38 -07:00 committed by Andrey Lushnikov
parent 560b817d7f
commit ac75455983
2 changed files with 39 additions and 2 deletions

View File

@ -221,11 +221,21 @@ class FrameManager extends EventEmitter {
subtree: true subtree: true
}); });
}); });
await this._client.send('Runtime.evaluate', { let contextId = undefined;
if (!frame.isMainFrame()) {
contextId = this._frameIdToExecutionContextId.get(frame._id);
console.assert(contextId, 'Frame does not have default context to evaluate in!');
}
let { exceptionDetails } = await this._client.send('Runtime.evaluate', {
expression: helper.evaluationString(code, selector), expression: helper.evaluationString(code, selector),
contextId,
awaitPromise: true, awaitPromise: true,
returnByValue: true, returnByValue: false,
}); });
if (exceptionDetails) {
let message = await helper.getExceptionMessage(this._client, exceptionDetails);
throw new Error('Evaluation failed: ' + message);
}
} }
} }

View File

@ -197,6 +197,33 @@ describe('Puppeteer', function() {
await page.evaluate(addElement, 'div'); await page.evaluate(addElement, 'div');
expect(added).toBe(true); expect(added).toBe(true);
})); }));
it('should run in specified frame', SX(async function() {
await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE);
await FrameUtils.attachFrame(page, 'frame2', EMPTY_PAGE);
let frame1 = page.frames()[1];
let frame2 = page.frames()[2];
let added = false;
frame2.waitFor('div').then(() => added = true);
expect(added).toBe(false);
await frame1.evaluate(addElement, 'div');
expect(added).toBe(false);
await frame2.evaluate(addElement, 'div');
expect(added).toBe(true);
}));
it('should throw if evaluation failed', SX(async function() {
await page.evaluateOnInitialized(function() {
document.querySelector = null;
});
await page.navigate(EMPTY_PAGE);
try {
await page.waitFor('*');
fail('Failed waitFor did not throw.');
} catch (e) {
expect(e.message).toBe('Evaluation failed: document.querySelector is not a function');
}
}));
}); });
it('Page Events: ConsoleMessage', SX(async function() { it('Page Events: ConsoleMessage', SX(async function() {