【问题标题】:Rails 4 Capybara how to test the exact order of the input fields in a formRails 4 Capybara如何测试表单中输入字段的确切顺序
【发布时间】:2018-07-03 13:15:19
【问题描述】:

在我的 Rails 4 应用程序中,我有一个表单供用户设置新密码。 我想用 Capybara 对此进行测试并测试输入字段的顺序,因为我需要确保它们在 UI 中始终处于特定顺序。

在我的用户界面中:

input[@id = "user_current_password"]
input[@id = "user_password"]
input[@id = "user_password_confirmation"]

我的测试现在看起来像:

it 'changing the expired password' do

    render

    expect(rendered).to have_content("Update Password")
    expect(rendered).to have_content("Current Password:")
    expect(rendered).to have_content("New Password:")
    expect(rendered).to have_content("Confirm New Password:")
    expect_submit_button("Change Password")

    # Boxes order
    current_password = page.has_xpath?('//input[@id = "user_current_password"]')
    new_password = page.has_xpath?('//input[@id = "user_password"]')
    confirm_new_password = page.has_xpath?('//input[@id = "user_password_confirmation"]')

  end

我需要添加一种方法来检查输入字段的顺序是否始终相同。因此,如果由于某种原因订单更改,我看到测试失败。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 capybara rspec-rails


    【解决方案1】:

    rendered in view specs 只是一个包含 HTML 的字符串,因此取决于 HTML 的外观,最简单的方法只是一个正则表达式

    rendered.match?(/id="user_current_password".+id="user_password".+id="user_password_confirmation"/m)
    

    如果您想获得更多“正确”并实际验证结构,您可能会使用 following-sibling 轴或 has_css? 与相邻兄弟选择器 (+) 一起使用 has_xpath?,但为了提供这些查询,我们' d 需要查看实际的 HTML。

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多