英文:
In cypress is there a way to select from a p-dropdown by text?
问题
I am trying to implement a Cypress test in which I would like to select from a p-dropdown by text.
Basically I want to select from the following dropdown by the text 'test 1 GROUP'
This is my html code for the p-dropdown:
<p-dropdown #scenarioDropdown [options]="scenarioDropdownList" [(ngModel)]="scenario" [attr.data-cy]="'scenarioDropdownList'"></p-dropdown>
with the "scenarioDropdownList" being populated with an array of following class, in which the label field is what is displayed in the options:
export interface SelectItem<T = any> {
label?: string;
value: T;
styleClass?: string;
icon?: string;
title?: string;
disabled?: boolean;
}
I think this should be pretty simple but I haven't had any luck so far.
In my Cypress test, I can get the dropdown like this - but I can't get it to find or select an actual option.
cy.get('[data-cy="scenarioDropdownList"]')
Any suggestions?
英文:
I am trying to implement a Cypress test in which I would like to select from a p-dropdown by text.
Basically I want to select from the following dropdown by the text 'test 1 GROUP'
This is my html code for the p-dropdown:
<p-dropdown #scenarioDropdown [options]="scenarioDropdownList" [(ngModel)]="scenario" [attr.data-cy]="'scenarioDropdownList'"></p-dropdown>
with the "scenarioDropdownList" being populated with an array of following class, in which the label field is what is displayed in the options:
export interface SelectItem<T = any> {
label?: string;
value: T;
styleClass?: string;
icon?: string;
title?: string;
disabled?: boolean;
}
I think this should be pretty simple but I haven't had any luck so far.
In my Cypress test, I can get the dropdown like this - but I can't get it to find or select an actual option.
cy.get('[data-cy="scenarioDropdownList"]')
Any suggestions?
Thanks much
答案1
得分: 1
IMO更好的做法是从网页HTML中工作,而不是从Angular源HTML中工作,因为网页是Cypress看到的内容。
Angular会编译其源代码,因此源代码和实际版本可能会有差异。
此外(不确定是否适用于Angular),在编译过程中,一些框架会拒绝像data-cy
这样的属性,因此您需要检查实际页面以查看它们是否存在并且是否可以使用。
这是p-dropdown
的实际页面
从中我可以看到测试所需的所有部分
cy.get('p-dropdown[placeholder="Select a City"]')
.find('div[role="button"]').click()
cy.contains('p-dropdownitem', 'London').click()
cy.get('p-dropdown[placeholder="Select a City"]')
.find('input')
.should('have.value', 'London')
当然,您可以替换day-cy
选择器并查看它们是否有效,但通常直接使用页面上的文本非常好,而且不会通过使用仅用于测试的属性增加页面加载的额外负担。
英文:
IMO it's better to work from the web page HTML rather than the Angular source HTML, since the web page is what Cypress sees.
Angular will compile it's source, so there would be differences in source and live versions.
Also (not sure if it applies to Angular) during compilation some frameworks reject attributes like data-cy
, so you would have to check the live page to see if they are present and you can use them.
Here's the live page for p-dropdown
From that I can see all the parts I need for the test
cy.get('p-dropdown[placeholder="Select a City"]`)
.find('div[role="button"]').click()
cy.contains('p-dropdownitem', 'London').click()
cy.get('p-dropdown[placeholder="Select a City"]`)
.find('input')
.should('have.value', 'London')
Of course, you can substitute day-cy
selectors and see if they work, but using actual text from the page is usually pretty good, and you don't add extra bulk to the page load by using testing-only attributes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论