October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Test Lists of Items in Cypress

Use stable selectors and retryable Cypress assertions to check a list’s count, contents, and order—even when items render asynchronously.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a stable selector for each repeated item, then assert the expected list state with a retryable Cypress query. For example, cy.get('[data-cy="todo-item"]').should('have.length', 3) waits for three matching items to appear; add text and ordering checks when those are part of the requirement.

Choose a stable selector for list items

Give repeated elements a selector that describes their testing role, such as a dedicated data-cy attribute. This avoids tying the test to CSS classes that may change for styling reasons.

As an Amazon Associate I earn from qualifying purchases.

<ul>
  <li data-cy="todo-item">Buy milk</li>
  <li data-cy="todo-item">Walk the dog</li>
</ul>

Use the same selector in the test:

cy.get('[data-cy="todo-item"]')

Cypress’s cy.get() documentation covers selecting matching elements and using positional queries such as .first() and .eq().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Assert the expected number of items

Use .should('have.length', expectedCount) when the correct state has a known exact number of items:

#1 Best Overall
HP 14" HD Chromebook Laptop for Students, Intel Quad-Core N4120(> N4020), 4GB RAM, 64GB eMMC, WiFi, Webcam, HDMI, USB-A&C, 14 Hours Battery Life, Zoom, Chrome OS, CUE Accessories
  • Intel Celeron N4120: 4 Cores & Threads, 1.1GHz Base Clock, Up to 2.6GHz Boost Clock, 4MB Cache, Intel UHD Graphics 600. The perfect combination of performance, power consumption, and value helps your device handle multitasking smoothly and reliably with four processing cores to divide up the work.
cy.get('[data-cy="todo-item"]').should('have.length', 3)

Cypress retries linked queries and .should() assertions until they pass or time out. That makes this pattern useful for lists populated asynchronously: the assertion can pass when the third item is rendered instead of checking too early.

Prefer a positive assertion for the state you expect. A negative assertion can pass for unintended states—for example, a list that disappeared or contains an unexpected blank item—without proving that the correct items are present. If the requirement is a minimum or a range rather than an exact count, write an assertion that expresses that condition.

Check item text and ordering

Assert a few important positions

For a short list with position-specific requirements, use .first() or .eq(index) and assert the expected text:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('[data-cy="todo-item"]').first().should('contain', 'Buy milk')
cy.get('[data-cy="todo-item"]').eq(1).should('contain', 'Walk the dog')

Be precise about selector semantics. For example, ul li:first selects the first matching li across the page, while ul li:first-child can match the first child of each list.

Verify the entire ordered list

When membership, text, and order all matter, collect the rendered text and compare it with the expected array. Cypress demonstrates mapping selected elements to text and converting the result to a regular array before comparison in its introduction.

const expected = ['Walk the dog', 'Feed the cat', 'Write JavaScript']

cy.get('.connectors-list > li').should(($items) => {
  const actual = $items.map((_, item) => item.innerText.trim()).get()
  expect(actual).to.deep.equal(expected)
})

The callback form of .should() is retried. Keep it safe to run more than once: use assertions and pure inspection, not clicks, typing, or other side effects.

Rank #3
ASUS 2026 15" FHD IPS Chromebook, Intel Processor Up to 2.80GHz, 4GB DDR4, 128GB Storage, HDMI, Super-Fast WiFi, Chrome OS, Pastel Silver (Renewed)
  • Intel Processor Up to 2.80GHz, 4GB DDR4, 128GB Storage
  • 15" FHD IPS Display, Intel UHD Graphics
  • 1x USB Type C, 1 x USB Type A, 1x Headphone/Microphone Combo Jack, HDMI
  • Fast WiFi and Bluetooth, Integrated Webcam
  • Chrome OS, AC Charger Included, Pastel Silver

Wait for an asynchronously populated list

Keep the query and assertion in one retryable Cypress chain:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('[data-cy="todo-item"]').should('have.length', 3)

A linked query and its assertion are retried from the top of the query chain until the assertion succeeds or the command times out. By contrast, a .then() callback runs once with the elements found at that moment:

// Avoid this if the list is still being filled asynchronously
cy.get('[data-cy="todo-item"]').then(($items) => {
  expect($items).to.have.length(3)
})

Use .then() when you need to act on a value after it is available, not as a substitute for a retryable assertion about a changing DOM. See Cypress’s retry-ability guide and .should() documentation for the behavior and callback rules.

Rank #4
Sale
Lenovo Chromebook 2-in-1 - Lightweight Laptop - Google Gemini - Intel® N150 CPU - 14" WUXGA IPS Touchscreen Display - 4GB RAM - 128GB UFS Storage - Integrated Intel® Graphics - Luna Grey
  • THE BETTER WAY TO LAPTOP – Imagine a Chromebook that’s as flexible as your day: thin and lightweight with built-in Google apps and stress-free security.
  • TAKE HITS KEEP MOVING – Sleek, light, and built to last- the Chromebook 2-in-1 is just 0.69” thick and 3.3lbs. Enjoy long-lasting battery life, fast charging, and military-grade durability for nonstop productivity wherever life takes you.
  • PERFORMANCE THAT MATCHES YOUR HUSTLE – Fuel your ideas with an Intel Core processor and 128GB storage. Boot up in under 10 seconds to start the day powerfully efficient.
  • FLEX YOUR CREATIVITY ANYWHERE, ANYTIME – Create, work, or unwind your way with a versatile 2-in-1 design. Flip easily between laptop, tent, and tablet modes with a responsive touchscreen built for flexibility.
  • BRILLIANT VIEWS AND IMMERSIVE AUDIO – See, hear, and create with awesome clarity. The WUXGA display brings rich detail to your work and play, while audio tuned by Waves MaxxAudio provides immersive, balanced sound.

Use fixtures for predictable expected rows

Fixtures are useful when a list should render a known set of records. They can hold test data separately from the spec and can be used to stub a network response, reducing reliance on changing backend data.

cy.fixture('users.json').then((users) => {
  cy.get('[data-cy="user-row"]').should('have.length', users.length)
  cy.get('[data-cy="user-email"]')
    .first()
    .should('have.text', users[0].email)
})

Cypress describes fixtures as a way to keep test data consistent between runs in its fixture documentation. In TypeScript, a type supplied to cy.fixture<User[]>() describes the yielded value; it does not validate that the fixture file actually matches that type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate tests from records when needed

cy.fixture() runs within a test. It does not load data early enough to create new it() blocks. If you want one test per record, import the data when defining the tests. For large files or Node-side processing, Cypress recommends using cy.task() and returning only the data the test needs; see Writing and organizing tests.

Best Value
HP Chromebook 14 Laptop, Intel Celeron N4120, 4 GB RAM, 64 GB eMMC, 14" HD Display, Chrome OS, Thin Design, 4K Graphics, Long Battery Life, Ash Gray Keyboard (14a-na0226nr, 2022, Mineral Silver)
  • FOR HOME, WORK, & SCHOOL – With an Intel processor, 14-inch display, custom-tuned stereo speakers, and long battery life, this Chromebook laptop lets you knock out any assignment or binge-watch your favorite shows..Voltage:5.0 volts
  • HD DISPLAY, PORTABLE DESIGN – See every bit of detail on this micro-edge, anti-glare, 14-inch HD (1366 x 768) display (1); easily take this thin and lightweight laptop PC from room to room, on trips, or in a backpack.
  • ALL-DAY PERFORMANCE – Reliably tackle all your assignments at once with the quad-core, Intel Celeron N4120—the perfect processor for performance, power consumption, and value (2).
  • 4K READY – Smoothly stream 4K content and play your favorite next-gen games with Intel UHD Graphics 600 (3) (4).
  • MEMORY AND STORAGE – Enjoy a boost to your system’s performance with 4 GB of RAM while saving more of your favorite memories with 64 GB of reliable flash-based eMMC storage (5).
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Pick the assertion pattern that matches the requirement

Need Pattern What it establishes
Confirm collection size .should('have.length', n) Exact expected cardinality, with retries while the list renders.
Check a few meaningful positions .first() or .eq(i) plus a text assertion Readable checks for specific items at specific positions.
Check the whole ordered list Map item text and deep-compare with an expected array Membership, text, and order in one comparison.
Assert predictable rows Fixture data plus DOM assertions Compares rendered rows with known test records.
Generate one test per record Import data when defining tests Creates test blocks before test execution; a fixture call inside a test cannot do this.
Process a large file or use Node-side logic cy.task() Keeps file processing on the server side and lets the task return the needed result.

Troubleshoot list assertions

  • The count assertion times out: Check that the selector matches the rendered item elements, that the expected count is correct for this test state, and that the page actually reaches that state. Keep the assertion in a retryable .should() chain rather than checking once in .then().
  • The count passes but the list is wrong: Count alone does not verify which records appear. Add text assertions for important positions or compare the complete rendered sequence with the expected array.
  • The test passes when the list is missing: Replace a negative assertion with a positive assertion describing the intended count or content.
  • A positional assertion targets an unexpected item: Confirm whether the selector means the first matching item globally or the first child in each list. Scope the query to the intended list and use .first() or .eq() deliberately.
  • A callback causes repeated actions: Cypress may rerun a .should(callback) callback. Keep it limited to assertions and pure DOM inspection; perform actions outside it.
  • Fixture typing appears to accept invalid data: A TypeScript generic describes the expected yielded type but does not validate fixture contents at runtime.

Or skip the browser setup

If your goal is to capture a page screenshot while testing a list page, ScreenshotNeo provides a one-request screenshot API. It can capture a rendered page, but it does not replace Cypress assertions about item count, text, or order. See the ScreenshotNeo website and API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Cookie banners are accepted and removed before capture, along with supported newsletter popups and chat widgets. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. The service also has an MCP server for AI agents, with tools including take_screenshot, get_page_info, and capture_pdf. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.