Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/codegen/browser/code/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function isBrowserScenario(scenario: ir.Scenario) {
case 'ReloadExpression':
case 'NewRoleLocatorExpression':
case 'RoleLocatorOptionsExpression':
case 'TextLocatorOptionsExpression':
case 'NewLabelLocatorExpression':
case 'NewCssLocatorExpression':
case 'NewAltTextLocatorExpression':
Expand Down
42 changes: 38 additions & 4 deletions src/codegen/browser/code/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,32 @@ function emitRoleLocatorOptionsExpression(
})
}

function emitTextLocatorOptionsExpression(
_context: ScenarioContext,
expression: ir.TextLocatorOptionsExpression
): ts.Expression {
const exact = expression.exact

return ObjectBuilder.from({
...(exact && { exact }),
})
}

function emitNewLabelLocatorExpression(
context: ScenarioContext,
expression: ir.NewLabelLocatorExpression
): ts.Expression {
const page = emitExpression(context, expression.page)
const text = emitExpression(context, expression.text)

const options =
expression.options !== null
? [emitExpression(context, expression.options)]
: []

return new ExpressionBuilder(page)
.member('getByLabel')
.call([text, ObjectBuilder.from({ exact: true })])
.call([text, ...options])
.done()
}

Expand All @@ -105,9 +121,14 @@ function emitNewAltTextLocatorExpression(
const page = emitExpression(context, expression.page)
const text = emitExpression(context, expression.text)

const options =
expression.options !== null
? [emitExpression(context, expression.options)]
: []

return new ExpressionBuilder(page)
.member('getByAltText')
.call([text, ObjectBuilder.from({ exact: true })])
.call([text, ...options])
.done()
}

Expand All @@ -118,9 +139,14 @@ function emitNewPlaceholderLocatorExpression(
const page = emitExpression(context, expression.page)
const text = emitExpression(context, expression.text)

const options =
expression.options !== null
? [emitExpression(context, expression.options)]
: []

return new ExpressionBuilder(page)
.member('getByPlaceholder')
.call([text, ObjectBuilder.from({ exact: true })])
.call([text, ...options])
.done()
}

Expand All @@ -131,9 +157,14 @@ function emitNewTitleLocatorExpression(
const page = emitExpression(context, expression.page)
const text = emitExpression(context, expression.text)

const options =
expression.options !== null
? [emitExpression(context, expression.options)]
: []

return new ExpressionBuilder(page)
.member('getByTitle')
.call([text, ObjectBuilder.from({ exact: true })])
.call([text, ...options])
.done()
}

Expand Down Expand Up @@ -457,6 +488,9 @@ function emitExpression(
case 'RoleLocatorOptionsExpression':
return emitRoleLocatorOptionsExpression(context, expression)

case 'TextLocatorOptionsExpression':
return emitTextLocatorOptionsExpression(context, expression)

case 'NewLabelLocatorExpression':
return emitNewLabelLocatorExpression(context, expression)

Expand Down
10 changes: 5 additions & 5 deletions src/codegen/browser/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ it('should emit a getByAltText locator', async ({ expect }) => {
nodeId: 'locator',
selector: {
type: 'alt',
text: 'Grot is happy',
text: { value: 'Grot is happy', exact: true },
},
inputs: { page: { nodeId: 'page' } },
},
Expand Down Expand Up @@ -1085,7 +1085,7 @@ it('should emit a getByLabel locator', async ({ expect }) => {
nodeId: 'locator',
selector: {
type: 'label',
text: 'Username',
text: { value: 'Username', exact: true },
},
inputs: { page: { nodeId: 'page' } },
},
Expand Down Expand Up @@ -1120,7 +1120,7 @@ it('should emit a getByPlaceholder locator', async ({ expect }) => {
nodeId: 'locator',
selector: {
type: 'placeholder',
text: 'Enter your email',
text: { value: 'Enter your email', exact: true },
},
inputs: { page: { nodeId: 'page' } },
},
Expand Down Expand Up @@ -1155,7 +1155,7 @@ it('should emit a getByTitle locator', async ({ expect }) => {
nodeId: 'locator',
selector: {
type: 'title',
text: 'Submit your form',
text: { value: 'Submit your form', exact: true },
},
inputs: { page: { nodeId: 'page' } },
},
Expand Down Expand Up @@ -1196,7 +1196,7 @@ it('should emit a waitFor statement', async ({ expect }) => {
nodeId: 'locator',
selector: {
type: 'title',
text: 'Submit your form',
text: { value: 'Submit your form', exact: true },
},
inputs: { page: { nodeId: 'page' } },
},
Expand Down
10 changes: 10 additions & 0 deletions src/codegen/browser/intermediate/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,33 @@ export interface NewAltTextLocatorExpression {
type: 'NewAltTextLocatorExpression'
text: Expression
page: Expression
options: Expression | null
}

export interface NewLabelLocatorExpression {
type: 'NewLabelLocatorExpression'
text: Expression
page: Expression
options: Expression | null
}

export interface NewPlaceholderLocatorExpression {
type: 'NewPlaceholderLocatorExpression'
text: Expression
page: Expression
options: Expression | null
}

export interface NewTitleLocatorExpression {
type: 'NewTitleLocatorExpression'
text: Expression
page: Expression
options: Expression | null
}

export interface TextLocatorOptionsExpression {
type: 'TextLocatorOptionsExpression'
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like TextBasedLocatorOptionsExpression is a better choice, since getByText exists?

exact?: boolean
}

export interface RoleLocatorOptionsExpression {
Expand Down Expand Up @@ -196,6 +205,7 @@ export type Expression =
| ClosePageExpression
| NewRoleLocatorExpression
| RoleLocatorOptionsExpression
| TextLocatorOptionsExpression
| NewLabelLocatorExpression
| NewPlaceholderLocatorExpression
| NewTitleLocatorExpression
Expand Down
32 changes: 28 additions & 4 deletions src/codegen/browser/intermediate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ function emitLocatorNode(context: IntermediateContext, node: m.LocatorNode) {
type: 'NewLabelLocatorExpression',
text: {
type: 'StringLiteral',
value: node.selector.text,
value: node.selector.text.value,
},
page,
options: node.selector.text.exact
? {
type: 'TextLocatorOptionsExpression',
exact: node.selector.text.exact,
}
: null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing saved locators silently lose exact matching behavior

Medium Severity

Previously, the code generation hardcoded { exact: true } for all text-based locators. Now it only emits { exact: true } when node.selector.text.exact is truthy. For existing saved ActionLocator data that predates this PR (which lacks the options field), toNodeSelector maps locator.options?.exact to undefined, causing the intermediate layer to set options: null. This silently changes the generated script from page.getByLabel("text", { exact: true }) to page.getByLabel("text"), switching from exact to substring matching in Playwright/k6 — a behavioral regression that could cause tests to match unintended elements.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 053e705. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, because it's not a user-facing feature yet. The user-facing recording-to-script conversion should not be affected.

})
break

Expand All @@ -102,9 +108,15 @@ function emitLocatorNode(context: IntermediateContext, node: m.LocatorNode) {
type: 'NewPlaceholderLocatorExpression',
text: {
type: 'StringLiteral',
value: node.selector.text,
value: node.selector.text.value,
},
page,
options: node.selector.text.exact
? {
type: 'TextLocatorOptionsExpression',
exact: node.selector.text.exact,
}
: null,
})
break

Expand All @@ -113,9 +125,15 @@ function emitLocatorNode(context: IntermediateContext, node: m.LocatorNode) {
type: 'NewTitleLocatorExpression',
text: {
type: 'StringLiteral',
value: node.selector.text,
value: node.selector.text.value,
},
page,
options: node.selector.text.exact
? {
type: 'TextLocatorOptionsExpression',
exact: node.selector.text.exact,
}
: null,
})
break

Expand All @@ -124,9 +142,15 @@ function emitLocatorNode(context: IntermediateContext, node: m.LocatorNode) {
type: 'NewAltTextLocatorExpression',
text: {
type: 'StringLiteral',
value: node.selector.text,
value: node.selector.text.value,
},
page,
options: node.selector.text.exact
? {
type: 'TextLocatorOptionsExpression',
exact: node.selector.text.exact,
}
: null,
})
break

Expand Down
15 changes: 14 additions & 1 deletion src/codegen/browser/intermediate/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function substituteExpression(
case 'ClickOptionsExpression':
case 'WaitForOptionsExpression':
case 'RoleLocatorOptionsExpression':
case 'TextLocatorOptionsExpression':
return node

case 'ClosePageExpression':
Expand Down Expand Up @@ -84,6 +85,9 @@ function substituteExpression(
type: 'NewLabelLocatorExpression',
text: substituteExpression(node.text, substitutions),
page: substituteExpression(node.page, substitutions),
options: node.options
? substituteExpression(node.options, substitutions)
: null,
}

case 'NewCssLocatorExpression':
Expand All @@ -96,22 +100,31 @@ function substituteExpression(
case 'NewAltTextLocatorExpression':
return {
type: 'NewAltTextLocatorExpression',
page: substituteExpression(node.page, substitutions),
text: substituteExpression(node.text, substitutions),
page: substituteExpression(node.page, substitutions),
options: node.options
? substituteExpression(node.options, substitutions)
: null,
}

case 'NewPlaceholderLocatorExpression':
return {
type: 'NewPlaceholderLocatorExpression',
text: substituteExpression(node.text, substitutions),
page: substituteExpression(node.page, substitutions),
options: node.options
? substituteExpression(node.options, substitutions)
: null,
}

case 'NewTitleLocatorExpression':
return {
type: 'NewTitleLocatorExpression',
text: substituteExpression(node.text, substitutions),
page: substituteExpression(node.page, substitutions),
options: node.options
? substituteExpression(node.options, substitutions)
: null,
}

case 'NewTestIdLocatorExpression':
Expand Down
Loading
Loading