Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - adding Cypress data-cy attribute

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 ?

like image 307
Mauro Insacco Avatar asked Dec 23 '19 10:12

Mauro Insacco


1 Answers

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"
like image 97
yurzui Avatar answered Oct 15 '22 18:10

yurzui