k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Implement page.getByTitle(titleText: string, options?)

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

Feature Description

Implementing getByTitle enables selecting elements via their title attribute—helpful for tooltips, icons, or elements with semantic hints. For instance, page.getByTitle('Settings') would locate <button title="Settings">. The matcher should allow different matching strategies (exact, substring, regex) and consider elements that expose title via ARIA (aria-label). Adding this API completes the suite of attribute-based selectors, giving k6 Browser users a consistent, Playwright-inspired interface for DOM querying.

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

Note: this is only to be implemented on the page.

This should work after the implementation work:

  await page.setContent(`
    <html><body>
      <button title="Settings">⚙️</button>
      <div id="prefs" hidden>Preferences</div>
      <script>
        document.querySelector('button').addEventListener('click', () => {
          const prefs = document.getElementById('prefs');
          prefs.hidden = !prefs.hidden;
        });
      </script>
    </body></html>
  `);
  const settingsBtn = page.getByTitle('Settings');
  // click to toggle preferences
  await settingsBtn.click();
  const prefs = page.getByText('Preferences');

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