Mar*_*une 141 forms rspec capybara
我有一个带有适当标签的字段,我可以用水豚填写没有问题:
fill_in 'Your name', with: 'John'
Run Code Online (Sandbox Code Playgroud)
我想在填写它之前检查它的值,但无法弄明白.
如果我fill_in在以下行之后添加:
find_field('Your name').should have_content('John')
Run Code Online (Sandbox Code Playgroud)
该测试失败,尽管之前的填充工作正如我通过保存页面验证的那样.
我错过了什么?
fqx*_*qxp 296
另一个漂亮的解决方案是:
page.should have_field('Your name', with: 'John')
Run Code Online (Sandbox Code Playgroud)
要么
expect(page).to have_field('Your name', with: 'John')
Run Code Online (Sandbox Code Playgroud)
分别.
另见参考资料.
注意:对于禁用的输入,您需要添加该选项disabled: true.
DVG*_*DVG 177
您可以使用xpath查询来检查是否存在input具有特定值的元素(例如'John'):
expect(page).to have_xpath("//input[@value='John']")
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://www.w3schools.com/xpath/xpath_syntax.asp.
也许是一个更漂亮的方式:
expect(find_field('Your name').value).to eq 'John'
Run Code Online (Sandbox Code Playgroud)
编辑:现在我可能会使用have_selector
expect(page).to have_selector("input[value='John']")
Run Code Online (Sandbox Code Playgroud)
如果您正在使用页面对象模式(您应该!)
class MyPage < SitePrism::Page
element :my_field, "input#my_id"
def has_secret_value?(value)
my_field.value == value
end
end
my_page = MyPage.new
expect(my_page).to have_secret_value "foo"
Run Code Online (Sandbox Code Playgroud)