mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-12 11:50:22 +03:00
fix(page.content): throw a nice error when navigation intervenes (#9080)
This commit is contained in:
parent
449a593050
commit
e85a3a5a41
@ -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> {
|
||||||
|
@ -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.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user