【问题标题】:Using Capybara determine whether a form field exists or not使用 Capybara 判断表单域是否存在
【发布时间】:2015-05-03 01:15:19
【问题描述】:

我想确定存在id="foo"input 字段:

<input id="foo" ... />

我还想确保在某些情况下,另一个带有id="bar"input 字段存在。

【问题讨论】:

    标签: ruby-on-rails forms capybara integration-testing


    【解决方案1】:

    存在:

    expect(page).to have_css('#bar')
    

    不存在:

    expect(page).to have_no_css('#bar')
    

    注意:不要使用expect(page).to_not have_css,因为它会等待你的元素出现。

    expect 和否定形式如has_no_selector?not has_selector? 的解释可以在Capybara's Asynchronous JavaScript documentation 中找到。

    【讨论】:

      【解决方案2】:

      要确定特定的input 字段是否存在,请使用:

      assert has_xpath?("//input[@id='foo']")
      

      要确定特定的 input 字段是否存在,请使用以下之一:

      assert has_no_xpath?("//input[@id='bar']")
      refute has_xpath?("//input[@id='bar']")
      

      Search for has_xpath in these Capybara node matcher docs.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-22
        • 2013-10-31
        • 1970-01-01
        • 2014-03-15
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多