Logging in with ruby/cucumber

893 views Asked by At

I'm new to Ruby and Cucumber, but I'm using it to test high level functionality in my web app. I've mostly got the idea of how it works, but I'm struggling with one bit. I'm using a background in the scenario file to say: Given a user is logged in and this are the steps:

Given /^I am logged in as "([^"]*)" with password "([^"]*)"$/ do |user, password|
  visit "http://www.website.com:82/login"

  #Is there a 'logout'?
  if (response_body contain('Login')) then
    fill_in "email", :with => user
    fill_in "password", :with => password
    click_button "Login"
  end
  response_body.should contain("Your Accounts")
end

I'm having trouble with the if. I want to say 'If not logged in, log in' but not sure whether to use the webrat object (if that's what response_body is) or something else!

Thanks

Mike

2

There are 2 answers

0
raidfive On BEST ANSWER

I probably would have went with something like (seems a bit cleared to me than throwing conditionals into Cucumber):

Given /^I am logged in as "([^"]*)" with password "([^"]*)"$/ do |user, password|
  visit destroy_session_path # make sure you are logged out

  visit "http://www.website.com:82/login"
  fill_in "email", :with => user
  fill_in "password", :with => password
  click_button "Login"

  response_body.should contain("Your Accounts")
end
0
Mike On

So, I changed it to this, and it worked:

Given /^I am logged in as "([^"]*)" with password "([^"]*)"$/ do |user, password|
  visit "http://www.website.com:82/login"

  #Is there a 'logout'?
  if (response_body.match('Login')) then
    fill_in "email", :with => user
    fill_in "password", :with => password
    click_button "Login"
  end
  response_body.should contain("Your Accounts")
end