我在Ruby on Rails应用程序中具有一个功能,该功能可以很好地与给定背景下的“响应浏览器_basic_authorize”消息一起使用。
但是,如果在方案之前添加@javascript标记,则给定的背景将失败,并显示“我不知道如何登录”。
怎么了,我该如何在应用程序上测试JavaScript交互?
Background:
Given I perform HTTP authentication as "<id>/<password>"
When I go to the homepage
Then I should see "Text-that-you-should-see-on-your-home-page"
Scenario: Displaying injury causative factors
Given I am on the new_incident_report page
When I choose "incident_report_employee_following_procedures_true"
Then I should see "Equipment failure?"
Then I should not see "Lack of training"
When /^I perform HTTP authentication as "([^\"]*)\/([^\"]*)"$/ do |username, password|
puts "id/pswd: #{username}/#{password}"
### Following works ONLY if performed first before even going to a page!!!
if page.driver.respond_to?(:basic_auth)
puts 'Responds to basic_auth'
page.driver.basic_auth(username, password)
elsif page.driver.respond_to?(:basic_authorize)
puts 'Responds to basic_authorize'
page.driver.basic_authorize(username, password)
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
puts 'Responds to browser_basic_authorize'
page.driver.browser.basic_authorize(username, password)
else
raise "I don't know how to log in!"
end
end
我正在使用Ruby on Rails 3.0.9,当前的gem和其他测试通过。
最佳答案
我假定您的@javascript
驱动程序是Selenium,因为这是默认设置。 Selenium Core FAQ中有关基本身份验证的建议是在URL中提供用户名和密码,例如http://user:pass@myexample.com/blah
。
我还没有找到确切的答案,但似乎Selenium Webdriver(由Capybara使用)不支持访问或修改HTTP标头,因此在URL中提供此信息可能是唯一的方法。
关于javascript - 添加@javascript标记时,Cucumber中的HTTP身份验证失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12180744/