Skip to content

Commit 4c5e2cd

Browse files
authored
fix(playwright): some wait funcs draw error due to switchTo iframe (#3918)
1 parent 5caa3ad commit 4c5e2cd

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

lib/helper/Playwright.js

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,23 @@ class Playwright extends Helper {
23802380
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
23812381
locator = new Locator(locator, 'css');
23822382
const context = await this._getContext();
2383-
const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'visible' });
2383+
let waiter;
2384+
let count = 0;
2385+
2386+
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
2387+
if (this.frame) {
2388+
do {
2389+
waiter = await this.frame.locator(buildLocatorString(locator)).first().isVisible();
2390+
await this.wait(1);
2391+
count += 1000;
2392+
if (waiter) break;
2393+
} while (count <= waitTimeout);
2394+
2395+
if (!waiter) throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec.`);
2396+
return;
2397+
}
2398+
2399+
waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'visible' });
23842400
return waiter.catch((err) => {
23852401
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${err.message}`);
23862402
});
@@ -2393,7 +2409,23 @@ class Playwright extends Helper {
23932409
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
23942410
locator = new Locator(locator, 'css');
23952411
const context = await this._getContext();
2396-
const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' });
2412+
let waiter;
2413+
let count = 0;
2414+
2415+
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
2416+
if (this.frame) {
2417+
do {
2418+
waiter = await this.frame.locator(buildLocatorString(locator)).first().isHidden();
2419+
await this.wait(1);
2420+
count += 1000;
2421+
if (waiter) break;
2422+
} while (count <= waitTimeout);
2423+
2424+
if (!waiter) throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec.`);
2425+
return;
2426+
}
2427+
2428+
waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' });
23972429
return waiter.catch((err) => {
23982430
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${err.message}`);
23992431
});
@@ -2406,6 +2438,23 @@ class Playwright extends Helper {
24062438
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
24072439
locator = new Locator(locator, 'css');
24082440
const context = await this._getContext();
2441+
2442+
let waiter;
2443+
let count = 0;
2444+
2445+
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
2446+
if (this.frame) {
2447+
do {
2448+
waiter = await this.frame.locator(buildLocatorString(locator)).first().isHidden();
2449+
await this.wait(1);
2450+
count += 1000;
2451+
if (waiter) break;
2452+
} while (count <= waitTimeout);
2453+
2454+
if (!waiter) throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec.`);
2455+
return;
2456+
}
2457+
24092458
return context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' }).catch((err) => {
24102459
throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec\n${err.message}`);
24112460
});
@@ -2487,12 +2536,17 @@ class Playwright extends Helper {
24872536
} else {
24882537
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
24892538
if (this.frame) {
2490-
const { setTimeout } = require('timers/promises');
2491-
await setTimeout(waitTimeout);
2492-
waiter = await this.frame.locator(`:has-text('${text}')`).first().isVisible();
2539+
let count = 0;
2540+
do {
2541+
waiter = await this.frame.locator(`:has-text('${text}')`).first().isVisible();
2542+
await this.wait(1);
2543+
count += 1000;
2544+
} while (count <= waitTimeout);
2545+
24932546
if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
24942547
return;
24952548
}
2549+
24962550
waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: waitTimeout });
24972551
}
24982552
return waiter.catch((err) => {

test/helper/Playwright_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ describe('Playwright', function () {
130130
});
131131
});
132132

133+
describe('#waitForVisible #waitForInvisible - within block', () => {
134+
it('should wait for visible element', async () => {
135+
await I.amOnPage('/iframe');
136+
await I._withinBegin({
137+
frame: '#number-frame-1234',
138+
});
139+
140+
await I.waitForVisible('h1');
141+
});
142+
143+
it('should wait for invisible element', async () => {
144+
await I.amOnPage('/iframe');
145+
await I._withinBegin({
146+
frame: '#number-frame-1234',
147+
});
148+
149+
await I.waitForInvisible('h9');
150+
});
151+
152+
it('should wait for element to hide', async () => {
153+
await I.amOnPage('/iframe');
154+
await I._withinBegin({
155+
frame: '#number-frame-1234',
156+
});
157+
158+
await I.waitToHide('h9');
159+
});
160+
});
161+
133162
describe('#waitToHide', () => {
134163
it('should wait for hidden element', () => {
135164
return I.amOnPage('/form/wait_invisible')

0 commit comments

Comments
 (0)