chore: make a single visibility check (#12317)

This commit is contained in:
Nikolay Vitkov 2024-04-24 15:13:09 +02:00 committed by GitHub
parent e254f746ee
commit e35e783a74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 8 deletions

View File

@ -1252,17 +1252,13 @@ export abstract class ElementHandle<
): Promise<string | Buffer> { ): Promise<string | Buffer> {
const {scrollIntoView = true, clip} = options; const {scrollIntoView = true, clip} = options;
let elementClip = await this.#nonEmptyVisibleBoundingBox();
const page = this.frame.page(); const page = this.frame.page();
// Only scroll the element into view if the user wants it. // Only scroll the element into view if the user wants it.
if (scrollIntoView) { if (scrollIntoView) {
await this.scrollIntoViewIfNeeded(); await this.scrollIntoViewIfNeeded();
// We measure again just in case.
elementClip = await this.#nonEmptyVisibleBoundingBox();
} }
const elementClip = await this.#nonEmptyVisibleBoundingBox();
const [pageLeft, pageTop] = await this.evaluate(() => { const [pageLeft, pageTop] = await this.evaluate(() => {
if (!window.visualViewport) { if (!window.visualViewport) {

View File

@ -306,14 +306,15 @@ describe('Screenshots', function () {
await page.setContent('<h1>remove this</h1>'); await page.setContent('<h1>remove this</h1>');
using elementHandle = (await page.$('h1'))!; using elementHandle = (await page.$('h1'))!;
await page.evaluate((element: HTMLElement) => { await page.evaluate(element => {
return element.remove(); return element.remove();
}, elementHandle); }, elementHandle);
const screenshotError = await elementHandle.screenshot().catch(error => { const screenshotError = await elementHandle.screenshot().catch(error => {
return error; return error;
}); });
expect(screenshotError.message).toBe( expect(screenshotError).toBeInstanceOf(Error);
'Node is either not visible or not an HTMLElement' expect(screenshotError.message).toMatch(
/Node is either not visible or not an HTMLElement|Node is detached from document/
); );
}); });
it('should not hang with zero width/height element', async () => { it('should not hang with zero width/height element', async () => {