在Cypress中,是否有一种通过文本选择p-dropdown的方法?

huangapple go评论158阅读模式
英文:

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'

在Cypress中,是否有一种通过文本选择p-dropdown的方法?

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的实际页面

在Cypress中,是否有一种通过文本选择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

在Cypress中,是否有一种通过文本选择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.

huangapple
  • 本文由 发表于 2023年8月11日 02:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定