无法在Cypress中选择动态下拉菜单中的数值。

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

Unable to select values in dynamic dropdown in cypress

问题

我试图从名为“Country Code”的动态下拉菜单中选择一个值,位于以下网站此处(已编辑)

默认情况下,显示一个值“+1”,其他值都被隐藏。

首先,我尝试了 cy.get('#country_code').select('+34'),但没有成功。

然后我尝试了 cy.get('#country_code').eq(9).click(),这也没有成功。

接下来,我尝试了 cy.get('#country_code').contains('+34').click(),但这也失败了。

请还给我关于如何在不在字段中输入数据的情况下选择菜单的建议。

这将有助于我理解其实现背后的根本概念,因为我在静态下拉菜单中也遇到了类似的问题。

我找不到处理这种问题的博客。

英文:

I am trying to select a value from the dynamic dropdown named "Country Code" on the following website This one (redacted)

Here by default one value is shown +1 and rest are hidden.

First I tried, cy.get('#country_code').select('+34') but it did not work .

Then I tried, cy.get('#country_code').eq(9).click() even this did not work.

Next I tried cy.get('#country_code').contains('+34').click() this failed as well.

Please also give me suggestions about how to select the menu without typing the data into the field.

This will not help me understand the root concept behind its implementation, because I had a similar problem with static dropdowns as well

I could not find a blog that deals with this kind of problem .

答案1

得分: 4

Change cy.get('#country_code').contains('+34') to cy.contains('+34') and it works!

The options are added to the page only after the dropdown is opened, in a "portal" (actually a Vuetify v-overlay-container) at the bottom of the page.

Using cy.contains('+34') will find it anywhere on the page, provided the text is unique (it is on this page!)

You need to .click({force: true}) because the item is below the bottom of the list and Cypress cannot scroll it into view (.scrollIntoView() isn't working).

cy.visit('https://pvg-test.instaging.net/register')

cy.get('#country_code').click()

cy.contains('+34')
  .click({force: true})

cy.get('#country_code')
  .should('have.value', '+34')
英文:

Change cy.get('#country_code').contains('+34') to cy.contains('+34') and it works!

The options are added to the page only after the dropdown is opened, in a "portal" (actually a Vuetify v-overlay-container) at the bottom of the page.

Using cy.contains('+34') will find it anywhere on the page, provided the text is unique (it is on this page!)

You need to .click({force: true}) because the item is below the bottom of the list and Cypress cannot scroll it into view (.scrollIntoView() isn't working).

cy.visit('https://pvg-test.instaging.net/register')

cy.get('#country_code').click()

cy.contains('+34')
  .click({force: true})

cy.get('#country_code')
  .should('have.value', '+34')

答案2

得分: 0

为什么 cy.select() 没有起作用?

cy.select() 选择一个位于 <select> 标签下方的 <option> 标签。在这种情况下,您所使用的网站既没有 <select> 标签,也没有 <option> 标签。

为什么 cy.get().eq().click() 没有起作用?

cy.get() 会选择与传入的选择器匹配的一个或多个元素。.eq() 允许您选择由 cy.get() 或其他 Cypress 查询产生的第 n+1 个项目。由于只有一个具有 country_code id 的项目,尝试选择第10个项目将失败。

为什么 cy.get().contains().click() 没有起作用?

cy.contains() 在先前的 Cypress 查询产生的元素下搜索。如果您要查找的元素不在该父元素下,那么.contains() 将无法工作。

那么,实际上不需要输入什么代码?

单击 #country_code 元素后显示的列表项具有 v-list-item 类。(我能够通过让 Cypress 单击 #country_code 并使用运行器来查看网站在此状态下的快照来看到这一点。) 因此,我们只需单击 #country_code 字段,然后找到具有我们想要的文本的 .v-list-item 元素,然后单击该元素。

cy.get('#country_code').click() // 打开下拉窗口
cy.get('.v-list-item') // 获取出现的选项列表
  .contains('+34') // 查找具有 `+34` 文本的元素
  .click()
英文:

Why did cy.select() not work?

cy.select() selects an <option> tag that is underneath a <select> tag. In this instance, the website you are using does not have either of those.

Why did cy.get().eq().click() not work?

cy.get() will yield one or more elements that match the selector passed in. .eq() allows you to select the n+1th item yielded by cy.get() or another Cypress query. As there is only one item that has the id of country_code, trying to select the 10th item will fail.

Why did cy.get().contains().click() not work?

cy.contains() searches underneath the yielded element from the preceding Cypress query. If the element you are trying to find is not underneath that parent, then .contains() will not work.

So, what will actually work without typing?

The list items that appear once clicking on the #country_code element have a class of v-list-item. (I was able to see this by having Cypress click #country_code, and using the runner to see the snapshot of the website in this state.) So, we will just have to click the #country_code field, then find the .v-list-item element that has the text we want, and click that element.

cy.get('#country_code').click() // open dropdown window
cy.get('.v-list-item') // get list of options that have appeared
  .contains('+34') // find element with `+34` text
  .click()

huangapple
  • 本文由 发表于 2023年4月13日 21:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006048.html
匿名

发表评论

匿名网友

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

确定