Skip to content

feat(k6/browser): support or() locator#5723

Open
amontfaucon wants to merge 1 commit intografana:masterfrom
amontfaucon:feature/or-locator
Open

feat(k6/browser): support or() locator#5723
amontfaucon wants to merge 1 commit intografana:masterfrom
amontfaucon:feature/or-locator

Conversation

@amontfaucon
Copy link

@amontfaucon amontfaucon commented Mar 10, 2026

What?

Add support for or() in locators.

Why?

This is a feature supported in playwright and hinders reusability of scripts with k6. It is very useful in improving test stability (e.g. handle login screens, MFA prompt, cookie consent that may not trigger systematically).

Implementation is inspired from the one of playwright. Only or() was added for now to keep it simple since and() is easier to achieve through other means.

For example, with this PR one can do the following:

import { browser } from "k6/browser";
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';

export const options = {
    scenarios: {
        browse: {
            executor: "shared-iterations",
            iterations: 1,
            options: {
                browser: {
                    type: "chromium",
                },
            },
        },
    }
};

export default async function () {
    const page = await browser.newPage();

    const orSelector = page.locator("#using-k6-browser").or(page.locator("#using-k6"));

    await page.goto("https://grafana.com/docs/k6/latest/using-k6/");

    await expect(orSelector).toHaveText("Using k6");
    await expect(orSelector).not.toHaveText("Using k6 browser");
    await expect(orSelector).toHaveAttribute("id", "using-k6")

    await page.goto("https://grafana.com/docs/k6/latest/using-k6-browser/");

    await expect(orSelector).toHaveText("Using k6 browser");
    await expect(orSelector).not.toHaveText("Using k6");
    await expect(orSelector).toHaveAttribute("id", "using-k6-browser")
}

Note: this will require a change in the TS types definitions.

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)

@amontfaucon amontfaucon changed the title feat: support or() locator feat(k6/browser): support or() locator Mar 10, 2026
@cla-assistant
Copy link

cla-assistant bot commented Mar 10, 2026

CLA assistant check
All committers have signed the CLA.

@cla-assistant
Copy link

cla-assistant bot commented Mar 10, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@amontfaucon amontfaucon marked this pull request as ready for review March 10, 2026 22:15
@amontfaucon amontfaucon requested a review from a team as a code owner March 10, 2026 22:15
@amontfaucon amontfaucon requested review from ankur22 and szkiba and removed request for a team March 10, 2026 22:15
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.

1 participant