← Back to library
Cypressassertionsintermediatev10+

Get text from multiple elements

Collect inner text from a list of elements using .each() and assert the gathered array.

1it('blog tags include "playwright"', () => {
2  cy.visit('/blog');
3
4  const texts = [];
5
6  cy.get('[data-testid="tag"]')
7    .each(($el) => {
8      texts.push($el.text().trim()); // .text() returns the raw string
9    })
10    .then(() => {
11      expect(texts.length).to.be.greaterThan(0);
12      expect(texts).to.include('playwright');
13
14      // Assert no duplicate tags
15      const unique = [...new Set(texts)];
16      expect(unique.length).to.equal(texts.length);
17    });
18});
#text#each#array#collect#list