/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test as it, expect } from './pageTest';
import { attachFrame, detachFrame } from '../config/utils';
async function giveItTimeToLog(frame) {
await frame.evaluate(() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f))));
await frame.evaluate(() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f))));
}
const addElement = tag => document.body.appendChild(document.createElement(tag));
it('should throw on waitFor', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
let error;
// @ts-expect-error waitFor is undocumented
await page.waitForSelector('*', { waitFor: 'attached' }).catch(e => error = e);
expect(error.message).toContain('options.waitFor is not supported, did you mean options.state?');
});
it('should tolerate waitFor=visible', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
let error = false;
// @ts-expect-error waitFor is undocumented
await page.waitForSelector('*', { waitFor: 'visible' }).catch(() => error = true);
expect(error).toBe(false);
});
it('should immediately resolve promise if node exists', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const frame = page.mainFrame();
await frame.waitForSelector('*');
await frame.evaluate(addElement, 'div');
await frame.waitForSelector('div', { state: 'attached' });
});
it('elementHandle.waitForSelector should immediately resolve if node exists', async ({ page }) => {
await page.setContent(`extra
target
`);
const div = await page.$('div');
const span = await div.waitForSelector('span', { state: 'attached' });
expect(await span.evaluate(e => e.textContent)).toBe('target');
});
it('elementHandle.waitForSelector should wait', async ({ page }) => {
await page.setContent(``);
const div = await page.$('div');
const promise = div.waitForSelector('span', { state: 'attached' });
await div.evaluate(div => div.innerHTML = 'target');
const span = await promise;
expect(await span.evaluate(e => e.textContent)).toBe('target');
});
it('elementHandle.waitForSelector should timeout', async ({ page }) => {
await page.setContent(``);
const div = await page.$('div');
const error = await div.waitForSelector('span', { timeout: 100 }).catch(e => e);
expect(error.message).toContain('Timeout 100ms exceeded.');
});
it('elementHandle.waitForSelector should throw on navigation', async ({ page, server }) => {
await page.setContent(``);
const div = await page.$('div');
const promise = div.waitForSelector('span').catch(e => e);
// Give it some time before navigating.
for (let i = 0; i < 10; i++)
await page.evaluate(() => 1);
await page.goto(server.EMPTY_PAGE);
const error = await promise;
expect(error.message).toContain('Error: frame navigated while waiting for');
expect(error.message).toContain('"locator(\'span\')"');
});
it('should work with removed MutationObserver', async ({ page, server }) => {
await page.evaluate(() => delete window.MutationObserver);
const [handle] = await Promise.all([
page.waitForSelector('.zombo'),
page.setContent(`