0

I am using watir-webdriver. Here is my step def, can someone correct me what I am doing wrong here. I am trying to sign up using a set of details. If first set of details are already used I want it to use the second set of details and so forth. Unfortunately when the test runs, it only seems to use the first and 2nd set of details. It won't move onto the third set. Please help or advise.

Given /^I have signed up as a lender'$/ do
  @browser.link(:text, 'Join').click
  @browser.radio(:id, 'user_group_name_lenders').set
  @browser.text_field(:id, 'user_username').set('qw@qw.com')
  @browser.text_field(:id, 'user_username_confirmation').set('qw@qw.com')
  @browser.text_field(:id, 'user_login_password').set('qwe123')
  @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')
  @browser.button(:value, 'Sign up').click
  if @browser.h1(:text, 'Investor signup').exists?
    @browser.text_field(:id, 'lender_lender_profile_attributes_first_name').should exist
  elsif @browser.label(:text, 'has already been taken').exists?
    @browser.radio(:id, 'user_group_name_lenders').set
    @browser.text_field(:id, 'user_username').set('we@we.com')
    @browser.text_field(:id, 'user_username_confirmation').set('we@we.com')
    @browser.text_field(:id, 'user_login_password').set('qwe123')
    @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')
    @browser.button(:value, 'Sign up').click
    @browser.h1(:text, 'Investor signup').exists?
  elsif @browser.label(:text, 'Email *').exists?
    @browser.radio(:id, 'user_group_name_lenders').set
    @browser.text_field(:id, 'user_username').set('er@er.com')
    @browser.text_field(:id, 'user_username_confirmation').set('er@er.com')
    @browser.text_field(:id, 'user_login_password').set('qwe123')
    @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')
    @browser.button(:value, 'Sign up').click
    @browser.h1(:text, 'Investor signup').exists?
  elsif @browser.label(:text, 'Password *').exists?
    @browser.radio(:id, 'user_group_name_lenders').set
    @browser.text_field(:id, 'user_username').set('rt@rt.com')
    @browser.text_field(:id, 'user_username_confirmation').set('rt@rt.com')
    @browser.text_field(:id, 'user_login_password').set('qwe123')
    @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')
    @browser.button(:value, 'Sign up').click
    @browser.h1(:text, 'Investor signup').should exist
  else @browser.link(:text, 'Terms & Conditions').exists?
    @browser.radio(:id, 'user_group_name_lenders').set
    @browser.text_field(:id, 'user_username').set('ty@ty.com')
    @browser.text_field(:id, 'user_username_confirmation').set('ty@ty.com')
    @browser.text_field(:id, 'user_login_password').set('qwe123')
    @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')
    @browser.button(:value, 'Sign up').click
    @browser.h1(:text, 'Investor signup').should exist
  end
end
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
Azher
  • 493
  • 5
  • 18
  • 1
    Your indentation is very odd here, it makes the code difficult to read. You should check this out http://web.njit.edu/all_topics/Prog_Lang_Docs/html/ruby/syntax.html#control for an example of correct code formatting for if/else stamements. – CBA Dec 04 '12 at 15:46
  • What is the third set of details? Is it the 'er@er.com' user name? – Justin Ko Dec 04 '12 at 16:09
  • Yes you are correct. The third set of details are 'er@er.com' – Azher Dec 04 '12 at 16:37
  • If this solution is incorrect, can someone advise me or set me in the right direction on how to achieve my goal. It will be very much appreciated. Thanks – Azher Dec 04 '12 at 17:26
  • Thank you once again Željko. I will get it right next time – Azher Dec 05 '12 at 09:22

1 Answers1

1

I think you are misunderstanding how an if-elsif-else statement works. Only 1 of the if/elsif/else chunks of code will be executed. In other words, the statement will never try "we@we.com" and if it fails try "er@er.com".

If the intent is simply to try each of the users until a successful sign up occurs, you could just iterate over the user names:

Given /^I have signed up as a lender'$/ do
    user_names = ['qw@qw.com', 'we@we.com', 'er@er.com', 'rt@rt.com', 'ty@ty.com']

    user_names.each do |user_name|
        @browser.radio(:id, 'user_group_name_lenders').set
        @browser.text_field(:id, 'user_username').set(user_name)
        @browser.text_field(:id, 'user_username_confirmation').set(user_name)
        @browser.text_field(:id, 'user_login_password').set('qwe123')
        @browser.text_field(:id, 'user_login_password_confirmation').set('qwe123')      
        @browser.button(:value, 'Sign up').click

        break if @browser.h1(:text, 'Investor signup').exists?
    end
    @browser.text_field(:id, 'lender_lender_profile_attributes_first_name').should exist
end

This will try each user name in turn, until either one works or you run out of user names.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Thank you again Justin. I can see I was approaching this issue from a wrong angle. Your code works well and makes a lot of sense. Once again thanks. – Azher Dec 05 '12 at 09:16