2011-07-27 68 views
3

功能用黃瓜檢查頁面標題

Scenario: As a user that has not signed in I want to be able to sign up for provisioner 
     Given I am not logged in 
     When I go to the home page 
     And click on the "Sign Up" button 
     Then I should be on the page with the title: "Sign Up" 
     When I provide valid information 
     And click the "Submit" button 
     Then I should see "Account is being created and verified, you will receive an email with instructions once account has been approved" 
     And the application should send the administrator an email 
     And I should be redirected to the home page as an anonymous user 

步驟

When /^I go to the home page$/ do 
    visit root_path 
end 

When /^click on the "([^"]*)" button$/ do |page_name| 
    click_link page_name 
end 

Then /^I should be on the page with the title: "([^"]*)"$/ do |page_title| 
    response.should have_selector('title', :content => "#{page_title}") 
end 

錯誤

expected css "title" to return something (RSpec::Expectations::ExpectationNotMetError) 

它未能在然後步驟,但是當我手動進入頁面時,頁面會顯示標題:「註冊」。我希望它能確保它在測試中的正確位置。我如何檢查?

感謝您提前提供的所有幫助。

+0

您是否使用Capybara? – polarblau

+0

沒關係 - 如果是的話,試試@Bohdan Pohorilets的回答。 – polarblau

回答

6

試試這個:

page.should have_css('head title', :text => title)

+3

我用了page.should have_selector('title',:content =>),它工作。謝謝! – BrianJakovich

+0

這需要rspec。 –

0

這可能是因爲在默認情況下豚選擇碰巧被設置爲CSS嘗試將其更改爲

response.should have_xpath('//title', :text => "#{page_title}") 
3

由於2.1,你可以這樣做:

expect(page).to have_title "my_title"

+0

這要求您使用rspec。 –

0

如果您使用的是黃瓜護欄寶石,請嘗試使用

assert page.has_title?(page_title), "page has title of '#{page.title}'"