Rails 4 Capybara 如何测试表单中输入字段的准确顺序
Rails 4 Capybara how to test the exact order of the input fields in a form
在我的 Rails 4 应用程序中,我有一个表单供用户设置新密码。
我想用 Capybara 对此进行测试并测试输入字段的顺序,因为我需要确保它们在 UI 中始终处于特定顺序。
在我的 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
我需要添加一种方法来检查输入字段的顺序是否始终相同。因此,如果由于某种原因顺序发生变化,我会看到测试失败。
rendered
在视图规范中只是一个包含 HTML 的字符串,因此具体取决于 HTML 的外观,最简单的方法就是正则表达式
rendered.match?(/id="user_current_password".+id="user_password".+id="user_password_confirmation"/m)
如果您想获得更多 "correct" 并实际验证结构,您可以使用 has_xpath?
使用 following-sibling
轴或可能 has_css?
与相邻的兄弟选择器( +),但要提供这些查询,我们需要查看实际的 HTML.
在我的 Rails 4 应用程序中,我有一个表单供用户设置新密码。 我想用 Capybara 对此进行测试并测试输入字段的顺序,因为我需要确保它们在 UI 中始终处于特定顺序。
在我的 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
我需要添加一种方法来检查输入字段的顺序是否始终相同。因此,如果由于某种原因顺序发生变化,我会看到测试失败。
rendered
在视图规范中只是一个包含 HTML 的字符串,因此具体取决于 HTML 的外观,最简单的方法就是正则表达式
rendered.match?(/id="user_current_password".+id="user_password".+id="user_password_confirmation"/m)
如果您想获得更多 "correct" 并实际验证结构,您可以使用 has_xpath?
使用 following-sibling
轴或可能 has_css?
与相邻的兄弟选择器( +),但要提供这些查询,我们需要查看实际的 HTML.