簡體   English   中英

Cucumber + Capybara:在我的應用程序之外重定向瀏覽器的方案存在問題

[英]Cucumber+Capybara: Problem with a scenario that redirects the browser outside of my app

Given I have a rails app
And I'm using cucumber
And I'm using capybara
And I have an action that results in a redirect_to "http://some.other.domain.com/some_path"
When I test this action
Then the in-app portion of the test works fine
But I see this error: No route matches "/some_path" with {:method=>:get} (ActionController::RoutingError)

所以capybara被正確地重定向到“ http://some.other.domain.com/some_path ”但由於某種原因它認為它應該處理我的應用程序內的url的路徑部分。 注意使用“ http://some.other.domain.com/ ”時,水豚沒有任何問題 - 如果我重定向到沒有路徑部分的網址,我的測試就會通過。

這是一個錯誤嗎?

我想我遇到了和你一樣的問題:我只是想確認一下,我的代碼會使用正確的狀態代碼重定向到給定的URL,但我不想對該URL做任何事情。

問題是,該站點按預期返回重定向,但Rack :: Test將所有內容解釋為正在測試的應用程序,並且該URL可能不存在。 但是我們可以捕獲錯誤並查看響應的樣子。 除了capybara的默認驅動程序,這可能不適用於其他任何東西。

begin
  click_button('Pay with Paypal')
rescue ActionController::RoutingError
end

expect(page.status_code).to eq(302)
expect(page.response_headers['Location']).to include('paypal.com/cgi-bin/websrc')

這是我寫的關於使用capybara-mechanize和VCR來測試外部重定向的示例。

http://blog.tddium.com/2011/10/04/testing-external-redirects-vcr-capybara-mechanize/

我為Capybara找到了一個很酷的解決方案(可以適應Cucumber)。

begin
  click_button 'Accept'
rescue ActionController::RoutingError
  # Capybara doesn't redirect externally, so matches '/cb' but that route doesn't exist
  expect(page.current_url).to eq "https://example.com/cb?param=123"
end

你在用哪個司機? Rack-Test驅動程序不允許您從其他域請求內容。 如果Capybara用Selenium或Culerity做這個,那肯定是一個bug。 如果你想幫助修復它,那么寫一個失敗的規范將非常感激:)

使用這個小片段:

external_redirect "https://api.twitter.com/oauth/authenticate?x_auth_access_type=read&oauth_token=TOKEN" do
  click_link "Continue with Twitter"
end

def external_redirect(url)
  yield
rescue ActionController::RoutingError # goes to twitter.com/oauth/authenticate
  current_url.must_equal url
else
  raise "Missing external redirect"
end

@javascript是一個當前正在運行的解決方案,雖然還有一個機械化驅動程序正在工作中,它使用機架測試,直到您點擊外部請求。

這有點新,我還沒有嘗試過,但我的意思是將我的外部@javascript測試更改為使用它(用@live或@external或類似標記)來提高速度。

我有類似的情況,我正在將我的應用程序與公司的SSO平台集成。 我解決這個問題的方法是通過在場景中添加@javascript標記來使網站運行Selenium。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM