Ruby Forum RSpec > Can't test for page content after a redirect

Posted by Christian Lescuyer (xtian)
on 07.08.2008 20:44
I'm trying to verify that data entered in a form has been saved in the
database.

  Scenario: User enters a bookmark URL
    Given a bookmark 'http://www.gotapi.com/rubyrails'
    When the user adds the bookmark
    Then should redirect to '/'
    And the page should contain 'http://www.gotapi.com/rubyrails'

The first test (should redirect) passes. The second fails. I understand
why: the page that is analysed is a dummy page "You are being
redirected."

Is it possible to check data in the second page?

Cheers
Xtian
Posted by Zach Dennis (Guest)
on 07.08.2008 20:56
(Received via mailing list)
On Thu, Aug 7, 2008 at 2:44 PM, Christian Lescuyer 
<lists@ruby-forum.com> wrote:
> why: the page that is analysed is a dummy page "You are being
> redirected."
>
> Is it possible to check data in the second page?

What about something like:

  Scenario: User enters a bookmark URL
    Given I log in as a user
    When I add 'http://www.gotapi.com/rubyrails' as a bookmark
    Then I should see that has the 'http://www.gotapi.com/rubyrails'
has been added to my bookmarks

I'd imagine the steps to look something like:

    Given "I log in as a user" do
       # do whatever to login
    end

    When I add  $url as a bookmark do |url|
        # fill out the new bookmmark form with the given url
        # after submitting form handle redirects
        follow_all_redirects
    end

    Then "I should see that has the $url has been added to my
bookmarks" do |url|
       # response should have url somewhere on page
    end

follow_all_redirects looks like:

    def follow_all_redirects
      if response.content_type == "text/html"
        follow_redirect! while response.redirect?
      elsif response.content_type == "text/javascript"
        if md=response.body.match(/window.location.href = "([^"]+)"/)
          get md.captures.first
        end
      end
      true
    end



--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
Posted by Christian Lescuyer (xtian)
on 07.08.2008 23:07
Thanks! I will try this tomorrow.

Xtian
Posted by Christian Lescuyer (xtian)
on 08.08.2008 19:29
Thanks Zach! It worked like a charm.

Finally I used:

  Scenario: User enters a bookmark URL
    When the user adds 'http://www.gotapi.com/rubyrails' as a bookmark
    Then the database should have a bookmark with url 
'http://www.gotapi.com/rubyrails'
    And  the page should contain 'http://www.gotapi.com/rubyrails'

and in bookmark_steps.rb:

  When "the user adds '$string' as a bookmark" do |string|
    post "/bookmarks/create", :bookmark => { :url => string }
    follow_all_redirects
  end

  Then "the database should have a bookmark with url '$url'" do |url|
    Bookmark.find_by_url(url).should_not be_nil
  end

  Then "the page should contain '$stuff'" do  |stuff|
    response.should have_text(/#{stuff}/)
  end
end

def follow_all_redirects
  if response.content_type == "text/html"
    follow_redirect! while response.redirect?
  end
  true
end

I skipped the Javascript right now, I'll add it when the time comes.

Xtian