I the following example an iframe with the src myurl
will be injected in the page.
I can find the iframe dom element but without the explicit wait the page.frame
function returns null.
I guess this is because page.frame
does not wait for the iframe to be loaded.
How can i wait until the content of an iframe has been loaded?
await expect(page.locator('iframe')).toHaveCount(1);
await page.waitForTimeout(2000); // explicit wait should be removed
const frame = page.frame({url: new RegExp('.*/myurl.*', 'i')});
expect(frame).not.toBeNull();
Two possible solutions have been suggested by Yury Semikhatsky:
const frame = page.frames()[0];
await frame.waitForURL(new RegExp('.*/myurl.*', 'i'));
or like this:
const iframeElement = await page.locator('iframe').elementHandle()
const frame = await iframeElement.contentFrame();
await frame.waitForURL(new RegExp('.*/myurl.*', 'i'));