k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Implement page.getByText(text: string, options?)

Open ankur22 opened this issue 6 months ago • 0 comments
trafficstars

Feature Description

With getByText, k6 Browser would let scripts locate any DOM element by its visible text content: for example, page.getByText('Learn more'). The API should support matching only fully visible text, optionally normalize whitespace, and accept regex for partial or case-insensitive searches. Internally, k6 would traverse text nodes in the rendered DOM, filter hidden elements, and apply the chosen matcher. This feature dramatically simplifies tasks like verifying labels, links, or messages without writing low-level selectors.

Playwright docs: https://playwright.dev/docs/api/class-page#page-get-by-text

Note, this is only to implement the API on page.

This should work after the implementation work:

  await page.setContent(`
    <html><body>
      <a href="#about">Learn more</a>
      <div id="about">About section</div>
      <script>
        document.querySelector('a').addEventListener('click', e => {
          e.preventDefault();
          document.getElementById('about').style.background = 'yellow';
        });
      </script>
    </body></html>
  `);
  await page.getByText('Learn more').click();
  const about = page.getByText('About section');

Suggested Solution (optional)

No response

Already existing or connected issues / PRs (optional)

https://github.com/grafana/k6/issues/4248

ankur22 avatar May 15 '25 12:05 ankur22