views:

34

answers:

1

I need to test a two-stage login system which first asks for your email address and password and then presents the user with two select lists containing [a-zA-Z0-9]. The labels beside the drop down's are of the form 'Select character X from your security phrase', where X is a random character index from a known security phrase.

I'd rather not stub the code for an acceptance test, so is it possible to write a matcher in cucumber which will, given that we know the whole phrase, select the required character in each of the two lists?

Here is the scenario I have so far and the steps involved:

Scenario: valid login email, password and secret phrase takes me to the dashboard
  Given I am not logged in
  When I log in as "[email protected]"
  Then I should be on the dashboard page
  And I should see "Your Dashboard"

When /^I log in as "([^\"]*)"$/ do |login|
  visit path_to('Login page')
  fill_in "Email", :with => login
  fill_in "Password", :with => "Password123"
  click_button "Log in"
  response.should contain("Please verify some characters from your security phrase")
  select "a", :from => "Select character X of your security phrase"
  select "b", :from => "Select character Y of your security phrase"  
  click_button "Submit"
end

For example, if the security phrase is 'Secret123', X = 3 and Y = 8, the above would have to produce the equivalent of:

select "c", :from => "Select character 3 of your security phrase"
select "2", :from => "Select character 8 of your security phrase"

The numbers X and Y in the actual page are inside span#svc_1 and span#svc_2 respectively.

Thanks,

+1  A: 

After a lot of messing about I finally figured it out. Documenting it here so it can help someone else in the same situation. In general_steps.rb:

When /^I log in as "([^\"]*)"$/ do |email|
  visit path_to('Login page')
  fill_in "Email", :with => email
  fill_in "Password", :with => "password"
  click_button "Log in"
  response.should contain("Please verify some characters from your secret phrase")
  select_correct_secret_phrase_char("span#sec_1")
  select_correct_secret_phrase_char("span#sec_2")
  click_button "Submit"
end

def select_correct_secret_phrase_char(css_selector)
  response.should have_selector(css_selector) do |scope|
    select "Secret1"[(scope.text.to_i)-1].chr, :from => "Select character #{scope.text} of your security phrase"
  end
end
lawm