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

Use Cypress .should() after a command that yields the value or element you want to check. Cypress retries linked queries and the assertion until it passes or the applicable timeout expires. Use a callback for several repeat-safe checks on the same subject; use .then() for one-time follow-up work.

How to write a .should() assertion

.should() is chained from a preceding Cypress command; it cannot be called directly from cy. It is an alias of .and(). Cypress supports four forms:

  • .should(chainers)
  • .should(chainers, value)
  • .should(chainers, method, value)
  • .should(callbackFn)

Choose a command that yields the subject you intend to assert against, then select a chainer that expresses the required state:

cy.get('.error').should('be.empty')
cy.contains('Login').should('be.visible')
cy.wrap({ foo: 'bar' }).its('foo').should('eq', 'bar')

The examples use Chai assertions and Cypress’s Chai-jQuery and Sinon-Chai extensions. The expected value should come from your application’s requirements, not from a documentation example copied without context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Common UI assertions

cy.get('.left-nav > .nav').children().should('have.length', 8)
cy.get('#header a').should('have.attr', 'href', '/users')
cy.get('nav').should('be.visible')

Positive and negative assertions are both available. Be careful with broad negative checks: an assertion such as “not visible” can pass in multiple states, including states other than the one your test is meant to verify. Prefer an assertion that identifies the actual requirement.

How Cypress retries assertions

Queries linked in a Cypress chain retry together. If a linked .should() assertion fails, Cypress reruns the linked query work and assertion until the check passes or the applicable timeout expires. One-time commands do not become retryable simply because a .should() follows them.

Cypress documentation examples commonly show a 10-second default wait, but ten seconds is not a universal invariant: configuration and command-level timeout options can change the applicable timeout. A command’s timeout option can set the timeout passed to the assertion.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Group checks that must hold at once

A callback assertion lets you check several properties against one refreshed subject. For example, this checks that the current text parses to a number from 1 through 10:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('[data-testid="random-number"]').should(($div) => {
  const n = parseFloat($div.text())
  expect(n).to.be.gte(1).and.be.lte(10)
})

If an assertion inside the callback throws, Cypress can invoke the callback again while retrying. Keep callback code synchronous, observational, safe to repeat, and free of side effects. Do not put Cypress commands inside a .should() callback. Avoid actions such as clicks, mutations, or behavior that should occur only once; put commands before or after the assertion instead.

What subject continues down the chain?

Most .should() calls yield the same subject received from the preceding command. Some chainers change the yielded value. For example, should('have.css', 'font-family') yields a CSS value, and should('have.attr', 'href') yields an attribute value. A callback’s return value is ignored, so the original subject continues down the chain.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Check the selected chainer’s yield behavior before chaining commands that depend on a particular subject type. Otherwise, a later command may receive a string or other value where the test expects a DOM element.

Avoid stale elements after a passing assertion

A passing assertion in the middle of a query chain creates a retry boundary: if a later query fails, Cypress does not rerun the queries before that assertion. If the page rerenders in between, later work may use a detached, stale DOM element.

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

When later work needs the current DOM node, start a new statement from the page root so Cypress can query the current page again:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
cy.get('.list').find('li').eq(2).should('contain', 'Header')

cy.get('.list')
  .find('li')
  .eq(2)
  .children('.child')
  .eq(3)
  .should('contain', 'child')

If several checks need to observe the same refreshed subject, a single retrying callback can be appropriate, provided every operation inside it is repeat-safe and contains no Cypress commands.

Choose between .should() and .then()

Use Retry behavior Callback guidance
.should() Retries linked queries and the assertion until it passes or times out. Use for assertions about state that may still be changing. A callback must be repeat-safe and contain no Cypress commands.
.then() Runs once after the prior command settles; it does not retry the earlier query. Use for one-time handling or manipulation after the preceding command completes.

A common pattern is to wait for the required state with .should(), then perform one-time work in a following .then(). Do not substitute .then() when the UI may still be updating and the check needs to keep retrying.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common assertion problems

The assertion times out

Check that the preceding command yields the intended subject, the selector matches the element you expect, and the asserted state is actually reached. If the state takes longer than the applicable timeout, review your Cypress timeout configuration or the command’s timeout option rather than assuming every assertion has the same fixed wait.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

A callback appears to run more than once

That is expected when Cypress retries a failing callback assertion. Remove one-time actions and side effects from the callback; keep it to synchronous reads and assertions, and issue Cypress commands outside it.

A later command receives the wrong kind of subject

Some chainers yield a value such as an attribute or CSS property instead of the original element. Check the chainer’s yield behavior and restructure the chain if the following command needs a DOM subject.

A later query finds a detached element

A prior passing assertion can lock in the subject for the remaining chain. If the page can rerender, begin a new statement with a fresh query from the page root before asserting on the updated element.

A negative assertion passes in an unexpected state

Make the assertion express the state the test actually requires. A broad negative check may be true for several unintended application states; use a more specific assertion when the distinction matters.

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

Or skip the browser setup

If your task is to capture a website screenshot rather than write a Cypress assertion, ScreenshotNeo offers a one-request screenshot API. This is a separate option, not a replacement for testing application behavior with Cypress. Its capture can remove cookie banners, popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are not billed; and an MCP server lets AI agents take screenshots.

For example, this cURL request saves a WebP screenshot:

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

See the ScreenshotNeo documentation for API options. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for free.

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.

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