Search code examples
angularlistboxcypressmulti-select

Cypress select to match value on multiSelect listbox


When I run this:

cy.get('select#account').select('phChecking')
  .should('have.value', 'phChecking')

I am trying to verify that the option is there and select it in a multiSelect listBox.

Currently, this fails as the return is in array format.

Error: AssertionError: Timed out retrying after 4000ms: expected '<select#account.form-control.ng-untouched.ng-valid.ng-dirty>' to have value 'phChecking', but the value was [ '0: 'phChecking'' ]

A few attempts to match that:

cy.get('select#account').select('phChecking')
  .should('have.value', [ '0: \'phChecking\'' ])

resulted in:

Timed out retrying after 4000ms: expected '<select#account.form-control.ng-untouched.ng-valid.ng-dirty>' to have value [ '0: 'phChecking'' ], but the value was [ '0: 'phChecking'' ]

Yes, they do look the same to me. Anyway, I'm trying to select the item and assert that it is there at the same time.


Solution

  • Checking the text instead of the value is one way to test the multi-select.

    But the value you are getting ['0': 'phChecking'] is an array, because it is a multi-select, and you are able to have more than one value selected.

    If you selected two items you would see something like

    [ '0': 'phChecking', '1': 'phSavings' ]
    

    When the result of a query is an array, you can't use a simple eq to compare them because they are different arrays. eq only compares the references (the array objects), not the contents of the arrays.

    Use deep.eq instead:

    cy.get('select#account')
      .select('phChecking')
    
    cy.get('select#account')
      .invoke('val')
      .should('deep.eq', ['0': 'phChecking'])