Skip to content

Add getByLabel in the browser module #4890

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 12 commits into from
Jul 24, 2025
Merged

Add getByLabel in the browser module #4890

merged 12 commits into from
Jul 24, 2025

Conversation

ankur22
Copy link
Contributor

@ankur22 ankur22 commented Jun 26, 2025

What?

This adds a convenience method on page to be able to select on label elements or elements with the aria-label 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 (when the aria-label attribute was used) or a XPath selector if we wanted to get the element by the label:

  <label for="password-input">Password</label>
  <input id="password-input">
// Not possible in CSS
const l = page.locator('//label[text()="Password"]'); // XPath

Now we can use:

const l = page.getByLabel('Password');

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)

Updates: #4789, #4248

@ankur22 ankur22 added this to the v1.2.0 milestone Jun 26, 2025
@ankur22 ankur22 force-pushed the add/getByAltText branch from 0439d26 to 5accbc6 Compare July 9, 2025 14:43
@ankur22 ankur22 force-pushed the add/getByLabel branch 6 times, most recently from 84c6eb3 to db24587 Compare July 9, 2025 15:27
@ankur22 ankur22 force-pushed the add/getByAltText branch from 964ff70 to d9df382 Compare July 18, 2025 11:00
@ankur22 ankur22 force-pushed the add/getByAltText branch from e6bd37b to 9447147 Compare July 18, 2025 15:05
@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 codebien and AgnesToulet and removed request for a team July 21, 2025 09:29
@ankur22 ankur22 closed this Jul 21, 2025
@ankur22 ankur22 reopened this Jul 21, 2025
@ankur22 ankur22 force-pushed the add/getByAltText branch from 9447147 to 39f4b94 Compare July 21, 2025 09:30
@ankur22 ankur22 force-pushed the add/getByLabel branch 2 times, most recently from 452dedb to 5e51b17 Compare July 21, 2025 09:34
<input aria-label="Username">
<label for="password-input">Password</label>
<input id="password-input">
<input aria-label="abc123">
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<input aria-label="abc123">
<input aria-label="abc123">
<span
role="checkbox"
aria-checked="false"
tabindex="0"
aria-labelledby="tac"></span>
<span id="tac">I agree to the Terms and Conditions.</span>

They mention the aria-labelledby attribute in Playwright doc so here's an example from the mdn doc to add a test for it. I've tested it and it's working fine so I'm OK if you prefer to add it in a following PR.

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 add this to a new PR, thanks for spotting this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an issue to track this: #4956

AgnesToulet
AgnesToulet previously approved these changes Jul 21, 2025
codebien
codebien previously approved these changes Jul 22, 2025
var a string
switch v.ExportType() {
case reflect.TypeOf(string("")):
a = fmt.Sprintf("'%s'", v.String()) // Strings require quotes
case reflect.TypeOf(string("")): // text values require quotes
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a constant here? Or do we expect the compiler to optimize?

Copy link
Contributor Author

@ankur22 ankur22 Jul 23, 2025

Choose a reason for hiding this comment

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

I didn't think about that tbh. I would expect the compiler to optimize it. I can make it a const though. I've added it to the catch all issue which I will work on after all the getBy* PRs have been merged.

a = fmt.Sprintf("'%s'", v.String()) // Strings require quotes
case reflect.TypeOf(string("")): // text values require quotes
if doubleQuote {
a = fmt.Sprintf(`"%s"`, v.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
a = fmt.Sprintf(`"%s"`, v.String())
a = `"`+ v.String())+`"`

This should be more efficient

Copy link
Contributor Author

@ankur22 ankur22 Jul 23, 2025

Choose a reason for hiding this comment

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

I've added it to the catch all issue which I will work on after all the getBy* PRs have been merged.

func isQuotedText(s string) bool {
return len(s) > 1 && s[0] == '\'' && s[len(s)-1] == '\''
switch {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems you could simplify by removing len(s) > 0 and have a if statement in line outside with len(s) < return false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean like:

	if len(s) < 1 {
		return false
	}

	switch {
	case s[0] == '\'' && s[len(s)-1] == '\'':
		return true
	case s[0] == '"' && s[len(s)-1] == '"':
		return true
	}
	return false

If so, i've added it to the catch all issue to work on later 👍

Base automatically changed from add/getByAltText to master July 23, 2025 07:58
@ankur22 ankur22 dismissed stale reviews from codebien and AgnesToulet July 23, 2025 07:58

The base branch was changed.

@ankur22 ankur22 added area: browser browser: playwright related to Playwright compatibility labels Jul 23, 2025
ankur22 added 12 commits July 24, 2025 08:39
More than one getBy* API will use the same option.
This is to get the getByLabel API to work.
This interfaces with the code that was copied from Playwright. This is
also copied from Playwright.
Some getBy* APIs work with single quotes and some are better at working
with double quotes. getByLabel should work with double quotes.

This inconsistency seems to stem from the code copied from Playwright
itself. I would prefer not to change the copied injected script from
Playwright so that it is easier to copy over updates/fixes.
The quoted string could be either single quoted ("'") or double quoted
('"'). isQuotedText needs to be able to handle both.
Co-authored-by: Agnès Toulet
@ankur22 ankur22 merged commit a5bf321 into master Jul 24, 2025
36 of 39 checks passed
@ankur22 ankur22 deleted the add/getByLabel branch July 24, 2025 08:14
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.

3 participants