In Cypress, you can use the .its('length') command to retrieve the count of selected elements. This command returns the number of selected elements, which is commonly used for assertions to verify the presence of a specific quantity of elements in the DOM.
Here is an example. Suppose we want to test a page containing multiple list items with the class .list-item:
javascriptdescribe('List items count test', () => { it('should have the correct number of .list-item elements', () => { // Visit your page cy.visit('your-page-url'); // Retrieve all .list-item elements and assert their length cy.get('.list-item').its('length').should('eq', 5); }); });
In this example, we expect the page to have 5 .list-item elements. .its('length') retrieves the count of .list-item elements, and .should('eq', 5) is used to assert that this count equals 5. If it does not equal 5, the test fails.
2024年6月29日 12:07 回复