How to Test Web Services with Ranorex

Jan 21, 2014 | Best Practices

Test Web Services With Ranorex

This blog post will illustrate how easy it is to test your web service using Ranorex. In detail you will add a web reference of your web service to your Ranorex Solution and validate the functionality of the service via user code actions.

To illustrate web service testing, we have chosen an easy to follow example based on sample web service provided by w3schools.com. Basically we will take a given temperature in Celsius, convert it to Fahrenheit and reconvert it back to Celsius. Finally we compare the calculated value with the initial one.

Flow Chart

What is a Web Service

The W3C defines a web service as a software system designed to support interoperable machine-to-machine interactions over a network. It has an interface described in a machine-processable format called the web service description language (in short: WSDL). Other systems interact with the web service by the mentioned description using SOAP messages. Those messages are typically conveyed via HTTP using XML serialization together with other web-related standards.

Summarizing we can say that it is an interface to quickly provide functionality over a network, such as adding two numbers and retrieving the result. For instance each type of cloud computing is using web services nowadays.

Ranorex allows adding web services as web references to your Ranorex solution. This means that you can access and in further consequence test web services using Ranorex.

Let’s now have a look on how you can test your web service based on the following web service example that converts Celsius to Fahrenheit (and vice versa):

http://www.w3schools.com/xml/tempconvert.asmx

Adding Web References to your Solution

Create a new Solution in Ranorex Studio. Add a web reference by right-clicking the References folder in Project View and choosing “Add Web Reference”.

Add Web Reference

Enter the URI of the web service to the address bar and press the “Go” button. The “WSDL” tab will automatically give you a description of the provided functionality. Enter a speaking reference name as well as a speaking namespace through which you want to access the service.

Enter URI

Accessing the Web Service

After adding the web reference you will see a new folder “Web References” within your “Projects” view. When opening the folder you will see the just added web service holding an automatically generated class “Reference.cs” including methods provided by the service.

Web Reference Files

Web Reference Code

The automatically generated class allows you to access the web service directly. There are two different approaches to do that: user code actions as well as code modules. In this blog we will treat the first approach user code actions in combination with validation actions and variables.

To start over, open an existing or create a new recording in your solution and add two variables, one holding the initial value named initialTemperature and one holding the calculated value named calculatedTemperature.

Add Variables

Add a new user code action and name it CelsiusToFahrenheit. Add an argument named temperature using the item argument editor. Bind the variable initialTemperature to the argument temperature.

Add User Code Action

Add Argument

Right-Click the added action and choose “View Code” to add functionality to your user code action.

View Code

Update the generated code as follows:

public partial class Recording1
    {
    	TempConvert.TempConvert converter;

        private void Init()
        {
        	converter = new TempConvert.TempConvert();
        }

        public void CelsiusToFahrenheit(string temperature)
        {
        	calculatedTemperature = converter.CelsiusToFahrenheit(temperature);
        }

}

We add a member converter (from namespace TempConvert and class TempConvert as defined when adding the web reference) to our recording class which has to be created in the Init method.

When calling the user code action CelsiusToFahrenheit, the value stored in variable initialTemperature will be taken as argument for the web service. The calculated temperature (return value of the web service) will be stored in the variable calculatedTemperature for further processing in the recording.

Testing the Web Service

Now, as we know how accessing a web service works, we want to validate whether or not our methods provided by the web service operate correctly. Therefore we add an additional user code action calling the FahrenheitToCelsius service as well as an action validating whether conversion and reconversion work as expected. Doing this, the user code might look as follows:

public partial class Recording1
    {
    	TempConvert.TempConvert converter;

        private void Init()
        {
        	converter = new TempConvert.TempConvert();
        }

        public void CelsiusToFahrenheit(string temperature)
        {
        	calculatedTemperature = converter.CelsiusToFahrenheit(temperature);
        	Report.Info(temperature + "°C = " + calculatedTemperature + "°F");
        }

        public void FahrenheitToCelsius(string temperature)
        {
        	calculatedTemperature = converter.FahrenheitToCelsius(temperature);
        	Report.Info(temperature+ "°F = " + calculatedTemperature + "°C");
        }

        public void AreEqual(string actual, string expected)
        {
        	Validate.AreEqual(actual, expected);
        }
    }

The recording and the corresponding report will then look like this:

Complete Recording

Report

As you can see in the given report, in the first step 10° Celsius are converted to 50° Fahrenheit. In the second step the calculated 50° Fahrenheit is converted back to 10° Celsius. The last step validates whether the initial value equals the calculated one (or not). In this very case the web service operates as expected since the validation was successful.

Conclusion

In the preceding sections, we saw that accessing and testing web services is pretty easy. All you need is a little bit of programming skills after the web service has been added to your solution as a web reference. To get the most out of your user code, also have a look at the corresponding chapters in our user guide:

Get a free trial of Ranorex Studio and streamline your automated testing tools experience.

Start your intelligent testing journey with a free DesignWise trial today.

Related Posts:

Test Design: What Is It and Why Is It Important?

Test Design: What Is It and Why Is It Important?

In software development, the quality of your tests often determines the quality of your application. However, testing can be as complex as the software itself. Poorly designed tests can leave defects undetected, leading to security vulnerabilities, performance issues,...

Ranorex Introduces Subscription Licensing

Ranorex Introduces Subscription Licensing

Software testing needs are evolving, and so are we. After listening to customer feedback, we’re excited to introduce subscription licensing for Ranorex Studio and DesignWise. This new option complements our perpetual licenses, offering teams a flexible, scalable, and...