如何测试需要从安全短语输入两个随机字符的表单?

时间:2010-05-12 15:00:21

标签: cucumber bdd webrat

我需要测试一个两阶段登录系统,该系统首先询问您的电子邮件地址和密码,然后向用户显示包含[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内。

谢谢,

1 个答案:

答案 0 :(得分: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