使用赛普拉斯从Mat-select中选择选项

时间:2019-06-10 05:16:28

标签: angular angular-material angular7 cypress

我的Mat-select下拉列表如下

<mat-form-field>
   <mat-label>Gender</mat-label>
      <mat-select id="gender" required formControlName="gender">
         <mat-option id="Male" value="male"> Male </mat-option>
         <mat-option value="female"> Female </mat-option>
      </mat-select>
 </mat-form-field>

我正在尝试使用赛普拉斯从现场选择男性或女性。

cy.get('mat-select').click().select('Male')

使用上面的代码,我得到以下错误:

CypressError: cy.select() can only be called on a <select>. Your subject is a: <mat-select>

我需要一些帮助解决此问题,谢谢。

对我有用的代码。

cy.get('#gender').click().then(() => {
            cy.get('#male').click()
        })

4 个答案:

答案 0 :(得分:7)

打开mat-select的mat-option对话框并选择包含“ Apple Inc.”的字段。

cy.get('mat-select[formControlName=companyName]').click().get('mat-option').contains('Apple Inc.').click();

答案 1 :(得分:3)

基本上,我所做的是模拟下拉列表的开口,然后汇总另一个单击以选择项目:

// simulate click event on the drop down
cy.get('mat-select').first()
        .click(); // opens the drop down

// simulate click event on the drop down item (mat-option)
cy.get('.mat-option-text span')
  .contains('Your MatItem Text')
  .then(option => {
            option[0].click();  // this is jquery click() not cypress click()
        });

答案 2 :(得分:2)

首先,单击“选择”以打开叠加层,然后根据您的选项文本内容单击您的选项:

cy.get('mat-select[formcontrolname="departament"]').click();
cy.get('mat-option').contains('Lima').click();

我的选项文字为 Lima(+001) 。当我查询.contains('Lima')时,出现“ AssertionError”。但是当我查询.contains('Lima').contains('001')元素时被发现。在我看来,仅支持字母数字(我没有深入探讨)。

答案 3 :(得分:0)

首先,选择要打开叠加层,然后根据您的选项文本内容选择您的选项,并使用should()来确认选择了正确的选项:

cy.get('mat-select').select('Male').should('have.value','male');

相关问题