Ruby implements HTTP Automation testing (II)-----Implementing HTTP Methods

Source: Internet
Author: User
Tags prefetch

In this section, we continue with the previous section to add the ability to send HTTP requests for our automation tools. The finished code is structured as follows:


1. First we added a conf directory, which is used to store the global configuration, such as the home page of the site to be tested, user name password and other basic information.

The code for SETUP.RB is as follows:

Setup {
@baseUrl = "Http://www.baidu.com"
}

The current function is very simple, just define the website we want to test the homepage, here to Baidu as an example. The question then is how to load this configuration into our main object, making it visible to the main object.


2. The MAIN.RB code is as follows:

Require_relative './class_macro/http_method_macro '
Require_relative './http_methods/http_methods '

Class << Self
Include Httpclassmacromodule
Include Httpmethodmodule

Http_method:get
Http_method:P OST
Http_method:D elete
Http_method:P UT

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"

The red part is that we implement the auto-load configuration and define the configuration as an instance variable of the main object. Unlike Java, Java typically parses an XML file and transforms the parsed configuration into an object or variable. We're in Ruby.

The configuration defined in the definition is changed directly into the variable of the main object.

The implementation method is as follows:

A. First define the Setup method, which is the instance method of main and the parameter is a block. This setup method simply executes the block in the context of the current object, so that if the variable is defined in the block, it automatically becomes the variable of the current object; Similarly, if the definition method becomes the method of this object.

B. Then we define the Load_setup method, which automatically loads all the configuration files under the Conf directory and executes.

C. In this way, we can define the object in the configuration file, variable operations, like writing code in the configuration file.


3. Then we implement the specific HTTP operation in http_methods/http_methods.rb , the code is as follows:

Require ' net/http '
Require ' URI '

Module Httpmethodmodule

def httpget (options)
params = options[:p Arams]
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[:p Arams]
P params
End

def httpput (Options)
params = options[:p Arams]
P params
End

def httpdelete (Options)
params = options[:p Arams]
P params
End
End


Here we only implement the get operation, if there are other testing needs, you can expand themselves. We parse the URL from the parameters, send a specific HTTP request, and print the returned content. The print here is just for testing.


4. We extend the class macro Http_method of the previous section to send specific HTTP requests in specific get,post,delete,put methods. The class_macro/http_method_macro.rb code is as follows:

Module Httpclassmacromodule
def self.included (Base)
Base.extend Httpclassmacros
End

Module Httpclassmacros
def http_method (name)
Define_method (name) do |*args|
@testCase = {}
@testCase [:p arams] = args[0]
@testCase [: request] = name.to_s

OP = name.to_s.downcase
Case OP
When the "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
We parse the input of the GET test case (get:url=> "/index.html") into the hash table of @testcase and pass it to the specific HTTP function, which is parsed by the HTTP function and sends the HTTP request.


The result of the final program operation is as follows:

"<! DOCTYPE html><!--STATUS ok-->
Process finished with exit code 0

In this section, we implemented the auto-load global configuration and translated the input of the test case into a specific HTTP request and sent it.

In the next section, we will implement the function of Operation Execl, parse the test case from Excel and execute it.

Ruby implements HTTP Automation testing (II)-----Implementing HTTP Methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.