Can't test for page content after a redirect

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

Thanks! I will try this tomorrow.

Xtian

On Thu, Aug 7, 2008 at 2:44 PM, Christian L.
[email protected] 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 D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs