fix(page.content): throw a nice error when navigation intervenes (#9080)

This commit is contained in:
Dmitry Gozman 2021-09-22 10:40:15 -07:00 committed by GitHub
parent 449a593050
commit e85a3a5a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View File

@ -769,15 +769,21 @@ export class Frame extends SdkObject {
} }
async content(): Promise<string> { async content(): Promise<string> {
const context = await this._utilityContext(); try {
return context.evaluate(() => { const context = await this._utilityContext();
let retVal = ''; return await context.evaluate(() => {
if (document.doctype) let retVal = '';
retVal = new XMLSerializer().serializeToString(document.doctype); if (document.doctype)
if (document.documentElement) retVal = new XMLSerializer().serializeToString(document.doctype);
retVal += document.documentElement.outerHTML; if (document.documentElement)
return retVal; retVal += document.documentElement.outerHTML;
}); return retVal;
});
} catch (e) {
if (js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
throw e;
throw new Error(`Unable to retrieve content because the page is navigating and changing the content.`);
}
} }
async setContent(metadata: CallMetadata, html: string, options: types.NavigateOptions = {}): Promise<void> { async setContent(metadata: CallMetadata, html: string, options: types.NavigateOptions = {}): Promise<void> {

View File

@ -101,3 +101,18 @@ it('should work with newline', async ({page, server}) => {
await page.setContent('<div>\n</div>'); await page.setContent('<div>\n</div>');
expect(await page.$eval('div', div => div.textContent)).toBe('\n'); expect(await page.$eval('div', div => div.textContent)).toBe('\n');
}); });
it('content() should throw nice error during navigation', async ({page, server}) => {
for (let timeout = 0; timeout < 200; timeout += 20) {
await page.setContent('<div>hello</div>');
const promise = page.goto(server.EMPTY_PAGE);
await page.waitForTimeout(timeout);
const [contentOrError] = await Promise.all([
page.content().catch(e => e),
promise,
]);
const emptyOutput = '<html><head></head><body></body></html>';
if (contentOrError !== expectedOutput && contentOrError !== emptyOutput)
expect(contentOrError?.message).toContain('Unable to retrieve content because the page is navigating and changing the content.');
}
});