From cbb069c23c035edacafd3e1551a506c85793a6e1 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 26 Sep 2023 11:04:05 +0200 Subject: [PATCH 01/12] fix: switchTo --- lib/helper/Playwright.js | 2 +- test/helper/Playwright_test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index a1b7837e9..a738b4227 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2558,7 +2558,7 @@ class Playwright extends Helper { // get content of the first iframe locator = new Locator(locator, 'css'); - if ((locator.frame && locator.frame === 'iframe') || locator.value.toLowerCase() === 'iframe') { + if ((locator.frame && locator.frame === 'iframe') || locator.value.toLowerCase() === 'iframe' || locator.value.toLowerCase().includes('iframe')) { contentFrame = await this.page.frames()[1]; // get content of the iframe using its name } else if (locator.value.toLowerCase().includes('name=')) { diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index 8f181603b..b2a72ca33 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -335,6 +335,13 @@ describe('Playwright', function () { I.switchTo(null); I.see('Iframe test'); }); + + it('should switch to iframe using css', async () => { + I.amOnPage('/iframe'); + I.switchTo('iframe#number-frame-1234'); + I.see('Information'); + I.see('Lots of valuable data here'); + }); }); describe('#seeInSource, #grabSource', () => { From 9a2e3ba8e6b27b74e6e8dac49ac31681cc9dcfa2 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Sat, 30 Sep 2023 06:36:19 +0200 Subject: [PATCH 02/12] fix: switch to --- lib/helper/Playwright.js | 43 ++++++++++++++-------------------- test/data/app/controllers.php | 11 +++++++-- test/data/app/index.php | 1 + test/data/app/view/iframes.php | 12 ++++++++++ test/helper/Playwright_test.js | 6 +++++ 5 files changed, 45 insertions(+), 28 deletions(-) create mode 100755 test/data/app/view/iframes.php diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index a738b4227..277b44531 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -827,7 +827,7 @@ class Playwright extends Helper { async _stopBrowser() { this.withinLocator = null; - this._setPage(null); + await this._setPage(null); this.context = null; popupStore.clear(); await this.browser.close(); @@ -1882,11 +1882,11 @@ class Playwright extends Helper { * @returns {Promise} */ async executeScript(fn, arg) { - let context = this.page; - if (this.context && this.context.constructor.name === 'Frame') { - context = this.context; // switching to iframe context + if (this.context && this.context.constructor.name === 'FrameLocator') { + // switching to iframe context + return this.context.locator(':root').evaluate(fn, arg); } - return context.evaluate.apply(context, [fn, arg]); + return this.page.evaluate.apply(this.page, [fn, arg]); } /** @@ -2408,7 +2408,7 @@ class Playwright extends Helper { } async _getContext() { - if (this.context && this.context.constructor.name === 'Frame') { + if (this.context && this.context.constructor.name === 'FrameLocator') { return this.context; } return this.page; @@ -2535,14 +2535,13 @@ class Playwright extends Helper { } if (locator >= 0 && locator < childFrames.length) { - this.context = childFrames[locator]; + this.context = await this.page.frameLocator('iframe').nth(locator); this.contextLocator = locator; } else { throw new Error('Element #invalidIframeSelector was not found by text|CSS|XPath'); } return; } - let contentFrame; if (!locator) { this.context = await this.page.frames()[0]; @@ -2551,21 +2550,17 @@ class Playwright extends Helper { } // iframe by selector - const els = await this._locate(locator); - if (!els[0]) { - throw new Error(`Element ${JSON.stringify(locator)} was not found by text|CSS|XPath`); - } + locator = buildLocatorString(new Locator(locator, 'css')); + const frame = await this._locateElement(locator); - // get content of the first iframe - locator = new Locator(locator, 'css'); - if ((locator.frame && locator.frame === 'iframe') || locator.value.toLowerCase() === 'iframe' || locator.value.toLowerCase().includes('iframe')) { - contentFrame = await this.page.frames()[1]; - // get content of the iframe using its name - } else if (locator.value.toLowerCase().includes('name=')) { - const frameName = locator.value.split('=')[1].replace(/"/g, '').replaceAll(/]/g, ''); - contentFrame = await this.page.frame(frameName); + if (!frame) { + throw new Error(`Frame ${JSON.stringify(locator)} was not found by text|CSS|XPath`); } + this.frame = await this.page.frameLocator(locator); + + const contentFrame = this.frame; + if (contentFrame) { this.context = contentFrame; this.contextLocator = null; @@ -3340,13 +3335,9 @@ async function proceedSee(assertType, text, context, strict = false) { let allText; if (!context) { - let el = await this.context; - if (el && !el.getProperty) { - // Fallback to body - el = await this.page.$('body'); - } + const el = await this.context; - allText = [await el.innerText()]; + allText = [await el.locator('body').innerText()]; description = 'web application'; } else { const locator = new Locator(context, 'css'); diff --git a/test/data/app/controllers.php b/test/data/app/controllers.php index 4e2b6e472..2dea9b435 100755 --- a/test/data/app/controllers.php +++ b/test/data/app/controllers.php @@ -138,7 +138,7 @@ function GET() { include __DIR__ . '/view/image.php'; } } - + class cookies { @@ -177,6 +177,13 @@ public function GET() } } +class iframes { + public function GET() + { + include __DIR__.'/view/iframes.php'; + } +} + class iframe_nested { public function GET() { @@ -311,4 +318,4 @@ class basic_auth { function GET() { include __DIR__.'/view/basic_auth.php'; } -} \ No newline at end of file +} diff --git a/test/data/app/index.php b/test/data/app/index.php index d963a6fb3..6ab0b28a6 100755 --- a/test/data/app/index.php +++ b/test/data/app/index.php @@ -39,6 +39,7 @@ '/external_url' => 'external_url', '/spinner' => 'spinner', '/iframe' => 'iframe', + '/iframes' => 'iframes', '/iframe_nested' => 'iframe_nested', '/dynamic' => 'dynamic', '/timeout' => 'timeout', diff --git a/test/data/app/view/iframes.php b/test/data/app/view/iframes.php new file mode 100755 index 000000000..4d097d939 --- /dev/null +++ b/test/data/app/view/iframes.php @@ -0,0 +1,12 @@ + + + + + + + +
+