admin管理员组文章数量:1336632
I am writing an automated test in Cypress to test updating a record in a list of records. I want to test the last record created in the list.
I have tried adding a data-test-id (attr.data-test-id="project-{{id}}") to the HTML and referenced it in my Cypress test.
HTML:
<mat-list-item
*ngFor="let project of projects; let id=index"
(click)="selected.emit(project)"
attr.data-test-id="project-{{id}}">
<h3 mat-line>
{{project.title}}
</h3>
<p mat-line>
{{project.details}}
</p>
Cypress test:
describe('#update', () => {
it('updates a record', (id) => {
cy.get('[attr.data-test-id="project-${id}"]').click()
});
I want to pass the index number (id) in the Cypress test, however, I am receiving a "Syntax error, unrecognized expression" message in Cypress. What am I doing wrong? Thanks in advance!
I am writing an automated test in Cypress to test updating a record in a list of records. I want to test the last record created in the list.
I have tried adding a data-test-id (attr.data-test-id="project-{{id}}") to the HTML and referenced it in my Cypress test.
HTML:
<mat-list-item
*ngFor="let project of projects; let id=index"
(click)="selected.emit(project)"
attr.data-test-id="project-{{id}}">
<h3 mat-line>
{{project.title}}
</h3>
<p mat-line>
{{project.details}}
</p>
Cypress test:
describe('#update', () => {
it('updates a record', (id) => {
cy.get('[attr.data-test-id="project-${id}"]').click()
});
I want to pass the index number (id) in the Cypress test, however, I am receiving a "Syntax error, unrecognized expression" message in Cypress. What am I doing wrong? Thanks in advance!
Share Improve this question edited Sep 28, 2019 at 3:28 DraeDaQA asked Sep 28, 2019 at 3:17 DraeDaQADraeDaQA 371 gold badge1 silver badge4 bronze badges 1-
Try
cy.contains('mat-list-item', 'text-of-the-item-i-want-to-test').click()
orcy.get('mat-list-item').eq(6).click()
(assuming e.g you want the 7th item). There's no way to parametize theit()
callback, so you have to define the position in another way. – user8745435 Commented Oct 9, 2019 at 23:07
2 Answers
Reset to default 2Not familiar with Angular, but from it seems the attr.data-
is angular-specific template syntax. The actual HTML it'll produce is going to be standard data-
attribute.
Thus, you need to query using:
cy.get(`[data-test-id="project-${id}"]`).click()
Also make sure you use template literals (using backticks: `string`
), otherwise your variable id
won't get interpolated.
Can you try below code and check the test is running fine. For the dollar symbol $
can you add Cypress.$
and give a try with the below code:
describe('Some update', () => {
it('updates a record', (id) => {
cy.get('[data-test-id="project-Cypress.${id}"]').click()
});
本文标签: javascriptHow to add a datatestid for each record of a listStack Overflow
版权声明:本文标题:javascript - How to add a data-test-id for each record of a list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411547a2469889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论