Skip to content

[Feature]: Allow passing in data to Evaluate function on Locator object #35602

Description

@matt-kxs

🚀 Feature Request

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

Example

const nodeId= "5";
const someLocator = this.page.frameLocator("iframe[data-test-id='someId']").locator("[data-test-id='someOtherId']");
const position = await someLocator.evaluate(
	({ 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) };

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()));

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