cypress: cy.click() on React Select v1 component doesn't show the drop down

  • Operating System: macOS Sierra
  • Cypress Version: 0.19.4
  • Browser Version: Electron 53

Is this a Feature or Bug?

Bug

Current behavior:

cy.click() on an react select component not showing the dropdown options.

Desired behavior:

cy.click() on an react select component shows the dropdown options.

How to reproduce:

A demo repo is created to reproduce the issue.

https://github.com/bencao/react-select-cypress

The test file is located at cypress/integration/react_select_spec.js

Test code:

describe('Weird behavior when testing React Select', function() {
  it('fails on Electron 53 and Canary 61, but passes on Chrome 59', function() {
    cy
      .visit('http://jedwatson.github.io/react-select/')
      .get('div.Select-control:first')
        .click()
      .get('.Select-option:contains(Victoria)')
        .click();
  });
});

Additional Info (images, stack traces, etc)

$ cypress run                                                                                                       [12:55:58]

Started video recording: /Users/zcao/Documents/workspace/react-select-cypress/cypress/videos/79qp6.mp4

  (Tests Starting)


  Weird behavior when testing React Select
    1) fails on Electron 53 and Canary 61, but passes on Chrome 59


  0 passing (6s)
  1 failing

  1) Weird behavior when testing React Select fails on Electron 53 and Canary 61, but passes on Chrome 59:
     CypressError: Timed out retrying: Expected to find element: '.Select-option:contains(Victoria)', but never found it.
      at Object.cypressErr (http://jedwatson.github.io/__cypress/static/js/cypress.js:5729:15)
      at Object.throwErr (http://jedwatson.github.io/__cypress/static/js/cypress.js:5694:22)
      at Object.throwErrByPath (http://jedwatson.github.io/__cypress/static/js/cypress.js:5721:21)
      at $Cy._retry (http://jedwatson.github.io/__cypress/static/js/cypress.js:12954:26)
      at http://jedwatson.github.io/__cypress/static/js/cypress.js:8122:28
      at tryCatcher (http://jedwatson.github.io/__cypress/static/js/vendor.js:46490:31)
      at Promise._settlePromiseFromHandler (http://jedwatson.github.io/__cypress/static/js/vendor.js:44625:31)
      at Promise._settlePromiseAt (http://jedwatson.github.io/__cypress/static/js/vendor.js:44701:18)
      at Promise._settlePromises (http://jedwatson.github.io/__cypress/static/js/vendor.js:44817:14)
      at Async._drainQueue (http://jedwatson.github.io/__cypress/static/js/vendor.js:42184:16)
      at Async._drainQueues (http://jedwatson.github.io/__cypress/static/js/vendor.js:42194:10)
      at MutationObserver.Async.drainQueues (http://jedwatson.github.io/__cypress/static/js/vendor.js:42022:14)




  (Tests Finished)

  - Tests:           1
  - Passes:          0
  - Failures:        1
  - Pending:         0
  - Duration:        5 seconds
  - Screenshots:     1
  - Video Recorded:  true
  - Cypress Version: 0.19.4


  (Screenshots)

  - /Users/zcao/Documents/workspace/react-select-cypress/cypress/screenshots/Weird behavior when testing React Select -- fails on Electron 53 and Canary 61 but passes on Chrome 59.png (1280x720)


  (Video)

  - Started processing:   Compressing to 32 CRF
  - Finished processing:  /Users/zcao/Documents/workspace/react-select-cypress/cypress/videos/79qp6.mp4 (0 seconds)


  (All Done)

FAIL: 1

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 30 (16 by maintainers)

Most upvoted comments

This is what i do to interact with React Select components:

Cypress.addParentCommand('chooseReactSelectOption', (selector, text, option) => {
  cy
    .get(`${selector} .Select-control input`).first().click({ force: true }).type(text, { force: true })
    .get('.Select-menu').contains(option).click();
});

This is what i do to interact with React Select components:

Cypress.addParentCommand('chooseReactSelectOption', (selector, text, option) => {
  cy
    .get(`${selector} .Select-control input`).first().click({ force: true }).type(text, { force: true })
    .get('.Select-menu').contains(option).click();
});

Thanks a lot for sharing. I found that I needed to make some adjustments – maybe because I’m using React Select v2.0.0, which was just released:

// cypress/support/commands.js

// …

Cypress.Commands.add("chooseReactSelectOption", (selector, text, option) => {
  cy
    .get(`${selector} input`)
    .first()
    .click({ force: true })
    .type(text, { force: true })
    .get(`${selector} .select__menu`)
    .contains(option)
    .click();
});

(It seems like some class names have been changed.)

I used the ID I set on the input as a selector:

// cypress/integration/create_questions_spec.js

// …

cy.chooseReactSelectOption('#respondents-input', 'clark@kent.com', 'clark@kent.com');

// …
// src/components/EditQuestion.js

// …

<Select
  defaultValue={selectedRespondentOptions}
  isMulti
  name="respondents"
  options={respondentOptions}
  classNamePrefix="select"
  id="respondents-input"
  onChange={this.onRespondentInputChange}
  styles={colourStyles}
/>

// …

@dziamid The trick does work! The key is to type on ‘input’ element. A simplified version:

it('works when type on the input element', function() {
  cy
    .visit('http://jedwatson.github.io/react-select/')
    // comparing to OLD VERSION that doesn't work as below comment line
    // .get('div.Select-control:first').click()
    // the key of the trick is to type on the input element
    .get(`div.Select-control input`).first().type('V')
    .get('.Select-option:contains(Victoria)')
    .click();
});

This fix will go out in the next release. I’ll be opening a new issue describing it and linking it here. There were a few edge cases surrounding focus events that we are getting nailed down. It will likely fix other issues two. There were two separate but related problems going on which made it difficult to track down.

I modified this to find the input by label instead of id, and also added a delay before selecting the option as my input involves an async call. Hopefully someone find’s it useful. Using react-select@3.0.4

Cypress.Commands.add(
  'chooseReactSelectOption',
  (label, text, option, selectDelay = 0) => {
    cy.contains(label)
      .parent()
      .find(`input`)
      .click({ force: true })
      .type(text, { force: true });

    cy.wait(selectDelay);

    cy.get(`[class*="-menu"]`)
      .contains(option)
      .click();
  },
);

@yhoiseth fixes for react-select v2.0 will be in this PR #2982 The issue associated with v2 is #1486 , so feel free to drop a comment on there

It also works if you type a string with an empty space

cy
    .get('div.Select input')
    .first()
   // It will show all the options
    .type(' ', { force: true })
    .get('.Select-option')
    .first()
    .click()

Works for headed mode (Chrome 67) and headless mode (Chrome 59) Cypress 3.0.2

The problem is not resolved yet. It’s actually originating from react-select and I wonder if cypress can actually work around the issue there without solving the root cause itself. The problem is that react-select dropdown won’t open when the window is not focused. For example, even when you use the full gui browser but you open devtools and your window focus is on devtools instead of the app window, it still won’t open.

The input solution that was suggested above indeed works, but obviously only for input enabled react-select. No input still causes problems. We currently worked around this by running --browser chrome --headed (unfortunately, since it’s not ideal).