当Select包含重复值时使用Cypress select()

时间:2019-06-17 14:10:35

标签: javascript testing select e2e-testing cypress

我需要在select中选择一个值。选择的是一个国家列表,显示其名称,并使用两个字母的国家/地区代码作为每个选项的值。我们将选择最多的国家/地区放在顶部,同时也将它们按字母顺序排列。这意味着浮出水面的项目将重复两次。

<select>
  <option value="gb">UK</option>
  <option value="us">USA</option>
  <option value="af">Afganistan</option>
  <option value="ai">Aland Islands</option>
  ...
  <option value="us">USA</option>
  ...
  <option value="us">UK</option>
  ...
</select>

我正在选择一个这样的值:

cy.getSelect().select('gb')

但是,这会引发错误:

  

CypressError:超时重试:cy.select()通过值或文本匹配多个选项:gb

这很有意义,因为“ UK”的值为gb,它出现在列表的顶部和列表中的字母顺序。

如何告诉赛普拉斯忽略重复值并选择第一个匹配项?

请注意,我无法保证任何国家/地区的索引,并且我还有许多其他测试会选择不同国家/地区。我需要一种方法来告诉赛普拉斯选择第一个比赛。

2 个答案:

答案 0 :(得分:1)

您可以尝试下拉至jQuery / JavaScript手动设置字段:

cy.get('select').then($country => {$country.val("gb")})

上面的$country应该是一个包含您所选择的html的jQuery对象。

请参阅:https://docs.cypress.io/api/commands/then.html#Syntax

答案 1 :(得分:-1)

以下是一些可供参考的资料: https://docs.cypress.io/api/commands/select.html#Text-Contenthttps://docs.cypress.io/api/commands/eq.html#Syntax

我建议尝试的方法如下: <object width="550" height="500" data="http://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Amber,+115+Portswood+Road,+Southampton,+SO17+2FX,+United+Kingdom&amp;aq=0&amp;sll=50.923556,-1.394663&amp;sspn=0.006709,0.01929&amp;vpsrc=6&amp;ie=UTF8&amp;hq=Amber,&amp;hnear=115+Portswood+Rd,+Southampton+SO17+2,+United+Kingdom&amp;t=m&amp;ll=50.923178,-1.393676&amp;spn=0.012985,0.027466&amp;z=15&amp;output=embed" type="text/html"></object> 要么 cy.get('select').select('gb').eq(0)

如果找到重复项,则这些选项中的任何一个都将抢占第一项。您可以使用cy.get('select').select('gb').first()进行的其他操作包括,对于最后一项使用eq(),对于第三项(以零为基数)则使用eq(-1),依此类推。

编辑:使用cy.get('select')代替cy.getSelect();

相关问题