angular - Cypress 数据属性未显示在 Angular dom 中

标签 angular angular-material cypress custom-data-attribute

我正在尝试使用 data-cy 属性将 Cypress 实现到我的 Angular 项目中,但它们的行为非常不一致。在某些地方,该属性确实显示在 dom 中,但在其他地方却没有。

不显示的示例:

<mat-radio-button
   *ngFor="let opt of question.options"
   [attr.data-cy]="question.key + '-' + opt.key"
   [value]="opt.key">
   {{opt.value}}
</mat-radio-button>

值属性确实显示了,所以有些东西可以工作。我尝试了不同的处理方式,例如删除括号,删除 attr. (但我知道这根本不起作用),并给出不同的值作为仅表示 test 的字符串

它起作用的示例:

<fa-icon class="logoutButton" [attr.data-cy]="'profile-button'"></fa-icon>

关于我做错了什么有什么想法吗?

最佳答案

一些库将属性限制为其发布的 API,但您始终可以使用 arialabel 而不是 data-cy,因为主要库都支持屏幕阅读器。

该属性出现在最外面的元素上。

来源

<mat-radio-button ariaLabel="test2" [value]="2">Select me</mat-radio-button>

HTML

<mat-radio-button 
  arialabel="test2" 
  class="mat-radio-button mat-accent mat-radio-checked" 
  ng-reflect-value="2"
  ...

测试

cy.get('mat-radio-button[arialabel="test2"]')

引用MatRadioButton

@Input('aria-label') ariaLabel: string

Used to set the 'aria-label' attribute on the underlying input element.

注释

  • 即使输入属性具有驼峰式大小写,HTML 属性也具有全部小写字符arialabel

  • aria-label 在文档中显示为与装饰器 @Input('aria-label') 一起使用,但不是要使用的形式在属性中

enter image description here

当然,使用 ng-for 您可以使用像 ariaLabel="question.key + '-' + opt.key"这样的唯一 key

关于angular - Cypress 数据属性未显示在 Angular dom 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71778999/

相关文章:

javascript - 如何创建一个函数以避免重复步骤?

laravel - 如何使用 cypress e2e 测试为必须连接到 Laravel API 的 Vue SPA 运行 Jenkins CI?

javascript - 在javascript中,需要执行动态数组的求和

javascript - 打印对象数组

angular - 更改 Angular mat-tab 的顺序

javascript - 如果跨度内有文本,为什么 Angular Material 模式对话框会展开?

angular - Ionic 4/Capacitor PWA 应用程序不会更新 Firebase 托管的最新内容

node.js - “ng”不被识别为内部或外部命令

javascript - 除某些点击事件外,Mat-Menu 上的 StopPropagation - Angular

automated-tests - Cypress.io : Reading and storing value of form input into a JS variable (or constant)