如何通过提供多个输入来自动化测试用例?

How to automate test cases by giving multiple inputs?

require 'rubygems'
require 'watir'
require 'watir-webdriver/wait'

username = 'abcd'
password = '1234'

Watir::Browser.new :firefox

puts 'Beginning of test: Yahoo Login.'
puts ' Step 1: go to the test site: ' + '<https://mail.yahoo.com>'
browser = Watir::Browser.start '<https://mail.yahoo.com>'

puts 'Step 2: enter username'
browser.text_field(id: 'login-username').when_present.set(username)
browser.text_field(id: 'login-username').wait_until_present

puts 'Step 3: enter password'
browser.text_field(:id, 'login-passwd').set(password)

puts 'Step 4: Check keep me signed in check box'
browser.checkbox(:id, 'persistent').clear

puts 'Step 5: Click sign in button'
browser.button(:value, 'Sign In').click

if browser.text.include? ' This ID is not yet taken. '
  puts 'Testcase01 pass : Enter a valid username '
  Kernel.exit(false)
end

if browser.text.include? 'Please enter your password'
  puts 'Testcase02 pass : password can not be blank'
  Kernel.exit(false)
end

browser.button(id: 'mail-search-btn').when_present
browser.button(id: 'mail-search-btn').wait_until_present

if browser.text.include? 'Hemanth'
  puts 'Testcase04 pass : ***Login successfull'
else
  puts '***Error in Login'
end

现在它完成了单个用户名和密码的工作,我需要循环它来检查 4 个测试用例?我的意思是我需要为上述脚本传递 4 种用户名和密码组合。我该怎么做?

这是测试用例 测试用例用户名密码登录
tc_yahoo_login_01 abcd xyz123 真
tc_yahoo_login_02 abcde abcd123 假
tc_yahoo_login_03 abc123 真
tc_yahoo_login_04 abcd 真
tc_yahoo_login_05 abcd abcd123 假
这些是我需要实施的测试用例,只有在成功登录后才会发生。你知道我想要得到什么输出吗?

下面的代码创建了一个名为 users 的 hash(其中包含 usernames/passwords),启动浏览器,迭代散列,并打印每个 key/value 对的键和值:

require 'watir-webdriver'
users = {"user_one" => "password", "user_two" => "password"}

b = Watir::Browser.new 
b.goto('http://www.example.org')
users.each do |key, value|
  puts key
  puts value
  b.refresh
end

#=> user_one
#=> password
#=> user_two
#=> password

要使用符号作为散列键,您可以执行相同的操作,但将键转换为字符串:

users = {user_one: "password", user_two: "password"}

b = Watir::Browser.new 
b.goto('http://www.example.org')
users.each do |key, value|
  puts key.to_s                    # convert symbol to string
  puts value
  b.refresh
end

还有一个工作演示:

require 'watir-webdriver'
users = {sample: "sample", sample1: "sample1", sample2: "sample2"} # hash of users
b = Watir::Browser.new                                             # launch browser
b.goto('https://foodclub.org/sample/login')
users.each do |key, value|                                         # iterate on hash
  b.text_field(name: "user_id").set  key.to_s                      # set key as username
  b.text_field(name: "password").set value                         # set value as password
  b.button(name: "login").click
  b.link(text: "Logout").click
end