用黄瓜检查页面标题

时间:2011-07-27 05:23:10

标签: ruby-on-rails ruby cucumber acceptance-testing

功能

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)

它在然后步骤失败,但是当我手动转到它时,页面会使用标题:“注册”进行渲染。我希望它能确保它在测试中找到正确的位置。我如何检查?

提前感谢所有帮助。

4 个答案:

答案 0 :(得分:6)

试试这个:

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

答案 1 :(得分:3)

自2.1以来你可以做到:

expect(page).to have_title "my_title"

答案 2 :(得分:0)

可能会发生这种情况,因为默认的水豚选择器设置为CSS尝试将其更改为

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

答案 3 :(得分:0)

如果您正在使用cucumber-rails gem,请尝试使用

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