2012-08-13 78 views
0

我有我的情況如下:我怎樣才能得到webrat跟隨重定向

And I click the link Log out 
Then I should see the login page 

點擊Log out鏈接會將用戶帶到/ log_out,會被重定向回到登錄頁面。 Webrat失敗,因爲它正在尋找「登錄」,但log_out頁面是空的(這只是一個codeigniter功能,它會破壞會話並重定向到/)

什麼是使webrat意識到它需要閱讀重定向結束頁面?

回答

0

一個簡單但效率低下的方法是在執行重定向所需的大概時間內添加一個睡眠。

And I click the link Log out 
And I wait for 5 seconds 
Then I should see the login page 

步驟定義

When /^I wait for (\d+) seconds$/ do |time| 
    sleep time.to_i 
end 

但是這種解決方案是非常脆弱,當涉及到網絡的滯後,你的等待可能要麼太長或太短。一個更好的方法就是睡覺,直到你要找的內容出現。

And I click the link Log out 
Then within 30 seconds, I should see the login page 

步驟定義

Then /^within (\d+) seconds, I should see the login page$/ do |time| 
    sleep time.to_i until page.has_text? "Login" 
    page.should have_content "Login" 
end 

如果你發現自己使用等待其他步驟,逗號後面步驟的其餘部分可以推廣。