2012-08-29 24 views
1

我的Ruby on Rails應用程序中有一項功能,該功能適用​​於從給定步驟中的消息「響應browser_basic_authorize」的消息。在添加@javascript標記時,黃瓜HTTP身份驗證失敗

但是,如果我在場景之前添加了@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,目前的寶石,和其他測試都通過。

回答

0

我推測你的@javascript驅動程序是Selenium,因爲這是默認設置。在Selenium Core FAQ中給出的關於基本認證的建議是在URL中提供用戶名和密碼,例如http://user:[email protected]/blah

我還沒有找到明確的答案,但它似乎Selenium Webdriver(由水豚使用),不支持訪問或修改HTTP頭,所以提供這些信息在URL中可能是唯一的辦法。