1

我需要测试一个两阶段登录系统,它首先询问您的电子邮件地址和密码,然后向用户显示两个包含 [a-zA-Z0-9] 的选择列表。下拉列表旁边的标签形式为“从您的安全短语中选择字符 X”,其中 X 是来自已知安全短语的随机字符索引。

我宁愿不为验收测试存根代码,所以是否可以在黄瓜中编写一个匹配器,鉴于我们知道整个短语,它会在两个列表中的每一个中选择所需的字符?

这是我到目前为止的场景和所涉及的步骤:

Scenario: valid login email, password and secret phrase takes me to the dashboard
  Given I am not logged in
  When I log in as "admin@example.com"
  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

例如,如果安全短语是 'Secret123',X = 3 和 Y = 8,上面将产生等价于:

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

实际页面中的数字 X 和 Y 分别在 span#svc_1 和 span#svc_2 内。

谢谢,

4

1 回答 1

1

折腾了好久终于弄明白了。在这里记录它,以便它可以帮助处于相同情况的其他人。在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
于 2010-05-18T15:19:40.433 回答