Ruby实现http自动化测试(四)------框架的雏形

经过前三节的讲解,一个HTTP的自动测试脚本已经差不多实现了。现在要做的就是执行从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
│  │
│  └─result
│          http_result.rb


main.rb:

require_relative './class_macro/http_method_macro'
require_relative './http_methods/http_methods'
require_relative '../autoHttpTest/excel/excel_manager'

class << self
  include HttpClassMacroModule
  include HttpMethodModule

  http_method :GET
  http_method :POST
  http_method :DELETE
  http_method :PUT

  def setup(&block)
    self.instance_eval {
      block.call
    }
  end

  def load_setup
    Dir.glob('./conf/setup*.rb').each do |file|
      load file
    end
  end
end

load_setup

excel = ExcelManager.new(@filepath)
excel.reverse_all_rows(@titleRow) do |row,rows|
  input = excel.get_cell_by_title(rows,'输入')
  expect = excel.get_cell_by_title(rows,'期望结果')
  result = eval(input)
  excel.write_cell_byTitle(row,1,'测试结果',eval(expect))
end

excel.quit_excel


class_macro/http_method_macro.rb:

module HttpClassMacroModule
  def self.included(base)
    base.extend HttpClassMacros
  end

  module HttpClassMacros
    def http_method(name)
      define_method(name) do |*args|
        @testCase = {}
        @testCase[:params] = args[0]
        @testCase[:request] = name.to_s

        op = name.to_s.downcase
        case op
          when "get" then
            httpGet(@testCase)
          when "post"
            httpPost(@testCase)
          when "put"
            httpPut(@testCase)
          when "delete"
            httpDelete(@testCase)
          else
             print "undefined http method:#{op}"
        end
      end
    end

  end
end

conf/setup.rb:

setup {
  @baseUrl = "http://www.baidu.com"
  @filepath = 'H:/testCase2.xls'
  @titleRow = 1
}


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
    select_sheet(1)
  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 write_cell(row,col,value)
    cell = col.to_s + row.to_s
    @worksheet.Range(cell).Value = value
  end

  def get_cell_byEncode(row, col, encode)
    cell = col.to_s + row.to_s
    data = @worksheet.Range(cell).Value
    data.encode(encode) if data.respond_to?(:encode)
  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 get_titles(row)
    titles = []
    for col in 'A'..'Z' do
      title = get_cell_byEncode(row, col, 'UTF-8')
      break if title.nil?
      titles << title
    end
    titles
  end

  def get_title_col(titles, title)
    col = 'A'
    titles.each do |value|
      if value == title then
        break
      else
        col = char_plus(col)
      end
    end
    col
  end

  def write_cell_byTitle(row,titleRow,title,value)
     titles = get_titles(titleRow)
     col = get_title_col(titles,title)
     write_cell(row,col,value)
  end

  def reverse_all_rows(titleRow=1, startRow=2, &block)
    titles = get_titles(titleRow)
    loop do
      result = reverse_one_row(startRow,titles)
      break if is_one_row_nil?(result)
      block.call(startRow,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


http_methods/http_methods.rb:

require 'net/http'
require 'uri'
require_relative '../result/http_result'

module HttpMethodModule

  def httpGet(options)
    params = options[:params]
    url = @baseUrl + params[:url]
    uri = URI.parse(url)
    req = Net::HTTP::Get.new(params[:url])
    Net::HTTP.start(uri.host) do |http|
      response = http.request(req)
      HttpResult.new(response)
    end
  end

  def httpPost(options)
    params = options[:params]
    p params
  end

  def httpPut(options)
    params = options[:params]
    p params
  end

  def httpDelete(options)
    params = options[:params]
    p params
  end
end


result/http_result.rb:

class HttpResult
  def initialize(respond)
    @respond = respond
  end

  def code
    code = @respond.code
    code.to_i
  end

  def body
    @respond.body
  end

end


程序的运行结果如下:

用例标题输入期望结果备注测试结果
GET_TEST_001GET :url=>'/index.html'result.code==200测试例1TRUE
GET_TEST_001GET :url=>'/index1.html'result.code==200测试例2FALSE

程序会读取'输入'并执行,再根据'期望结果'(也是ruby代码)的执行结果更新'测试结果'.

一个好的框架在于易于扩展,后面的章节,我们将这个框架的功能做的更多样化。可以达到如下效果:

1.方便的加入输入和期望结果中可以支持的DSL

2.方便的增加对其它测试方向的支持(现在只支持HTTP测试)

3.增加期望结果的复杂程度,便于更精确的判断测试结果。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

self-motivation

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值