如何使用水豚检查表单字段是否正确预填? [英] How can I check that a form field is prefilled correctly using capybara?

查看:19
本文介绍了如何使用水豚检查表单字段是否正确预填?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有适当标签的字段,我可以毫无问题地用水豚填充:

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 元素(例如'约翰'):

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']")

参见 http://www.w3schools.com/xpath/xpath_syntax.asp了解更多信息.

也许是一种更漂亮的方式:

For perhaps a prettier way:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆