Angular - adding Cypress data-cy attribute

≯℡__Kan透↙ 提交于 2020-08-07 05:18:32

问题


I just yesterday started using cypress.io with angular, as the docs say, I'm using the attribute data-cy to specifically target elements

<div data-cy="myelement">Hello</div>

cy.get("[data-cy]=myelement")

The problem is that angular doesn't recognize the data-cy attribute if I want to bind it dinamically

<div *ngIf="user$ | async as user" [data-cy]="user.name">Online</div> 

Do I have to create a personal directive to add that attribute dinamically? Or there is a better way ?


回答1:


Angular treats data- specially and you may get in trouble when will be creating a directive.

You should be using attribute binding instead:

[attr.data-cy]="user.name"


来源:https://stackoverflow.com/questions/59453410/angular-adding-cypress-data-cy-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!