October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

How to Use Select and Select2 Widgets in Cypress

Cypress .select() is for native selects. For Select2, choose between testing the visible widget and deliberately setting its hidden backing select, then assert the state that matters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Cypress .select() for a native <select>. Select2 adds a custom visible interface around a select, which may be hidden from Cypress actionability checks: for realistic end-to-end coverage, operate the visible widget; for deliberate state setup, use .select(value, { force: true }) and verify both the underlying value and displayed selection. For remote results, wait on retrying queries and assertions rather than assuming results are immediately available.

Use Cypress .select() with a native select

Cypress .select() selects an option within a native <select>. Pass the option’s value or its visible text, then assert the value the application receives. Cypress documents both forms in its .select() API.

As an Amazon Associate I earn from qualifying purchases.

cy.get('[data-cy="state"]').select('MA')
cy.get('[data-cy="state"]').should('have.value', 'MA')

cy.get('[data-cy="state"]').select('Massachusetts')
cy.get('[data-cy="state"]').should('have.value', 'MA')

Use a selector that identifies one intended control. Cypress’s best practices recommend application-owned data selectors where possible; IDs are also useful when stable. Assertions should reflect what matters to the test: often that is the selected backing value, but it may also include the displayed label or application behavior.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

How do I use Select2 with Cypress?

Select2 decorates a select with a separate rendered interface. The original select can be hidden, while the user interacts with a selection display, search field, and result list. Cypress normally checks actionability before acting, so calling .select() on a hidden backing element can fail. See the Select2 basic setup and Cypress’s interaction guide.

Prefer the visible widget for user-path coverage

To test what a user can do, open the visible Select2 control, type into its search field if needed, choose a rendered result, and assert the resulting state. Adapt selectors to the actual markup and scope them to a particular widget when the page has more than one:

// Example selectors; inspect and adapt to the app's rendered markup.
cy.get('[data-cy="state-select2"]').click()
cy.get('[data-cy="state-select2"] .select2-search__field')
  .type('Massachusetts')
cy.contains('.select2-results__option', 'Massachusetts').click()

cy.get('[data-cy="state"]').should('have.value', 'MA')
cy.get('[data-cy="state-select2"] .select2-selection__rendered')
  .should('contain', 'Massachusetts')

The search and result selectors shown are illustrative, not universal. Select2 configuration and version can affect rendered markup, and a search field may be placed outside the selection container in some configurations. Inspect the live DOM, use stable app-owned selectors where available, and scope queries so Cypress targets one widget. An unscoped search-field query can match multiple controls, but typing requires a single target. The Select2 tutorial’s example and this behavior are described in the Cypress Select2 tutorial.

Use force only for intentional backing-state setup

If the purpose of the test is to set a value on the hidden native control rather than exercise the visible UI, Cypress can bypass actionability checks with force: true. Confirm both the backing select and the rendered label if both are relevant:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('#favorite-state').select('MA', { force: true })
cy.get('#favorite-state').should('have.value', 'MA')
cy.get('#select2-favorite-state-container')
  .should('have.text', 'Massachusetts')

Force is a test shortcut, not proof that a user can operate the widget. Cypress’s interaction guidance explains that action commands normally check element state before acting; use the visible-control pattern when that interaction itself is under test.

Test Select2 multiple selection

Select2 supports multiple selection when its underlying select has the multiple attribute. For intentional backing-state setup, Cypress accepts an array of option values; assert the resulting array:

cy.get('#states').select(['MA', 'VT'], { force: true })
cy.get('#states').invoke('val').should('deep.equal', ['MA', 'VT'])

For end-to-end coverage, select each choice through the visible widget and assert the selected chips or labels as well as important backing values. The appropriate assertion depends on whether the test is about rendered interaction, stored value, or both.

Handle programmatic changes and Select2 events

Select2 listens for change on the select it decorates. When code changes the value programmatically, trigger change so Select2 and other listeners can respond. Select2’s programmatic control documentation shows this pattern:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$('#mySelect2').val('1').trigger('change')

If the update should notify Select2 without firing unrelated change handlers, Select2 documents the narrower change.select2 event in its events guidance. Select2 also relays public events on the attached select; a select2:select handler can read selection data at e.params.data:

$('#mySelect2').on('select2:select', (e) => {
  const selected = e.params.data
})

In Cypress, invoking a page’s jQuery or Select2 methods can help inspect or set state when those libraries are present. Treat such setup or inspection as distinct from testing the user-visible interaction.

Wait for AJAX-backed Select2 results

Remote results are loaded asynchronously. Select2 does not create an option node for a remote item until that item has been selected for the first time; afterward, that option remains in the DOM even if the selection changes. Therefore, do not assume a remote option exists in the select before opening or searching the widget. Select2 documents this behavior in its AJAX data source guide.

Open the widget, enter a query if required, and wait for the relevant result to appear before clicking it. Cypress retries queries and assertions while waiting, rather than requiring a fixed sleep; see its retry-ability documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('[data-cy="country-select2"]').click()
cy.get('[data-cy="country-select2"] .select2-search__field')
  .type('Canada')
cy.contains('.select2-results__option', 'Canada')
  .should('be.visible')
  .click()
cy.get('[data-cy="country"]').should('have.value', 'CA')

As with other Select2 examples, the selectors depend on the actual rendered markup. If the results panel is rendered outside the widget container, scope it using a stable identifier or another selector that distinguishes the intended control.

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

Choose the method that matches the test

Situation Recommended interaction Useful verification
Native single select .select(value) or .select(optionText) Assert the selected value.
Select2 user workflow Open the visible widget; search and click the rendered option as needed. Assert the backing value and, when relevant, the displayed label.
Hidden Select2 backing value for focused setup .select(value, { force: true }) Verify the value and rendered state if both matter.
Select2 multiple values Use the visible UI for interaction coverage, or an array with forced select for state setup. Assert selected values and, for visible interaction, chips or labels.
Remote/AJAX option Open/search, then query for the rendered result. Use retrying visibility or content assertions before selection.

Troubleshoot common failures

  • .select() says the element is not visible or actionable: The target may be Select2’s hidden original select. For UI coverage, interact with the visible widget; for intentional state setup, use { force: true } and verify state.
  • Typing fails because the query matches more than one search field: Scope the search-field query to the intended widget or use a stable application-owned selector.
  • The expected remote option is missing: It may not exist until the widget is opened and the search request returns. Query for the result after initiating the search and let Cypress retry.
  • The backing value changes but the displayed Select2 choice does not: When changing the value programmatically, trigger change so Select2 receives the update.
  • The rendered-label assertion breaks after a dependency update: A generated class or container structure may have changed. Inspect the current DOM and prefer selectors owned by the application rather than relying on implementation-specific markup.

Or skip the browser setup

If your goal is to capture a page screenshot while checking the page output—not to test Cypress interactions—ScreenshotNeo provides a screenshot API and MCP server. One GET request returns an image or PDF; this cURL example saves a WebP screenshot of the requested page. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are handled before the shot; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers report the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents.
  • 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 to try it with 1,000 screenshots a month and no card.

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.

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
PC Slower Than It Used to Be?Free scan - under a minute
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.