简体   繁体   English

赛普拉斯 e2e 测试与 Angular 材料垫选择不能设置 select 值

[英]Cypress e2e test with Angular Material mat-select can't set a select value

I am trying to set a mat-option value with a Cypress test in Angular 9. I have tried all of the following workarounds below and none of them work.我正在尝试使用 Angular 9 中的赛普拉斯测试设置 mat-option 值。我尝试了以下所有以下解决方法,但均无效。 I am using Angular Material and the mat-select component with dynamic ngFor mat-options.我正在使用 Angular Material 和带有动态 ngFor mat-options 的 mat-select 组件。 The selector works fine but I can't set it or get the click to work correctly on the mat-select.选择器工作正常,但我无法设置它或让点击在 mat-select 上正常工作。

Example mat-select I am using我正在使用的示例垫选择

<div fxLayout="row" fxLayoutAlign="space-between">
  <mat-form-field>
    <mat-label>Begin Year</mat-label>
    <mat-select formControlName="beginYear" data-cy="beginYear-select">
      <mat-option *ngFor="let year of beginYearOptions" [value]="year">
        {{ year }}
      </mat-option>
    </mat-select>
  </mat-form-field>

failed attempts to get the click set获取点击集的尝试失败

cy.get('[data-cy=beginYear-select]').contains(yearValue.toString()).click();

or或者

cy.get('mat-select').first().click({ force: true }).get('mat-option').contains(yearValue.toString()).click()

or或者

cy.get('mat-select[formcontrolname="beginYear"]').first().click().get('mat-option').contains(yearValue.toString()).click();

or或者

cy.get('mat-select[formcontrolname="beginYear"]').then(() => {
      cy.get('mat-option').contains(endYear.toString()).click();
      })

or或者

Cypress.Commands.add('selectMaterialDropDown', (formControlName, selectOption) => {
  cy.get(`[formcontrolname="${formControlName}"]`).click().then(() => {
    cy.get(`.cdk-overlay-container .mat-select-panel .mat-option-text`).should('contain', selectOption);
    console.log('PASSED!!!')
    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);
    });
  });
});

None of these worked and basically threw an error saying mat-option can't be found.这些都不起作用,基本上抛出了一个错误,说找不到 mat-option。 Most of the time the mat-select popup did not even appear after the click event.大多数情况下,在单击事件之后甚至没有出现 mat-select 弹出窗口。 I have also tried adding wait calls to see if it was an async issue but the same thing happened.我还尝试添加等待调用以查看它是否是异步问题,但同样的事情发生了。 Any help would be greatly appreciated.任何帮助将不胜感激。 I have attached some references below that I have also tried.我在下面附上了一些我也尝试过的参考资料。 I am confused why the popup does not appear consistently from mat-select on a click event and if it does it can't find any options.我很困惑为什么弹出窗口在点击事件上的 mat-select 出现不一致,如果出现,它找不到任何选项。

REFERENCES: Make selection using cypress in Angular if you're using material forms select dropdownlist item using cypress How to select an option with Angular Material 2 and Protractor? REFERENCES: Make selection using cypress in Angular if you're using material forms select dropdownlist item using cypress How to select an option with Angular Material 2 and Protractor? Cypress: Test if element does not exist 赛普拉斯:测试元素是否不存在

I think you need to pass '#' before mat-select to properly select it by it's id.我认为您需要在 mat-select 之前通过 '#' 以正确地通过它的 ID select 它。 Every mat-select is like a input, so in most cases it will be something like "mat-select-1", "mat-select-2"... and so on.每个 mat-select 就像一个输入,所以在大多数情况下,它会像“mat-select-1”、“mat-select-2”……等等。 I made it work by first clicking my mat-select than clicking an option from that select.我首先单击我的垫子选择而不是单击 select 中的一个选项,从而使它起作用。

cy.get('#mat-select-1').click()
cy.get('#mat-option-2').click()

Althrough I just didn't make it work with select() from cypress, this was a workaround.尽管我只是没有使它与柏树的 select() 一起使用,但这是一种解决方法。

Try this:尝试这个:

cy.get('mat-select[data-cy="beginYear-select"]').contains(yearValue.toString()).click(); cy.get('mat-select[data-cy="beginYear-select"]').contains(yearValue.toString()).click();

If it doesn't work try to remove "-" in the identifier: beginYearSelect如果它不起作用,请尝试删除标识符中的“-”:beginYearSelect

Had a similar issue and had to get around it like this:有一个类似的问题,不得不像这样解决它:

cy.get('[data-cy=beginYear-select]')
  .click()
  .get('mat-option')
  .contains('2021')
  .click();
cy.get('body').click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM