Ruby實現Http自動化測試(二)-----實現http方法

來源:互聯網
上載者:User

標籤:

這一節,我們繼續上一節的內容,為我們的自動化工具添加發送HTTP請求的功能。完成後的代碼結構如下:


1.首先我們增加了一個conf目錄,這裡用來存放全域配置,如要測試的網站的首頁,使用者名稱密碼等基本資料。

setup.rb的代碼如下:

setup {
  @baseUrl = "http://www.baidu.com"
}

目前功能還很簡單,只是定義了我們要測試的網站首頁,這裡以百度為例。然後問題就是怎樣將這個配置載入到我們的main對象裡,使其對main對象可見。


2.main.rb代碼如下:

require_relative ‘./class_macro/http_method_macro‘
require_relative ‘./http_methods/http_methods‘

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

GET :url=>"/index.html"

紅色部分就是我們實現自動載入配置,並將配置定義為main對象的執行個體變數。和JAVA不同,JAVA一般要解析XML檔案,並將解析出的配置轉換為對象或變數。我們在ruby

裡定義的配置就直接變成了main對象的變數。

實現方法如下:

a.首先定義setup方法,這個方法為main的執行個體方法,參數為一個block.這個setup方法就只是將這個block在當前對象的上下文中執行了一下,這樣這個block中如果定義變數的話,就自動變為當前對象的變數了;同理,如果定義方法就變成這個對象的方法了。

b.然後我們定義load_setup方法,這個方法自動載入conf目錄下的所有設定檔,並執行。

c.這樣,我們就可以在設定檔中對對象進行定義方法,變數各種操作,就像在設定檔中寫代碼一樣。


3.然後我們在http_methods/http_methods.rb中實現具體的http操作,代碼如下:

require ‘net/http‘
require ‘uri‘

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)
      p response.body
    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


這裡我們只實現了get操作,如果有其它測試需要,可以自己擴充。我們從參數裡解析出url等資訊,發送具體的HTTP請求,並列印返回的內容。這裡的列印只是為了測試。


4.我們擴充上一節的類宏http_method,在具體的GET,POST,DELETE,PUT等方法中發送具體的HTTP請求。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
我們將GET測試案例的輸入(GET :url=>"/index.html")分析到@testCase這個hash表裡後,傳遞給具體的http函數,由http函數解析並發送HTTP請求。


最後程式運行結果如下:

"<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"><meta content=\"always\" name=\"referrer\"><link rel=\"dns-prefetch\" href=\"//s1.bdstatic.com\"/><link rel=\"dns-prefetch\" href=\"//t1.baidu.com\"/></body></html>\r\n"

Process finished with exit code 0

這一節,我們實現了自動載入全域配置,並將測試案例的輸入轉換為具體的HTTP請求並發送。

下一節,我們將實現操作EXECL的功能,從EXCEL中解析測試案例並執行。

Ruby實現Http自動化測試(二)-----實現http方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.