Search code examples
javascripthtmlselectcypress

cy.contains() cannot be passed an empty string


I'm testing my django server front-end with cypress. I should test the case where user selects empty string from options list. The code

//Next try to remove frequency    
cy.get('select').eq(5).find('option').contains('').then((selectOption) => {  
        cy.get('select').eq(5).select(selectOption.text());
}); 

led to error message:

cy.contains() cannot be passed an empty string 

although the empty string belongs to my choices:

html:

<select id="id_update_freq" calss ="form-control" name="update_freq"
    <option value="">""</option> 
    <option value="vk">Viikkotilasto</option>  
    <option value="kk">Kuukausitilasto</option> 
    <option value="nv">Kolmannesvuositilasto</option>
    <option value="kv">Puolivuositilasto</option>
    <option value="pv">Vuositilasto</option>
</select>

How can I test the case ?


Solution

  • You should use select():

    Select an <option> within a <select>.

    describe('78120822', () => {
      it('should pass', () => {
        cy.get('#id_update_freq').select('');
        cy.get('#id_update_freq').should('have.value', '');
      });
    });