Skip to content

Add getByText in the browser module #4912

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 28, 2025
Merged

Add getByText in the browser module #4912

merged 6 commits into from
Jul 28, 2025

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 text in the element.

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 text in the element:

<div>Hello World</div>
// Not available in CSS
const l = page.locator('//div[text()="Hello World"]'); // XPath

Now we can use:

const l = page.getByText('Hello World');

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)

#4792, #4248

@ankur22 ankur22 added this to the v1.2.0 milestone Jul 9, 2025
@ankur22 ankur22 force-pushed the add/getByTestId branch 2 times, most recently from 5f024e4 to e9a2230 Compare July 21, 2025 09:09
@ankur22 ankur22 force-pushed the add/getByText branch 3 times, most recently from a904656 to 17869e1 Compare July 21, 2025 09:14
@ankur22 ankur22 marked this pull request as ready for review July 21, 2025 09:30
@ankur22 ankur22 requested a review from a team as a code owner July 21, 2025 09:30
@ankur22 ankur22 requested review from codebien and joanlopez and removed request for a team July 21, 2025 09:30
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, mostly I have the same questions I submitted on the other pull request.

joanlopez
joanlopez previously approved these changes Jul 22, 2025
Base automatically changed from add/getByTestId to master July 25, 2025 12:33
@ankur22 ankur22 dismissed stale reviews from joanlopez and codebien July 25, 2025 12:33

The base branch was changed.

@ankur22 ankur22 merged commit 84a02b6 into master Jul 28, 2025
46 of 48 checks passed
@ankur22 ankur22 deleted the add/getByText branch July 28, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants