Skip to content

Commit ba1a1bd

Browse files
committed
cherry-pick(#18819): fix(locators): frameLocator().nth serialized correctly
Fixes #18798.
1 parent 09c2d89 commit ba1a1bd

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

packages/playwright-core/src/server/frames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ export class Frame extends SdkObject {
16671667
for (let i = 0; i < frameChunks.length - 1 && progress.isRunning(); ++i) {
16681668
const info = this._page.parseSelector(frameChunks[i], options);
16691669
const task = dom.waitForSelectorTask(info, 'attached', false, i === 0 ? scope : undefined);
1670-
progress.log(` waiting for frameLocator('${stringifySelector(frameChunks[i])}')`);
1670+
progress.log(` waiting for ${this._asLocator(stringifySelector(frameChunks[i]) + ' >> internal:control=enter-frame')}`);
16711671
const handle = i === 0 && scope ? await frame._runWaitForSelectorTaskOnce(progress, stringifySelector(info.parsed), info.world, task)
16721672
: await frame._scheduleRerunnableHandleTask(progress, info.world, task);
16731673
const element = handle.asElement() as dom.ElementHandle<Element>;

packages/playwright-core/src/server/isomorphic/locatorGenerators.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ export function asLocator(lang: Language, selector: string, isFrameLocator: bool
3232
}
3333

3434
function innerAsLocator(factory: LocatorFactory, parsed: ParsedSelector, isFrameLocator: boolean = false): string {
35+
const parts = [...parsed.parts];
36+
// frameLocator('iframe').first is actually "iframe >> nth=0 >> internal:control=enter-frame"
37+
// To make it easier to parse, we turn it into "iframe >> internal:control=enter-frame >> nth=0"
38+
for (let index = 0; index < parts.length - 1; index++) {
39+
if (parts[index].name === 'nth' && parts[index + 1].name === 'internal:control' && (parts[index + 1].body as string) === 'enter-frame') {
40+
// Swap nth and enter-frame.
41+
const [nth] = parts.splice(index, 1);
42+
parts.splice(index + 1, 0, nth);
43+
}
44+
}
45+
3546
const tokens: string[] = [];
3647
let nextBase: LocatorBase = isFrameLocator ? 'frame-locator' : 'page';
37-
for (let index = 0; index < parsed.parts.length; index++) {
38-
const part = parsed.parts[index];
48+
for (let index = 0; index < parts.length; index++) {
49+
const part = parts[index];
3950
const base = nextBase;
4051
nextBase = 'locator';
4152

@@ -111,7 +122,7 @@ function innerAsLocator(factory: LocatorFactory, parsed: ParsedSelector, isFrame
111122

112123
let locatorType: LocatorType = 'default';
113124

114-
const nextPart = parsed.parts[index + 1];
125+
const nextPart = parts[index + 1];
115126
if (nextPart && nextPart.name === 'internal:control' && (nextPart.body as string) === 'enter-frame') {
116127
locatorType = 'frame';
117128
nextBase = 'frame-locator';

packages/playwright-core/src/server/isomorphic/locatorParser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,19 @@ function transform(template: string, params: TemplateParams, testIdAttributeName
152152
.replace(/,exact=true/g, 's')
153153
.replace(/\,/g, '][');
154154

155+
const parts = template.split('.');
156+
// Turn "internal:control=enter-frame >> nth=0" into "nth=0 >> internal:control=enter-frame"
157+
// because these are swapped in locators vs selectors.
158+
for (let index = 0; index < parts.length - 1; index++) {
159+
if (parts[index] === 'internal:control=enter-frame' && parts[index + 1].startsWith('nth=')) {
160+
// Swap nth and enter-frame.
161+
const [nth] = parts.splice(index, 1);
162+
parts.splice(index + 1, 0, nth);
163+
}
164+
}
165+
155166
// Substitute params.
156-
return template.split('.').map(t => {
167+
return parts.map(t => {
157168
if (!t.startsWith('internal:') || t === 'internal:control')
158169
return t.replace(/\$(\d+)/g, (_, ordinal) => { const param = params[+ordinal - 1]; return param.text; });
159170
t = t.includes('[') ? t.replace(/\]/, '') + ']' : t;

tests/library/locator-generator.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,14 @@ it('reverse engineer frameLocator', async ({ page }) => {
316316
const locator = page
317317
.frameLocator('iframe')
318318
.getByText('foo', { exact: true })
319-
.frameLocator('frame')
319+
.frameLocator('frame').first()
320320
.frameLocator('iframe')
321321
.locator('span');
322322
expect.soft(generate(locator)).toEqual({
323-
csharp: `FrameLocator("iframe").GetByText("foo", new() { Exact = true }).FrameLocator("frame").FrameLocator("iframe").Locator("span")`,
324-
java: `frameLocator("iframe").getByText("foo", new FrameLocator.GetByTextOptions().setExact(true)).frameLocator("frame").frameLocator("iframe").locator("span")`,
325-
javascript: `frameLocator('iframe').getByText('foo', { exact: true }).frameLocator('frame').frameLocator('iframe').locator('span')`,
326-
python: `frame_locator("iframe").get_by_text("foo", exact=True).frame_locator("frame").frame_locator("iframe").locator("span")`,
323+
csharp: `FrameLocator("iframe").GetByText("foo", new() { Exact = true }).FrameLocator("frame").First.FrameLocator("iframe").Locator("span")`,
324+
java: `frameLocator("iframe").getByText("foo", new FrameLocator.GetByTextOptions().setExact(true)).frameLocator("frame").first().frameLocator("iframe").locator("span")`,
325+
javascript: `frameLocator('iframe').getByText('foo', { exact: true }).frameLocator('frame').first().frameLocator('iframe').locator('span')`,
326+
python: `frame_locator("iframe").get_by_text("foo", exact=True).frame_locator("frame").first.frame_locator("iframe").locator("span")`,
327327
});
328328

329329
// Note that frame locators with ">>" are not restored back due to ambiguity.

tests/page/locator-frame.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ it('should work for $ and $$', async ({ page, server }) => {
9797

9898
it('should wait for frame', async ({ page, server }) => {
9999
await page.goto(server.EMPTY_PAGE);
100-
const error = await page.frameLocator('iframe').locator('span').click({ timeout: 1000 }).catch(e => e);
101-
expect(error.message).toContain('waiting for frameLocator(\'iframe\')');
100+
const error = await page.locator('body').frameLocator('iframe').locator('span').click({ timeout: 1000 }).catch(e => e);
101+
expect(error.message).toContain(`waiting for locator('body').frameLocator('iframe')`);
102102
});
103103

104104
it('should wait for frame 2', async ({ page, server }) => {

0 commit comments

Comments
 (0)