Wednesday, June 18, 2014

QTP / UFT - Testing without Web Services Add-in (using WinHTTP)

QTP / UFT - Testing without Web Services Add-in (using WinHTTP)

We can test web services with QTP / UFT without the need to use web services add-in. This can be done with WinHTTP.

Below I have tried to explain the examples on "QTP / UFT WinHTTP" in-depth. You can learn more about WinHTTP and WinHttpRequest objects from the links given at the end of the post.

Lets straightaway start with QTP / UFT WinHTTP examples:

Example 1:

Below code verifies whether the given link (url) is working or not.

Few lines explaining the below code:
We are creating WinHttpRequest object. Then we are calling its open method. The first parameter to open method is "Method", which specifies HTTP verb to use such as GET, POST etc. Second parameter is the URL and third is Async (optional) which specifies whether to open the HTTPS connection in synchronous (FALSE) or asynchronous (TRUE) mode.

Next the SetRequestHeader method adds, changes, or deletes an HTTP request header. First parameter is the name of the header to be set and second is the value of the header. It returns S_OK on success or an error value otherwise.

Send method sends an HTTP request to an HTTP server. Return value is S_OK on success or an error value otherwise.

[The Open method does not establish a connection to the resource as the name might imply. Rather, it initializes the internal data structures that maintain information about the session, connection, and request.
The Send method assembles the request headers and sends the request. When called in synchronous mode, the Send method also waits for a response before allowing the application to continue.
After sending the request, the script returns the value of the ResponseText property of the WinHttpRequest object (see Example 2). This property contains the entity body of the response.
]

Lastly we are capturing the return value and closing the object.



The above code will return the result in message box as "Hurray, the link - http://www.google.com/ exists".

Example 2:

Here we will test the web service exposed by http://www.w3schools.com at http://www.w3schools.com/webservices/tempconvert.asmx.

Web services exposed here converts temperature from Celsius To Fahrenheit or vice versa.

Below code converts temperature from Celsius To Fahrenheit.

In the below example CelsiusToFahrenheit request is being sent to the server. The parameter which is being sent in the request is Celsius with a value of 30. The request is being saved in an XML file (a.xml in our case) and converted to string. Then, similar to the above example, the WinHttpRequest object is created and request is sent. After receiving the response, its shown in the message box.

The SOAP action (line 10) communicates what action should be performed. Here we are converting value which is in Celsius To Fahrenheit.



The "a.xml" file in our case.


The above code will return the result in message box as 86, which is the result of converting 30 degree Celsius to Fahrenheit.

Example 3:

In this below example we are using HPFlights_Service which comes with UFT / QTP. For this below example, Service (HPFlights_Service) needs to be running. The location of the service on your machine will be:
C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight_service

When the service is running (see below image), type h at the command prompt and press enter. The "Service Test Sample Flight Application" page opens which contains operations which the web service contains.



There are many operations which you can try. Below we are creating a flight order by passing the flight information like flight number, departure date etc (in b.xml file in our case) and receiving the response as you can see below. As we have done in the above code (Example 2) you can extract the required exact information from the response.

This below screenshot was captured during run time and also shows the resultant message box:


A "b.xml" file:


Example 4:

If you have understood the above code you will be able to understand this example also. There are some differences though like the above code uses POST (submits data to be processed to a specified resource) and this one uses GET (requests data from a specified resource).

We are requesting the information for flight number 1304 here.

In the below code we are using URL: http://localhost:24240/HPFlights_REST/Flights/{FlightNumber} from "Service Test Sample Flight Application" page and we will get the response accordingly.

This below screenshot was captured during run time and also shows the resultant message box:


Similarly you can use other operations (like Delete, ReserveOrder etc.) from this "Service Test Sample Flight Application" page.

You can learn more about WinHttpRequest object and Webservices.