Make selection using cypress in Angular if you're using material forms

前端 未结 2 1850
离开以前
离开以前 2021-01-03 03:22

I have a form that looks something like this:

...
&l
2条回答
  •  我在风中等你
    2021-01-03 03:37

    I found the following test to work with your repository (Latest commit 353633c),

    describe('Test Gender Select', () => {
    
      before(function () {
        cy.visit('http://localhost:4200/')
      })
    
      it('signs up a new user', () => {
        cy.get('button').contains('Create New Account').click();
    
        cy.get('#gender').select('Female', {
          force: true
        });
        cy.get('#gender').contains('Female')
      });
    });
    

    You can see from the Cypress test runner that it has indeed selected the Female option, so I believe it covers the aim of your original test.

    If I try to use click() like so

    cy.get('#gender').click({
      force: true
    });
    

    cypress gives the message

    CypressError: cy.click() cannot be called on a element. Use cy.select() command instead to change the value.

    So, following this instruction and using

    cy.get('#gender').select('Female');
    

    gives the following error message about visibility, which seems to be standard for a select using material design (both angular-material and materialize).

    CypressError: Timed out retrying: cy.select() failed because this element is not visible ... Fix this problem, or use {force: true} to disable error checking.

    so using { force: true } option on cy.select() fixes this problem.

    I understand the visibility issue occurs because material design covers the parent with the options list, and Cypress uses criteria for visibility based on the parent (see this question), although now the force option works (with Cypress v3.0.3).

提交回复
热议问题