Skip to content

Add getByPlaceholder in the browser module #4904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 24, 2025
Merged

Conversation

ankur22
Copy link
Contributor

@ankur22 ankur22 commented Jul 9, 2025

What?

This adds a convenience method on page to be able to select on elements with the placeholder attribute.

Why?

This is part of the story of adding getBy*, which makes working with selectors a little easier. We used to have to work with the page.locator API providing either a CSS selector or a XPath selector if we wanted to get the element by the placeholder attribute on an element:

<input placeholder="Enter your name">
const l = page.locator('input[placeholder="Enter your name"]'); // CSS
const l = page.locator('//input[@placeholder="Enter your name"]'); // XPath

Now we can use:

const l = page.getByPlaceholder('Enter your name');

Working with getBy* in general is an industry standard in the frontend testing world.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

#4790, #4248

@ankur22 ankur22 added this to the v1.2.0 milestone Jul 9, 2025
@ankur22 ankur22 changed the base branch from master to add/getByLabel July 9, 2025 13:53
@ankur22 ankur22 force-pushed the add/getByLabel branch 5 times, most recently from 84c6eb3 to db24587 Compare July 9, 2025 15:27
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch 3 times, most recently from 4b7c297 to d5337e0 Compare July 9, 2025 15:46
@ankur22 ankur22 added area: browser browser: playwright related to Playwright compatibility labels Jul 18, 2025
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch from d5337e0 to 7d3eda9 Compare July 21, 2025 08:31
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch 4 times, most recently from 2b664ad to bc19851 Compare July 21, 2025 08:59
@ankur22 ankur22 marked this pull request as ready for review July 21, 2025 09:29
@ankur22 ankur22 requested a review from a team as a code owner July 21, 2025 09:29
@ankur22 ankur22 requested review from oleiade and codebien and removed request for a team July 21, 2025 09:29
@ankur22 ankur22 force-pushed the add/getByLabel branch 2 times, most recently from 452dedb to 5e51b17 Compare July 21, 2025 09:34
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch from bc19851 to 9629a7a Compare July 21, 2025 09:35
codebien
codebien previously approved these changes Jul 22, 2025
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

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

LGTM

// buildAttributeSelector is a helper method that builds an attribute selector
// for use with the internal:attr engine. It handles quoted strings and
// applies the appropriate suffix for exact or case-insensitive matching.
func (f *Frame) buildAttributeSelector(attrName, attrValue string, opts *GetByBaseOptions) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the expectation on this? If this used with high frequency, I expect we can use a strings builder with a bigger capacity and avoiding re-allocating the string in the case the execution joins the isQuoted branch.

If we need to apply it, then it would require a quick benchmark of buildAttribtuteSelector func.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll work with strings builder, but not in this PR. I've added it as an action item in a catch all issue that I'll work on once all the getBy* PRs are merged in.

oleiade
oleiade previously approved these changes Jul 22, 2025
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch from 9629a7a to bdf1492 Compare July 24, 2025 08:08
Base automatically changed from add/getByLabel to master July 24, 2025 08:14
@ankur22 ankur22 dismissed stale reviews from oleiade and codebien July 24, 2025 08:14

The base branch was changed.

ankur22 added 6 commits July 24, 2025 09:15
This will be used by a few of the getBy* APIs which are basically
wrappers around the attribute engine.
Use buildAttributeSelector in getByAltText and getByPlaceholder.
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch from bdf1492 to c2e8497 Compare July 24, 2025 08:15
@ankur22 ankur22 requested review from oleiade and codebien July 24, 2025 08:16
@ankur22 ankur22 merged commit 1eaa382 into master Jul 24, 2025
38 of 39 checks passed
@ankur22 ankur22 deleted the add/getByPlaceholder branch July 24, 2025 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: browser browser: playwright related to Playwright compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants