Ruby实现http自动化测试(三)------Excel

来源:互联网 发布:现货软件下载 编辑:程序博客网 时间:2024/05/17 01:27

这一节我们实现用Ruby读取Excel的功能。

一般情况下,我们的测试例都写在Excel,所以实现自动化测试,读取Excel是必不可少的功能。我们先实现读取Excel的功能。

代码结构如下:

├─autoHttpTest
│  │  main.rb
│  │
│  ├─class_macro
│  │      http_method_macro.rb
│  │
│  ├─conf
│  │      setup.rb
│  │
│  ├─excel
│  │      excel_manager.rb
│  │      test_excel.rb
│  │
│  └─http_methods
│          http_methods.rb

和上一节相比,主要是增加了excel目录,用于操作excel.

excel_manager.rb:

require 'win32ole'

class ExcelManager

  def initialize(path, visible=false, encode='UTF-8')
    @excel = WIN32OLE::new('excel.Application')
    @workbook = @excel.Workbooks.Open(path)
    @excel.Visible = visible
    @encode = encode
  end

  def select_sheet(sheet)
    @worksheet = @workbook.Worksheets(sheet)
    @worksheet.Select
  end

  def get_cell(row, col)
    cell = col.to_s + row.to_s
    data = @worksheet.Range(cell).Value
  end

  def get_cell_byEncode(row, col, encode)
    cell = col.to_s + row.to_s
    data = @worksheet.Range(cell).Value
    data.encode(encode) unless data.nil?
  end

  def char_plus(c)
    c_asc = c[0].ord
    c_asc += 1
    c_asc.chr
  end

  def reverse_one_row(row, titles)
    results = {}
    col = 'A'
    titles.each do |title|
      data = get_cell_byEncode(row, col, 'UTF-8')
      results[title] = data
      col = char_plus(col)
    end
    results
  end

  def is_one_row_nil?(rows)
    is_nil = true
    rows.each do |key,value|
      if !value.nil? then
        is_nil = false
        break
      end
    end
    is_nil
  end

  def reverse_all_rows(titleRow=1, startRow=2, &block)
    titles = []
    for col in 'A'..'Z' do
      title = get_cell_byEncode(titleRow, col, 'UTF-8')
      break if title.nil?
      titles << title
    end

    loop do
      result = reverse_one_row(startRow,titles)
      break if is_one_row_nil?(result)
      block.call(result)
      startRow += 1
    end
  end

  def get_cell_by_title(result,title)
     result[title]
  end

  def prt_one_row_by_title(result,col)
    puts result[col]
  end

  def prt_one_row(result)
    result.each do |key, value|
      print "#{key} => #{value}  "
    end
    print "\r\n"
  end

  def quit_excel
    @workbook.close
    @excel.Quit
  end
end


根据函数名,应该比较容易理解函数的功能。我们再写一个test测试这个类的功能。我们要操作的Excel实际内容如下:

<用例标题输入期望结果备注 GET_TEST_001GET :url=>'/index.html'获取到主页测试例1 GET_TEST_001GET :url=>'/index1.html'获取到主页1测试例2 

test_excel.rb:

require_relative '../excel/excel_manager'

path = 'H:/testCase.xls'
obj = ExcelManager.new(path,true)

obj.select_sheet(1)
obj.reverse_all_rows(1,2) do |result|
  obj.prt_one_row(result)
end
obj.quit_excel

测试代码中,我们先选择第一个工作簿,然后遍历所有的行并打印。最后退出。运行效果如下:

C:\Ruby200-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) H:/rubyWork/autoHttpTest/excel/test_excel.rb
用例标题 => GET_TEST_001  输入 => GET :url=>'/index.html'  期望结果 => 获取到主页  备注 => 测试例1  
用例标题 => GET_TEST_001  输入 => GET :url=>'/index1.html'  期望结果 => 获取到主页1  备注 => 测试例2  

Process finished with exit code 0

结合上一节实现的功能,我们就可以将测试用例的输入从EXCEL中读出来了,然后就是在ruby程序里执行的问题了。下一节继续吧。

0 0
原创粉丝点击