Skip to content

Add getByTitle in the browser module #4910

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 4 commits into from
Jul 25, 2025
Merged

Add getByTitle in the browser module #4910

merged 4 commits into from
Jul 25, 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 title 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 title attribute on an element:

<div title="Information box">This is some information</div>
const l = page.locator('div[title="Information box"]'); // CSS
const l = page.locator('//div[@title="Information box"]'); // XPath

Now we can use:

const l = page.getByTitle('Information box');

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)

#4793, #4248

@ankur22 ankur22 added this to the v1.2.0 milestone Jul 9, 2025
@ankur22 ankur22 changed the base branch from master to add/getByPlaceholder July 9, 2025 19:48
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch 5 times, most recently from 2b664ad to bc19851 Compare July 21, 2025 08:59
@ankur22 ankur22 force-pushed the add/getByTitle branch 2 times, most recently from c373455 to 360ac31 Compare July 21, 2025 09:04
@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 oleiade and codebien and removed request for a team July 21, 2025 09:30
@ankur22 ankur22 force-pushed the add/getByPlaceholder branch from bc19851 to 9629a7a Compare July 21, 2025 09:35
oleiade
oleiade previously approved these changes Jul 21, 2025
Copy link
Contributor

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

This is awesome!!

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.

@ankur22 I don't see any concurrency primitive. Is this API synchronous?

Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, is there a specific reason why we use static instead of the idiomatic testdata folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea, it's been like that from the beginning as far as I can tell. Are there any advantages to working with testData over static?

Copy link
Contributor

Choose a reason for hiding this comment

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

From go help:

Directory and file names that begin with "." or "_" are ignored by the go tool, as are directories named "testdata".

AFAIK, this means static folder might be included in the built binary.

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 will add this to the catch all issue to explore once the getBy* PRs are all merged in 👍


l := p.GetByTitle(tt.title, tt.opts)
c, err := l.Count()
assert.NoError(t, err)
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
assert.NoError(t, err)
require.NoError(t, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this to force the test to stop if the assert fails?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is, I don't see the reason to continue running tests if they fail in a place where we don't expect.

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 will review all the asserts and make sure they are require where needed in the new tests. I have added an action point in the catch all issue to work on after all the getBy* APIs have been merged in.

@ankur22
Copy link
Contributor Author

ankur22 commented Jul 23, 2025

@ankur22 I don't see any concurrency primitive. Is this API synchronous?

That's correct, it is a synchronous method. When we call page.waitForTitle it builds a locator object and returns it to the caller. This is done without performing any long running IO (e.g. CDP requests to chromium) and is a very quick process.

However, when we call a method on the locator object (e.g. l.click()) that is when it performs the IO ops against chromium to perform the action. These methods are asynchronous.

codebien
codebien previously approved these changes Jul 23, 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 👍

@ankur22 ankur22 force-pushed the add/getByPlaceholder branch 2 times, most recently from bdf1492 to c2e8497 Compare July 24, 2025 08:15
Base automatically changed from add/getByPlaceholder to master July 24, 2025 10:50
@ankur22 ankur22 dismissed stale reviews from codebien and oleiade July 24, 2025 10:50

The base branch was changed.

@ankur22 ankur22 requested review from codebien and oleiade July 24, 2025 14:41
@ankur22 ankur22 merged commit 04e78bd into master Jul 25, 2025
46 of 49 checks passed
@ankur22 ankur22 deleted the add/getByTitle branch July 25, 2025 09:56
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