Kan*_*eja 9 rspec capybara rspec-rails ruby-on-rails-4
我正在尝试为自由职业者选择单选按钮,代码如下(当我们在浏览器上检查元素时)
<label for="registration_payer_type_business"><input checked="checked" id="registration_payer_type_business" name="registration[payer_type]" type="radio" value="business">
Company
</label>
<label for="registration_payer_type_freelancer"><input id="registration_payer_type_freelancer" name="registration[payer_type]" type="radio" value="freelancer">
Freelancer
</label>
Run Code Online (Sandbox Code Playgroud)
我试过了
page.choose("registration_payer_type_freelancer")
Run Code Online (Sandbox Code Playgroud)
这不会给出任何错误,但是当在水豚中保存和打开页面时,仍然没有选择对抗自由职业者的广播框.如果人们可以使用xpath给出示例并选择,我将不胜感激.
Tho*_*ole 17
您最有可能遇到的真正问题是save_and_open_page使用当前属性值而不是当前属性值来保存HTML.这意味着您选择了一个单选按钮(更改已检查的属性值,而不是属性值,不一定会显示).save_and_open_screenshot如果您想查看页面的当前状态,最好使用它.下面说的是可以选择单选按钮的方法.
要选择具有Capybara的特定单选按钮,您可以使用id,名称,标签文本和值,如果需要使其唯一(例如名称)
choose('registration_payer_type_freelancer') # id
choose('registration[payer_type]', option: 'freelancer') # name and value to make unique
choose('Freelancer') # label text
choose(option: 'freelancer') # just value if the only radio button with that value
Run Code Online (Sandbox Code Playgroud)
在所有这些情况下,如果页面上的实际单选按钮输入元素不可见(用于样式目的等),而您想要单击可以通过的可见标签 allow_label_click: true
choose('registration_payer_type_freelancer', allow_label_click: true) # find by id and select by clicking the label if input is non-visible
Run Code Online (Sandbox Code Playgroud)
您可以使用的其他选项是通过CSS查找(如果您的默认选择器类型是默认值,则可以忽略:css参数:css)
find(:css, '#registration_payer_type_freelancer').click
Run Code Online (Sandbox Code Playgroud)
您也可以使用XPath查询来定位元素,但是98%的时间它们确实不是必需的(更多的人正确理解CSS,并且查找器的范围通常可以用来获取任何元素),并且有问题需要意识到 - https://github.com/teamcapybara/capybara/blob/master/README.md#beware-the-xpath--trap
find(:xpath, './/input[@value="freelancer"]').click
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5473 次 |
| 最近记录: |