Capybara save_and_open_page 不反映单选按钮状态
Capybara save_and_open_page doesn't reflect radio buttons tate
我正在尝试 select 自由职业者的单选按钮代码如下(当我们在浏览器上检查元素时)
<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>
我试过了
page.choose("registration_payer_type_freelancer")
这不会给出任何错误,但是当在水豚中保存和打开页面时,单选框仍未 select 针对自由职业者。如果人们可以使用 xpath 和 choose.
举例说明,我将不胜感激
您最有可能遇到的真正问题是 save_and_open_page
使用当前属性值而不是当前 属性 值保存 HTML。这意味着您已经 select 编辑了一个单选按钮(更改选中的 属性 值,而不是属性值,不一定会显示)。如果您想查看页面的当前状态,最好使用 save_and_open_screenshot
。下面说的是 select 单选按钮的方法。
对于 select 带有 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
在所有这些情况下,如果实际的单选按钮输入元素在页面上不可见(出于样式目的等),而您想改为单击可见标签,您可以传递 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
您可以使用的其他选项是通过 CSS 查找(如果您的默认 select 或类型是默认的,则可以忽略 :css 参数:css)
find(:css, '#registration_payer_type_freelancer').click
您也可以使用 XPath 查询来定位元素,但在 98% 的情况下它们确实不是必需的(更多人正确理解 CSS 并且通过查找器的作用域,它通常可用于获取任何元素),并有需要注意的问题 - https://github.com/teamcapybara/capybara/blob/master/README.md#beware-the-xpath--trap
find(:xpath, './/input[@value="freelancer"]').click
我正在尝试 select 自由职业者的单选按钮代码如下(当我们在浏览器上检查元素时)
<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>
我试过了
page.choose("registration_payer_type_freelancer")
这不会给出任何错误,但是当在水豚中保存和打开页面时,单选框仍未 select 针对自由职业者。如果人们可以使用 xpath 和 choose.
举例说明,我将不胜感激您最有可能遇到的真正问题是 save_and_open_page
使用当前属性值而不是当前 属性 值保存 HTML。这意味着您已经 select 编辑了一个单选按钮(更改选中的 属性 值,而不是属性值,不一定会显示)。如果您想查看页面的当前状态,最好使用 save_and_open_screenshot
。下面说的是 select 单选按钮的方法。
对于 select 带有 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
在所有这些情况下,如果实际的单选按钮输入元素在页面上不可见(出于样式目的等),而您想改为单击可见标签,您可以传递 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
您可以使用的其他选项是通过 CSS 查找(如果您的默认 select 或类型是默认的,则可以忽略 :css 参数:css)
find(:css, '#registration_payer_type_freelancer').click
您也可以使用 XPath 查询来定位元素,但在 98% 的情况下它们确实不是必需的(更多人正确理解 CSS 并且通过查找器的作用域,它通常可用于获取任何元素),并有需要注意的问题 - https://github.com/teamcapybara/capybara/blob/master/README.md#beware-the-xpath--trap
find(:xpath, './/input[@value="freelancer"]').click