Capybara: How to test the title of a page?

前端 未结 8 1600
一向
一向 2020-12-08 12:41

In a Rails 3 application using Steak, Capybara and RSpec how do I test the page\'s title?

相关标签:
8条回答
  • 2020-12-08 13:30

    You should be able to search for the title element to make sure it contains the text you want:

    page.should have_xpath("//title", :text => "My Title")
    
    0 讨论(0)
  • 2020-12-08 13:32

    Testing the Title of each page can be done in a much easier way with RSpec.

    require 'spec_helper'
    
    describe PagesController do
      render_views
    
      describe "GET 'home'" do
        before(:each) do
          get 'home'
          @base_title = "Ruby on Rails"
        end
    
        it "should have the correct title " do
          response.should have_selector("title",
                                    :content => @base_title + " | Home")
        end
      end
    end
    
    0 讨论(0)
提交回复
热议问题