Skip to content

Commit

Permalink
fix: switch to
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Sep 30, 2023
1 parent c27a955 commit 9a2e3ba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
43 changes: 17 additions & 26 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1882,11 +1882,11 @@ class Playwright extends Helper {
* @returns {Promise<any>}
*/
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]);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
11 changes: 9 additions & 2 deletions test/data/app/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function GET() {
include __DIR__ . '/view/image.php';
}
}


class cookies {

Expand Down Expand Up @@ -177,6 +177,13 @@ public function GET()
}
}

class iframes {
public function GET()
{
include __DIR__.'/view/iframes.php';
}
}

class iframe_nested {
public function GET()
{
Expand Down Expand Up @@ -311,4 +318,4 @@ class basic_auth {
function GET() {
include __DIR__.'/view/basic_auth.php';
}
}
}
1 change: 1 addition & 0 deletions test/data/app/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'/external_url' => 'external_url',
'/spinner' => 'spinner',
'/iframe' => 'iframe',
'/iframes' => 'iframes',
'/iframe_nested' => 'iframe_nested',
'/dynamic' => 'dynamic',
'/timeout' => 'timeout',
Expand Down
12 changes: 12 additions & 0 deletions test/data/app/view/iframes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<div class="box"><iframe name="content1" src="https://codecept.io" id="number-frame-5678" width="100%"> </iframe> </div>
<div class="box"><iframe name="content2" src="info" id="number-frame-1234" width="100%"/></div>

</body>
</html>
6 changes: 6 additions & 0 deletions test/helper/Playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ describe('Playwright', function () {
I.see('Information');
I.see('Lots of valuable data here');
});

it('should switch to iframe using css when there are more than one iframes', () => {
I.amOnPage('/iframes');
I.switchTo('iframe#number-frame-1234');
I.see('Information');
});
});

describe('#seeInSource, #grabSource', () => {
Expand Down

0 comments on commit 9a2e3ba

Please sign in to comment.