Skip to content

Add locator.count docs page #1963

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 2 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Locator can be created with the [page.locator(selector[, options])](https://graf
| [check([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/check) {{< docs/bwipt id="471" >}} | Select the input checkbox. |
| [clear([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/clear) | Clears text boxes and input fields of any existing values. |
| [click([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/click) {{< docs/bwipt id="471" >}} | Mouse click on the chosen element. |
| [count()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/count) | Returns the number of elements matching the selector. |
| [dblclick([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/dblclick) {{< docs/bwipt id="471" >}} | Mouse double click on the chosen element. |
| [dispatchEvent(type, eventInit, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/dispatchevent) | Dispatches HTML DOM event types e.g. `'click'`. |
| [fill(value, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/fill) | Fill an `input`, `textarea` or `contenteditable` element with the provided value. |
Expand Down
56 changes: 56 additions & 0 deletions docs/sources/k6/next/javascript-api/k6-browser/locator/count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: 'count()'
description: 'Browser module: locator.count method'
---

# count()

Returns the number of elements matching the selector. This does not wait for actionability checks (whether the matching elements are `visible`, `stable`, and `enabled`). If the return value from `count` doesn't match the expected count, then you should manually retry the API call.

### Returns

| Type | Description |
| ----------------- | --------------------------------------------------------------------------- |
| `Promise<number>` | A promise which resolves with the number of elements matching the selector. |

### Example

{{< code >}}

```javascript
import { expect } from 'https://jslib.k6.io/k6-testing/0.4.0/index.js';
import { browser } from 'k6/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();
await page.goto('https://quickpizza.grafana.com/login');

const expected = 3;
let match = false;
for (let i = 0; i < 5; i++) {
match = await page.locator('input').count();
if (match == expected) {
break;
}
}

expect(match).toEqual(expected);

await page.close();
}
```

{{< /code >}}
Loading