Skip to content

Commit 30fa1e9

Browse files
authored
fix: switchTo throws error when passing strict locator (#3847)
* fix: switchTo throws error when passing css locator * fix: switchTo * fix: improvements for within * fix: puppeteer within issue
1 parent 2bf4d37 commit 30fa1e9

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

lib/helper/Playwright.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,8 @@ class Playwright extends Helper {
850850
await this.switchTo(null);
851851
return frame.reduce((p, frameLocator) => p.then(() => this.switchTo(frameLocator)), Promise.resolve());
852852
}
853-
await this.switchTo(locator);
854-
this.withinLocator = new Locator(locator);
853+
await this.switchTo(frame);
854+
this.withinLocator = new Locator(frame);
855855
return;
856856
}
857857

@@ -2552,22 +2552,25 @@ class Playwright extends Helper {
25522552

25532553
// iframe by selector
25542554
const els = await this._locate(locator);
2555-
// assertElementExists(els, locator);
2555+
if (!els[0]) {
2556+
throw new Error(`Element ${JSON.stringify(locator)} was not found by text|CSS|XPath`);
2557+
}
25562558

25572559
// get content of the first iframe
2558-
if ((locator.frame && locator.frame === 'iframe') || locator.toLowerCase() === 'iframe') {
2560+
locator = new Locator(locator, 'css');
2561+
if ((locator.frame && locator.frame === 'iframe') || locator.value.toLowerCase() === 'iframe') {
25592562
contentFrame = await this.page.frames()[1];
25602563
// get content of the iframe using its name
2561-
} else if (locator.toLowerCase().includes('name=')) {
2562-
const frameName = locator.split('=')[1].replace(/"/g, '').replaceAll(/]/g, '');
2564+
} else if (locator.value.toLowerCase().includes('name=')) {
2565+
const frameName = locator.value.split('=')[1].replace(/"/g, '').replaceAll(/]/g, '');
25632566
contentFrame = await this.page.frame(frameName);
25642567
}
25652568

25662569
if (contentFrame) {
25672570
this.context = contentFrame;
25682571
this.contextLocator = null;
25692572
} else {
2570-
this.context = els[0];
2573+
this.context = this.page.frame(this.page.frames()[1].name());
25712574
this.contextLocator = locator;
25722575
}
25732576
}
@@ -3515,7 +3518,10 @@ async function elementSelected(element) {
35153518

35163519
function isFrameLocator(locator) {
35173520
locator = new Locator(locator);
3518-
if (locator.isFrame()) return locator.value;
3521+
if (locator.isFrame()) {
3522+
const _locator = new Locator(locator.value);
3523+
return _locator.value;
3524+
}
35193525
return false;
35203526
}
35213527

lib/helper/Puppeteer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ class Puppeteer extends Helper {
604604
return this.switchTo(null)
605605
.then(() => frame.reduce((p, frameLocator) => p.then(() => this.switchTo(frameLocator)), Promise.resolve()));
606606
}
607-
await this.switchTo(locator);
608-
this.withinLocator = new Locator(locator);
607+
await this.switchTo(frame);
608+
this.withinLocator = new Locator(frame);
609609
return;
610610
}
611611

@@ -2606,7 +2606,10 @@ async function elementSelected(element) {
26062606

26072607
function isFrameLocator(locator) {
26082608
locator = new Locator(locator);
2609-
if (locator.isFrame()) return locator.value;
2609+
if (locator.isFrame()) {
2610+
const _locator = new Locator(locator);
2611+
return _locator.value;
2612+
}
26102613
return false;
26112614
}
26122615

test/data/app/view/iframe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<h1>Iframe test</h1>
99

10-
<iframe name="content" src="info" />
10+
<iframe name="content" src="info" id="number-frame-1234"/>
1111

1212
</body>
13-
</html>
13+
</html>

test/helper/webapi.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ module.exports.tests = function () {
455455
if (isHelper('TestCafe')) this.skip(); // TODO Not yet implemented
456456

457457
await I.amOnPage('/iframe');
458-
await I.switchTo('iframe');
458+
await I.switchTo({ css: 'iframe' });
459459
const val = await I.executeScript(() => document.getElementsByTagName('h1')[0].innerText);
460460
assert.equal(val, 'Information');
461461
});
@@ -1199,6 +1199,36 @@ module.exports.tests = function () {
11991199
if (!err) assert.fail('seen "Iframe test"');
12001200
}
12011201
});
1202+
1203+
it('within should respect context in see when using frame', async function () {
1204+
if (isHelper('TestCafe')) this.skip();
1205+
1206+
await I.amOnPage('/iframe');
1207+
await I._withinBegin({
1208+
frame: '#number-frame-1234',
1209+
});
1210+
1211+
try {
1212+
await I.see('Information');
1213+
} catch (err) {
1214+
if (!err) assert.fail('seen "Information"');
1215+
}
1216+
});
1217+
1218+
it('within should respect context in see when using frame with strict locator', async function () {
1219+
if (isHelper('TestCafe')) this.skip();
1220+
1221+
await I.amOnPage('/iframe');
1222+
await I._withinBegin({
1223+
frame: { css: '#number-frame-1234' },
1224+
});
1225+
1226+
try {
1227+
await I.see('Information');
1228+
} catch (err) {
1229+
if (!err) assert.fail('seen "Information"');
1230+
}
1231+
});
12021232
});
12031233

12041234
describe('scroll: #scrollTo, #scrollPageToTop, #scrollPageToBottom', () => {

0 commit comments

Comments
 (0)