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

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

I have a form that looks something like this:

...
&l
2条回答
  •  误落风尘
    2021-01-03 03:55

    For mat-select in Angular 7+ you have to use promises to wait for the options in the modal cdk-overlay to become available.

    Here is a helper function for reuse:

    function selectMaterialDropDown(formControlName, selectOption) {
      cy.get(`[formcontrolname="${formControlName}"]`).click().then(() => {
        cy.get(`.cdk-overlay-container .mat-select-panel .mat-option-text`).should('contain', selectOption);
        cy.get(`.cdk-overlay-container .mat-select-panel .mat-option-text:contains("${selectOption}")`).first().click().then(() => {
          // After click, mat-select should contain the text of the selected option
          cy.get(`[formcontrolname="${formControlName}"]`).contains(selectOption);
        });
      });
    }
    

    Call function:

    selectMaterialDropDown('myMatSelectControlName', 'OptionTextToSelect');

提交回复
热议问题