如何使用Cypress测试mat-stepper。

huangapple go评论57阅读模式
英文:

How to test mat-stepper with Cypress

问题

我想使用Cypress测试mat-stepper

特别是我想验证特定的步骤是否当前已选择。

我尝试在mat-step标记上放置了一个data-cy属性,但这不会呈现在HTML上,因此我无法直接访问当前选择的步骤。

我想要测试的Stepper示例

在Cypress中有没有一种方法可以验证哪个是当前选择的标头?

英文:

I want to test mat-stepper with Cypress.

In particular I want to verify that a particular step is currently selected or not.

I have tried to put a data-cy attribute on the mat-step tag, but this is not rendered on HTML, so I can not access directly to the current selected step.

Example of stepper I want to test

Is there a way in Cypress where I can verify which is the selected header?

答案1

得分: 0

我尝试了一种不同的方法。

而不是在标签上使用属性,我尝试与Angular模块进行交互。

it('检查选择的步骤', () => {
  cy.visit(pageToVisit);
  let angular: any;
  cy.window()
    .then((win) => angular = win.ng)
    .then(() => cy.document()
      .then((doc) => {
        const componentInstance = angular.getComponent(doc.querySelector('mat-stepper'));
        expect(componentInstance._selectedIndex).to.equal(wantedIndex);
      }));
});
英文:

I've tried a different approach.

Instead of using an attribute on the tag, I try to interact with the Angular module

it('Check selected step', () => {
cy.visit(pageToVisit);
let angular: any;
cy.window()
  .then((win) => angular = win.ng)
  .then(() => cy.document()
    .then((doc) => {
      const componentInstance = angular.getComponent(doc.querySelector('mat-stepper'));
      expect(componentInstance._selectedIndex).to.equal(wantedIndex);
    }));
});

huangapple
  • 本文由 发表于 2023年5月26日 15:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定