Skip to content
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

Expose k6/browser.Selector to the runtime #4210

Open
oleiade opened this issue Jan 21, 2025 · 1 comment
Open

Expose k6/browser.Selector to the runtime #4210

oleiade opened this issue Jan 21, 2025 · 1 comment

Comments

@oleiade
Copy link
Member

oleiade commented Jan 21, 2025

Feature Description

As I work on the upcoming k6 assertion library, I needed to be able to access a Locator's selector expression to be able to show it in test results. As I navigated the code and documentation I couldn't find a way to access it from the Runtime.

For illustration, what I'm looking to be able to do is something along the lines of the following:

await page.goto('http://localhost:8000');
const locator = page.locator("#toBeCheckedCheckbox");

// This is what I aim to do
console.log(`${locator.selector}`)  // Would print '#toBeCheckedCheckbox'

With the intent to be able to produce such output:

Image

Suggested Solution (optional)

I would like to suggest we make that field public, or at least have a getter for it, so that it's possible to print/reuse the locator selector expression in the output.

Already existing or connected issues / PRs (optional)

No response

@ankur22
Copy link
Contributor

ankur22 commented Jan 21, 2025

@oleiade I took a look at PW for some inspiration and I found this stackoverflow post. From it i discovered the _selector is a private variable of the Locator class. I was able to get this to work in PW:

    await page.goto('https://quickpizza.grafana.com/')

    const loc = page.locator('role=button[name="Pizza, Please!"]');

    console.log(loc._selector);

Which prints role=button[name="Pizza, Please!"].

This doesn't work with k6 browser though as we don't expose it anywhere during the mapping of Locator.

However, I personally don't like the fact that it's accessing a private member variable like this, i'd prefer a public API instead, a getter such as .selector() so it's obvious that it's readonly.

WDYT @inancgumus?

@inancgumus inancgumus added triage and removed triage labels Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants