Skip to content

[Bug]: Passing in data to locator.evaluate does not work as expected #35643

Description

@matt-kxs

Version

1.51.0

Steps to reproduce

When using page.evaluate it is possible to pass in multiple values. When using someLocator.evaluate you can only pass in SVGElement | HTMLElement. I am requesting that we update locator.evaluate to work the same way as page.evaluate

const nodeId= "5";
const someLocator = this.page.frameLocator("iframe[data-test-id='someId']").locator("[data-test-id='someOtherId']");
const position = await someLocator.evaluate(
	({ nodeId }) => {
                console.log(nodeId)
		const cy = window["getCy"]();
		const cyElement = cy.getElementById(nodeId);
		if (cyElement !== undefined && cyElement.renderedPosition !== undefined) {
			const pos = cyElement.renderedPosition();
			return { x: pos.x, y: pos.y };
		}
		throw new Error(`Node with ID ${nodeId} not found or has no rendered position.`);
	},
	{ nodeId }
);
return { x: Math.round(position.x), y: Math.round(position.y) };

Note: I was told that locator should work this way in: #35602 so I'm logging a bug instead

Expected behavior

console.log(nodeId) should display the passed in value.

Actual behavior

nodeId is not defined because passing in data does not work in a locator context.

Additional context

The exact same code will work if I use page or the Frame object. It only does not work when using a Locator object.

Motivation
The product I'm testing has lots of iframes and nested iframes. I need an easy way to perform evaluate within the iframe context of a locator with the ability to optionally pass in data. My current workaround is to hard code the iframe locator and use evaluateHandle to convert back into a Frame object. The Frame object supports passing in variables to evaluate. This is harder to do when it's not easy or possible to determine the iframe locator at run-time (as far as I know there's no way to get a locator's frame). For example:

const frame = await getFrameFromLocator(this.page.locator("iframe:visible").first()));

Environment

System:
OS: Microsoft Windows 10 Enterprise
CPU: (8) Intel

Binaries:
Node: 22.14.0
npm: 10.9.2
npmPackages:
   @playwright/test: 1.51.0 => 1.51.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions