水豚断言只读字段

Tom*_*Tom 5 ruby cucumber capybara

我正在尝试测试以下 html 片段:

<input type="text" value="Pool A" name="name" class="form-control form-control-1-2" required="" readonly="" style="background-color: rgb(222, 224, 226);">
Run Code Online (Sandbox Code Playgroud)

我可以看到该元素设置为只读。我更喜欢残疾人,但我们走了!

有没有办法可以使用 Capybara 将该输入字段断言为只读?

readonly: true我在一些文档中遇到过,所以我尝试了:

expect(page).to have_css('name', readonly: true)

但它并没有说这readonly不是一个有效的选择。

任何帮助,将不胜感激

Tho*_*ole 6

您可以传递给的任何选择器类型(:field 等)find也可以传递给have_selector. 由于:field选择器类型将名称/ID/关联标签文本作为其第一个参数,因此检查页面是否具有“名称”作为 id、名称或标签文本的只读字段的最简洁方法是

expect(page).to have_selector(:field, 'name', readonly: true)
Run Code Online (Sandbox Code Playgroud)

可以更简洁地写为

expect(page).to have_field('name', readonly: true)
Run Code Online (Sandbox Code Playgroud)