Skip to content

Commit

Permalink
fix(locator): do not explode locators (#34104)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Dec 19, 2024
1 parent b7a1cfd commit 04e670c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface LocatorFactory {
}

export function asLocator(lang: Language, selector: string, isFrameLocator: boolean = false): string {
return asLocators(lang, selector, isFrameLocator)[0];
return asLocators(lang, selector, isFrameLocator, 1)[0];
}

export function asLocators(lang: Language, selector: string, isFrameLocator: boolean = false, maxOutputSize = 20, preferredQuote?: Quote): string[] {
Expand Down Expand Up @@ -220,7 +220,7 @@ function combineTokens(factory: LocatorFactory, tokens: string[][], maxOutputSiz
const visit = (index: number) => {
if (index === tokens.length) {
result.push(factory.chainLocators(currentTokens));
return currentTokens.length < maxOutputSize;
return result.length < maxOutputSize;
}
for (const taken of tokens[index]) {
currentTokens[index] = taken;
Expand Down
24 changes: 24 additions & 0 deletions tests/library/locator-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,27 @@ it('parseLocator frames', async () => {
expect.soft(parseLocator('java', `locator("iframe").contentFrame().getByText("foo")`, '')).toBe(`iframe >> internal:control=enter-frame >> internal:text=\"foo\"i`);
expect.soft(parseLocator('java', `frameLocator("iframe").getByText("foo")`, '')).toBe(`iframe >> internal:control=enter-frame >> internal:text=\"foo\"i`);
});

it('should not oom in locator parser', async ({ page }) => {
const l = page.locator.bind(page);
const locator = page.locator('text=L1').or(l('text=L2').or(l('text=L3').or(l('text=L4')).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L5').or(l('text=L6'))).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L7')
.or(l('text=L8'))))).or(l('text=L9').or(l('text=L10').or(l('text=L11')).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L12').or(l('text=L13'))).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L14').or(l('text=L15'))))).or(l('text=L16').or(l('text=L17').or(l('text=L18')).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L19').or(l('text=L20'))).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L21').or(l('text=L22'))))).or(l('text=L23').or(l('text=L24').or(l('text=L25')).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L26').or(l('text=L27'))).or(l('#f0')
.contentFrame().locator('#f0_mid_0')
.contentFrame().locator('text=L28').or(l('text=L29')))));
const error = await locator.count().catch(e => e);
expect(error.message).toContain('Frame locators are not allowed inside composite locators');
});

0 comments on commit 04e670c

Please sign in to comment.