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

[Feature request] Add toHaveCount, assertion for count of elements #30

Closed
aha-oretama opened this issue Oct 23, 2020 · 10 comments
Closed
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@aha-oretama
Copy link

aha-oretama commented Oct 23, 2020

I need new assertion checking the count of elements which the selector matches.
It's very useful to verify the count of elements.

      // before
      await page.waitForSelector(selector);
      const count = await page.$$eval(selector, (el) => el.length);
      expect(count).toBe(1);

      // after
      await expect(page).toHaveCount(selector, 1)

It might be better to consider if the new assertion can verify the greater or less count.

await expect(page).toHaveCount(selector, '> 1')
await expect(page).toHaveGreaterThanCount(selector, 1)
@mxschmitt mxschmitt added enhancement New feature or request good first issue Good for newcomers labels Oct 23, 2020
@mxschmitt
Copy link
Member

Sounds useful!

@chilikasha
Copy link

I have a chat app and such an assertion would be very helpful

@mmarkelov
Copy link
Member

Should be released in new version. @mxschmitt could you please publish new one. I just don't have access to do it

@mxschmitt
Copy link
Member

Published 0.3.0 👍

@tarun1122
Copy link

tarun1122 commented Mar 26, 2022

I didn't see the above request implemented here: https://playwright.dev/docs/test-assertions#locator-assertions-to-have-count

await expect(this.questions).toHaveCount(1)

I want to validate if my list count is greater than 1.
How can I achieve this?

@mmarkelov @mxschmitt

@mskelton
Copy link
Member

@tarun1122 Not sure, but this might work:

await expect(this.questions).not.toHaveCount(0)
await expect(this.questions).not.toHaveCount(1)

As a side note, your question should probably be opened as an issue in the Playwright repo.

@tarun1122
Copy link

@mskelton Actually my list is dynamic. Sometimes it gives 2 questions sometimes 5+. I just need to ensure that my list always has atleast 1 question and hence, I want to check the list count is greater than 1.

@mskelton
Copy link
Member

mskelton commented Mar 26, 2022

Right, if you verify the count is not 0 and not 1, that then ensures there is at least two elements.

@douglaslb
Copy link

I have the same problem... I would like to write a test for a dynamic list, so sometimes my list has 5 elements, or more than 20... is there a way to test this?

@simon04
Copy link

simon04 commented Nov 27, 2022

@douglaslb, you might want to use locator.count:

await expect(await questions.count()).toBeGreaterThan(20);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

8 participants