英文:
Cypress - Can't interact with a content in a list
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是Cypress的新手,还在学习中。
我在使用一个列表进行测试时遇到了一些困难,例如从列表中点击按钮。
这是我的代码:
session.cy.ts:
it('should display Create Button and click on Detail button', () => {
// 找到它
cy.get('.list button[routerLink="create"]').should('exist');
// 找不到
cy.get('.items .item button[routerLink="detail"]').click();
cy.url().should('include', '/sessions/detail/1');
});
list.component.html:
<div class="list">
<mat-card>
<mat-card-header fxLayout="row" fxLayoutAlign="space-between center">
<mat-card-title class="m0">Rentals available</mat-card-title>
<button *ngIf="user!.admin" mat-raised-button color="primary" routerLink="create">
<mat-icon>add</mat-icon>
<span class="ml1">Create</span>
</button>
</mat-card-header>
<div class="items mt2" fxLayout="row wrap" fxLayout.lt-md="column">
<mat-card class="item" *ngFor="let session of (sessions$ | async)" fxFlex>
<mat-card-header>
<mat-card-title>{{ session.name }}</mat-card-title>
<mat-card-subtitle>
Session on {{ session.date | date: 'longDate'}}
</mat-card-subtitle>
</mat-card-header>
<div mat-card-image fxLayoutAlign="center center">
<img class="picture" src="assets/sessions.png" alt="Yoga session">
</div>
<mat-card-content>
<p>
{{ session.description }}
</p>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" [routerLink]="['detail',session.id]">
<mat-icon>search</mat-icon>
<span class="ml1">Detail</span>
</button>
<button *ngIf="user!.admin" mat-raised-button color="primary"[routerLink]="['update',session.id]">
<mat-icon>edit</mat-icon>
<span class="ml1">Edit</span>
</button>
</mat-card-actions>
</mat-card>
</div>
</mat-card>
</div>
我唯一看到的区别是routerlink:
- 对于create,它是
routerLink="create"
- 但对于detail,有一些[]
[routerLink]="['detail',session.id]
我想知道是否有另一种方法来测试点击Detail,而不需要修改HTML,只在测试文件中添加一个唯一的类,例如class='detailButton'
?
英文:
I'm new on Cypress and still learning.
I have some difficult to make a test with a list like click button from a list.
Here my code
session.cy.ts :
it('should display Create Button and click on Detail button', () => {
// Found it
cy.get('.list button[routerLink="create"]').should('exist');
// Not found
cy.get('.items .item button[routerLink="detail"]').click();
cy.url().should('include', '/sessions/detail/1');
});
list.component.html :
<div class="list">
<mat-card>
<mat-card-header fxLayout="row" fxLayoutAlign="space-between center">
<mat-card-title class="m0">Rentals available</mat-card-title>
<button *ngIf="user!.admin" mat-raised-button color="primary" routerLink="create">
<mat-icon>add</mat-icon>
<span class="ml1">Create</span>
</button>
</mat-card-header>
<div class="items mt2" fxLayout="row wrap" fxLayout.lt-md="column">
<mat-card class="item" *ngFor="let session of (sessions$ | async)" fxFlex>
<mat-card-header>
<mat-card-title>{{ session.name }}</mat-card-title>
<mat-card-subtitle>
Session on {{ session.date | date: 'longDate'}}
</mat-card-subtitle>
</mat-card-header>
<div mat-card-image fxLayoutAlign="center center">
<img class="picture" src="assets/sessions.png" alt="Yoga session">
</div>
<mat-card-content>
<p>
{{ session.description }}
</p>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" [routerLink]="['detail',session.id]">
<mat-icon>search</mat-icon>
<span class="ml1">Detail</span>
</button>
<button *ngIf="user!.admin" mat-raised-button color="primary"[routerLink]="['update',session.id]">
<mat-icon>edit</mat-icon>
<span class="ml1">Edit</span>
</button>
</mat-card-actions>
</mat-card>
</div>
</mat-card>
</div>
The only differences I see are the routerlink :
- for the create it is
routerLink="create"
- but for the detail there is some []
[routerLink]="['detail',session.id]
I want to know if there's another way to test the click Detail without modifying the html by adding an unique class like class='detailButton
but just the test file ?
答案1
得分: 2
列表中的routerLink属性有点奇怪,因为它还包括session.id
。
无论session.id
的值是什么,你都需要将其包含在选择器中。假设它等于123
,那么选择器将是
cy.get('.items .item button[ng-reflect-router-link="detail,123"]')
为了进行实验,可以访问Angular示例网页,并在app.component.html
中将Heros链接更改为[routerLink]="['detail',123]"
。
这是生成的HTML(在开发工具中检查):
<nav _ngcontent-ng-c1547112614="" class="ng-tns-c1547112614-0">
<a
_ngcontent-ng-c1547112614=""
routerlink="/crisis-center" // 这是一个简单的routerLink
routerlinkactive="active"
ariacurrentwhenactive="page"
class="ng-tns-c1547112614-0"
ng-reflect-router-link="/crisis-center"
ng-reflect-router-link-active="active"
ng-reflect-aria-current-when-active="page"
href="/crisis-center"
>Crisis Center</a>
<a
_ngcontent-ng-c1547112614=""
routerlinkactive="active"
ariacurrentwhenactive="page"
class="ng-tns-c1547112614-0"
ng-reflect-router-link-active="active"
ng-reflect-aria-current-when-active="page"
ng-reflect-router-link="detail,123" // 这是带有session.id的链接
href="/detail/123"
>Heroes</a>
</nav>
如果你不知道session.id
的值,可以尝试使用部分属性选择器。^=
运算符表示“以...开始”:
cy.get('.items .item button[ng-reflect-router-link^="detail,"]')
或者使用*=
运算符,表示“包含”
cy.get('.items .item button[href*="detail"]') // href是另一种选择
或者尝试使用按钮内部的文本
cy.contains('.items .item button', 'Detail')
英文:
The routerLink attribute inside the list is a little funky because it also includes the session.id
.
Whatever the value of session.id
is, you would need to include it in the selector. Say it equals 123
, then the selector would be
cy.get('.items .item button[ng-reflect-router-link="detail,123"]')
For experimentation purposes, go to the Angular sample webcontainer and on the app.component.html
change the Heros link to [routerLink]="['detail',123]"
.
This is the HTML generated (inspecting in dev-tools):
<nav _ngcontent-ng-c1547112614="" class="ng-tns-c1547112614-0">
<a
_ngcontent-ng-c1547112614=""
routerlink="/crisis-center" // this is a simple routerLink
routerlinkactive="active"
ariacurrentwhenactive="page"
class="ng-tns-c1547112614-0"
ng-reflect-router-link="/crisis-center"
ng-reflect-router-link-active="active"
ng-reflect-aria-current-when-active="page"
href="/crisis-center"
>Crisis Center</a>
<a
_ngcontent-ng-c1547112614=""
routerlinkactive="active"
ariacurrentwhenactive="page"
class="ng-tns-c1547112614-0"
ng-reflect-router-link-active="active"
ng-reflect-aria-current-when-active="page"
ng-reflect-router-link="detail,123" // this is with session.id in link
href="/detail/123"
>Heroes</a>
</nav>
If you don't know the session.id
value, try using a partial attribute selector. The ^=
operator means "begins with":
cy.get('.items .item button[ng-reflect-router-link^="detail,"]')
or the *=
operator, which means "contains"
cy.get('.items .item button[href*="detail"]') // href is an alternative
Or try using the text inside the button
cy.contains('.items .item button', 'Detail')
答案2
得分: 0
根据提供的HTML片段,你只有一个"创建"按钮,但是根据会话的数量,你正在迭代会话列表以渲染包括此按钮的卡片。
因此,Cypress无法解析cy.get('.items .item')
,就像JavaScript无法在没有首先定位到正确项的情况下访问列表项的属性一样:
const list = [{value: 'foo'}];
// 无法工作
console.log(list.value)
根据你的策略,你需要使用Cypress API(eq、first、last等)来定位预期的项。
英文:
Based on the html snippet provided, you have only 1 'Create' button but as many 'detail' button as sessions given you are iterating over your session list to render cards including this button.
So Cypress can't resolve cy.get('.items .item')
the same way Javascript can't resolve accessing the property of an item of a list without firstly targetting the proper item:
const list = [{value: 'foo}];
// cannot work
console.log(list.value)
Depending on your strategy, you need to target the expected item with the Cypress API (eq, first, last...).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论