如何使用水豚检查表单字段是否正确填充? [英] How can I check that a form field is prefilled correctly using capybara?
问题描述
我有一个带有适当标签的字段,可以毫无问题地用水豚填写:
I have a field with a proper label that I can fill in with capybara without a problem:
fill_in 'Your name', with: 'John'
我想在填写之前先检查其值
I'd like to check the value it has before filling it in and can't figure it out.
如果我在 fill_in
之后添加以下行:
If I add after the fill_in
the following line:
find_field('Your name').should have_content('John')
该测试失败了,尽管正如我通过保存页面验证的那样,之前的填充工作正常。
That test fails, although the filling just before worked as I've verified by saving the page.
什么我想念吗?
推荐答案
您可以使用 xpath查询检查是否有 input
具有特定值的元素(例如'John'):
You can use an xpath query to check if there's an input
element with a particular value (e.g. 'John'):
expect(page).to have_xpath("//input[@value='John']")
请参见< a href = http://www.w3schools.com/xpath/xpath_syntax.asp rel = noreferrer> http://www.w3schools.com/xpath/xpath_syntax.asp 有关更多信息。
也许更漂亮:
expect(find_field('Your name').value).to eq 'John'
编辑:如今我可能会使用have_selector
Nowadays I'd probably use have_selector
expect(page).to have_selector("input[value='John']")
如果使用的是页面对象模式(应该是!)
If you are using the page object pattern(you should be!)
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"
这篇关于如何使用水豚检查表单字段是否正确填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!