是否有可能在水豚中选择多个相等的元素?

问题描述

我开始研究以下{@ {1}},capybaracucumberSitePrism这类宝石的自动化测试,我发现在测试中发生了很多事情是输入类型字段(如果重复)。

类似的东西:

webdriver

所以我想知道,是否有可能同时在所有字段中定义一些值,例如:

<input type="text" placeholder="some cool text"/>
<input type="text" placeholder="some cool text"/>
<input type="text" placeholder="some cool text"/>

我很难找到答案,创建循环来一次定义一个循环没有问题,但是我不知道如何同时选择它们。

解决方法

find仅限于返回一个元素,如果要多个元素,则需要使用all。使用水豚之类的东西

page.all('input',text: 'some cool text').each do |inp| 
  inp.set('A simple string')
end

将按照您的要求进行操作。如果您想确定所处理的恰好是3个匹配元素,则可以使用count选项(可用的选项之间有also最低,最高,and

page.all('input',text: 'some cool text',count: 3).each do |inp| 
  inp.set('A simple string')
end

更新:由于您已经更新了问题,可以解决

page.all("input[placeholder='some cool text']",count: 3).each do |inp| 
  inp.set('A simple string')
end

但是我可能会使用Capybara提供的选择器类型,例如

page.all(:fillable_field,placeholder: 'some cool text',count: 3).each do |inp|
  inp.set('A simple string')
end