使用 cypress 从 Mat-select 中选择选项

Pre*_*mar 16 angular-material angular cypress angular7

我有垫选择下拉列表如下

<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>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 Cypress 从该领域选择男性或女性。

cy.get('mat-select').click().select('Male')
Run Code Online (Sandbox Code Playgroud)

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

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

我需要一些帮助来解决这个问题,谢谢。

对我有用的代码。

cy.get('#gender').click().then(() => {
            cy.get('#male').click()
        })
Run Code Online (Sandbox Code Playgroud)

小智 21

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

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


小智 6

基本上我所做的是模拟下拉打开,然后总结另一个项目选择的点击:

// 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()
        });
Run Code Online (Sandbox Code Playgroud)

  • 为什么使用 jQuery click 而不是 Cypress? (2认同)