用于登录页面身份验证的 Capybara 测试用例
Capybara test case for Login page authentication
我下面有水豚测试用例。
it "Testing login page with valid data" do
fill_in 'email', with: 'kiran@gmail.com'
expect(page).to have_selector("input[value='kiran@gmail.com']")#Checking values are inserted in email field
fill_in 'password', with: 'Kiran.6565'
expect(page).to have_selector("input[value='Kiran.6565']")#Checking values are inserted in password field
click_button('submit')
expect(current_path).to eql(patient_detail_path(4))
end
我正在检查登录页面,一旦电子邮件和密码字段匹配,它应该重定向到具有 id 字段值的 patient_details_path。在上面的代码中,我指定的电子邮件和密码对于手动登录工作正常,但问题出在测试用例中。预期结果:它应该重定向到另一个页面(patient_details_path)但它再次重定向到主页(/)。
Failures:
1) Login Page Interface Test login page with valid data
Failure/Error: expect(current_path).to eql(patient_detail_path(4))
expected: "/patient_details/4"
got: "/"
(compared using eql?)
# ./spec/views/login_spec.rb:41:in `block (2 levels) in <top (required)>'
Finished in 1.08 seconds (files took 2.13 seconds to load)
14 examples, 1 failure
我尝试了来自 Whosebug 的不同解决方案,但没有任何效果。以下是尝试过的不同解决方案。
#expect(current_path).to eql(patient_detail_path(4))
#expect(page).to have_current_path(patient_detail_path(4))
如果电子邮件和密码不匹配,它将抛出错误并再次重定向到登录页面。在我的场景中,即使电子邮件和密码有效,它也会引发错误。如果我在我的测试用例中添加以下代码,它将通过测试用例。
#expect(page).to have_content "Invalid username/password combination"
任何人都请帮助我,我是 rails 和水豚的 ruby 新手。
我猜你要写的测试应该写成这样
before :each do
@user = # Create the required user with whatever method you're using
@patient = # Create the required patient with whatever method you're using
end
it "Logs in with valid data" do
visit(patient_detail_path(@patient)) # gets redirected to the login path
fill_in 'email', with: 'kiran@gmail.com'
fill_in 'password', with: 'Kiran.6565'
click_button('submit')
expect(page).to have_current_path(patient_detail_path(@patient))
end
这是一般性猜测,可能不是 100% 正确(很难准确地猜测您要做什么,因为您的问题中缺少一半测试 - 前块 - )但一般部分应该在那里。由于您没有登录,我猜您实际上并没有使用给定的电子邮件和密码创建有效用户,或者您没有创建 ID 为 4 的患者(您真的不应该依赖尽管在功能测试中测试特定的 ID 号)。
此外,在检查给定的 path/url 时,您应该始终使用 have_current_path
匹配器,因为它可以防止测试不稳定,并且由于它不是视图测试,因此它不应该在 spec/views/login_spec.rb
,更合适的是 spec/features/login_spec.rb
.
在我看来,在 Rails 有机会更新 URL 以反映新页面之前,驱动程序正在捕获 URL。
在执行导航更改后很难在 URL 上断言,因为可能会出现竞争条件。我会建议:
断言其他一些可以验证用户是否成功登录的信息。
使用等待选项断言。
expect(page).to have_current_path(patient_detail_path(@patient), wait: 3)
我下面有水豚测试用例。
it "Testing login page with valid data" do
fill_in 'email', with: 'kiran@gmail.com'
expect(page).to have_selector("input[value='kiran@gmail.com']")#Checking values are inserted in email field
fill_in 'password', with: 'Kiran.6565'
expect(page).to have_selector("input[value='Kiran.6565']")#Checking values are inserted in password field
click_button('submit')
expect(current_path).to eql(patient_detail_path(4))
end
我正在检查登录页面,一旦电子邮件和密码字段匹配,它应该重定向到具有 id 字段值的 patient_details_path。在上面的代码中,我指定的电子邮件和密码对于手动登录工作正常,但问题出在测试用例中。预期结果:它应该重定向到另一个页面(patient_details_path)但它再次重定向到主页(/)。
Failures:
1) Login Page Interface Test login page with valid data
Failure/Error: expect(current_path).to eql(patient_detail_path(4))
expected: "/patient_details/4"
got: "/"
(compared using eql?)
# ./spec/views/login_spec.rb:41:in `block (2 levels) in <top (required)>'
Finished in 1.08 seconds (files took 2.13 seconds to load)
14 examples, 1 failure
我尝试了来自 Whosebug 的不同解决方案,但没有任何效果。以下是尝试过的不同解决方案。
#expect(current_path).to eql(patient_detail_path(4))
#expect(page).to have_current_path(patient_detail_path(4))
如果电子邮件和密码不匹配,它将抛出错误并再次重定向到登录页面。在我的场景中,即使电子邮件和密码有效,它也会引发错误。如果我在我的测试用例中添加以下代码,它将通过测试用例。
#expect(page).to have_content "Invalid username/password combination"
任何人都请帮助我,我是 rails 和水豚的 ruby 新手。
我猜你要写的测试应该写成这样
before :each do
@user = # Create the required user with whatever method you're using
@patient = # Create the required patient with whatever method you're using
end
it "Logs in with valid data" do
visit(patient_detail_path(@patient)) # gets redirected to the login path
fill_in 'email', with: 'kiran@gmail.com'
fill_in 'password', with: 'Kiran.6565'
click_button('submit')
expect(page).to have_current_path(patient_detail_path(@patient))
end
这是一般性猜测,可能不是 100% 正确(很难准确地猜测您要做什么,因为您的问题中缺少一半测试 - 前块 - )但一般部分应该在那里。由于您没有登录,我猜您实际上并没有使用给定的电子邮件和密码创建有效用户,或者您没有创建 ID 为 4 的患者(您真的不应该依赖尽管在功能测试中测试特定的 ID 号)。
此外,在检查给定的 path/url 时,您应该始终使用 have_current_path
匹配器,因为它可以防止测试不稳定,并且由于它不是视图测试,因此它不应该在 spec/views/login_spec.rb
,更合适的是 spec/features/login_spec.rb
.
在我看来,在 Rails 有机会更新 URL 以反映新页面之前,驱动程序正在捕获 URL。
在执行导航更改后很难在 URL 上断言,因为可能会出现竞争条件。我会建议:
断言其他一些可以验证用户是否成功登录的信息。 使用等待选项断言。
expect(page).to have_current_path(patient_detail_path(@patient), wait: 3)