Rest API Testing using C#

Rest API has become very popular and its nowadays, it is kind of must have skill set to have for any developer or tester. In Every other job description, you can see having experience with API with always preferred. REST API is most popular and you can see Facebook, Twitter, Flickr etc. are coming with APIs and you can build your own application based on that. All major products are providing API interface so developers can utilize and build own product based on the data provided by API services. In this article I will explain you how to use REST web services using C#. Along with explanations, I would keep writing different part of code There.




Rest stands for Representational State Transfer (ReST). It relies on stateless, client-server, cacheable communication protocol mainly running over HTTP protocol. Thus RESTful services are url based https://api.product.com/categories/machines/id/123

Consumers are capable of sending GET, POST, PUT and DELETE verbs and these verbs indicates you what the purpose of the request. There are many other verbs can be used with rest but these are being used heavily across the products.

Verbs:

GET:

To read specific resource or collection of resources.

POST:

create new resource

PUT:

Update a specific resource or collection of resources.

DELETE:

Remove specific resource based on identifier.

 

Resource:

You resource is in URL. Resources are viewed via their URI names, offering consumers a friendly name, easily understandable hierarchy of resource into that. There are some standard but restrict rule to have into resources structures.

  • a. identifier should be in url instead of into query. It’s good to have /serialnumbers/1234 instaed of /api?rnum=1234.
  • b. Resource name should be noun instead of verb else it may confuse with our request verbs.
  • c. Keep url as short as possible.
  • d. avoid hyphans(-) or underscores(_).

Response:


Response with status code is part of the HTTP specification. There are tons of status code you can find on internet. Refer w3.org or RestAPITutorial. I will list my Top 5 which you should memorize and this can be part of your interview too.

  1. 200 OK: Success code.
  2. 201  CREATED: Successfully created happened using POST/PUT.
  3. 401 UNAUTHORIZED:  In response of missing token or invalid authentication.
  4. 404 NOT FOUND: This error occurred when resource not found.
  5. 500 INTERNAL SERVER ERROR: Usually this happens when server is down.

Moreover, RESTful services also support JSON and XML. You can get output in either format.

Usage:

 

Writing Rest Client in C#:

Lets start with C#. First we need to think what we want. It’s simple, we want to create a client at our end where we can use REST api to access or manipulate different type of resources via url because all resources are mapped with url only. Now you solved many things. Read again previous lines and you will find some special words, ‘access or manipulate resource’, ‘url’ and ‘different type of resource’. Since REST is url based and you access resource via url then you call the url as ‘EndPoint’. Type of resources are termed as ‘Content Type’ and access are termed as ‘httpVerb(get/post/put/delete/….)’  and if you need some data to manipulate then this will be termed as ‘Post Data’.

first we will try to access some api via browser or using API client as chrome extension and see the output and later we will write code to replicate the same. One the Rest API service I found is http://restcountries.eu/ this is one of the most simplest REST API I have seen where you do not need to register or use app id to access resources.  We will use these API to learn how to access via client and try to write code which can accommodate both simple and complex type of APIs.

Install PostMan Rest Client in you chrome browser. Open PostMan and supply end point to get Calling Code as given in http://restcountries.eu/https://restcountries.eu/rest/v1/callingcode/65

 

image

you can notice above that you are using verb GET from end point “” and once you click on send button you are getting response in JSON. You can change the format from that drop down and see the response.

based on above screenshot you lets design the client via code. You need to create first client class and this client class should have above attributes.

httpVerb –> GET/POST/PUT/DELETE

endpoint –> some url

ContentType –> json/xml/html

since we need multiple verbs so we can have enum of httpVerb.


public enum httpVerb
{
GET,
POST,
PUT,
DELETE
}

Now create client class  and have all properties into that class that required for accessing resources.

to for and have multiple constructor using method overloading just in case if you like to


public class Client
    {
        public string EndPoint { get; set; }
        public Verb Method { get; set; }
        public string ContentType { get; set; }
        public string PostData { get; set; }

        public Client()
        {
            EndPoint = "";
            Method = Verb.GET;
            ContentType = "application/JSON";
            PostData = "";
        }

        public Client(string endpoint, Verb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/json";
            PostData = postData;
        }

}

Once you have object you need methods to perform the the actions on the object. You need to write a method MakeRequest which should perform real action utilizing all variable in that object. Lets work on GET/POST request first.

public string Request(string parameters)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;
            
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Faile: Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }

                return responseValue;
            }
        }

Now its time to call the class and create rest client object and call method to access to your object.

 

In your main method, create client object and supply required info and call appropriate method.


var client = new Client();
client.EndPoint = @"<a href="https://restcountries.eu&quot;;">https://restcountries.eu";</a>
client.Method = Verb.GET;
var pdata = client.PostData;
var response = client.Request("/rest/v1/currency/eur");

You can see parse response and get your desired output in JSON.

You can find complete code here.

 


using System;
using System.IO;
using System.Net;
using System.Text;

public enum Verb
{
    GET,
    POST,
    PUT,
    DELETE
}

namespace RestAPITesting
{
    class Program
    {
        static void Main(string[] args)
        {
           
            var client = new Client();
            client.EndPoint = @"https://restcountries.eu";
            client.Method = Verb.GET;
            var pdata = client.PostData;
            var response = client.Request("/rest/v1/currency/eur");
        }
    }
    public class Client
    {
        public string EndPoint { get; set; }
        public Verb Method { get; set; }
        public string ContentType { get; set; }
        public string PostData { get; set; }

        public Client()
        {
            EndPoint = "";
            Method = Verb.GET;
            ContentType = "application/JSON";
            PostData = "";
        }

        public Client(string endpoint, Verb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/json";
            PostData = postData;
        }


        public string Request()
        {
            return Request("");
        }

        public string Request(string parameters)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;
            
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Faile: Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }

                return responseValue;
            }
        }

    }
}

Recording Selenium Test Execution

After long time, got few min to think about new blog. There are many blog available for each and every solution but I was thinking to have few which is either not available or very limited information available over internet. I have free version of sauce lab and I am always fascinated by this kind of tool and I would love to build such tool sometime in future. There is one feature that is recording the session is always attracted me. I tried to find some resources which can help me to achieve so in my framework.

I found Microsoft expression encoder to do recording your screen. You can use their dlls to add into your C# based framework to execute and record the session. In you framework you can write a method which starts recording since beginning and later during cleanup task, you can have some login to determine if you want to save recording or not. For example, I my framework, I start recording based on my configuration file and in teardown methods, I check if there is an error or not. if yes then I encode into wmv format else I discard recording.




Follow following steps to have code and settings into framework:

Step 1:

Install Microsoft Encoder Expression: Refer CerebrumLabs.com for that (http://www.cerebrumlabs.com/free-screen-recording-software/). Once you install, you may get following folder in installation location. C:\Program Files (x86)Microsoft Expression. If you try to explore few folders inside this folder you will find SDK folder @: C:\Program Files (x86)\Microsoft Expression\Encoder 4\SDK, where you can see Microsoft.Expression.Encoder.dlls which you can use in your framework code.

Step 2:

Include dlls given in SDK folders into your framework. You can write a method and call that. I have a key value pair in my framework config file and I can decide if I want to do recording or not.  In your test’s [setup] method, you can call this method which will start recording at the beginning of the test.

using System;

.

.

using OpenQA.Selenium;

.

.

using Microsoft.Expression.Encoder.ScreenCapture;

using System.Drawing;

using Microsoft.Expression.Encoder.Profiles;

using Microsoft.Expression.Encoder;

namespace FRAMEWORK

{

    //Call this method in setup method.   
    public static void StartRecordingVideo()

    {

        //Provide setting in config file if you want to do recording or not.
        if (testEInfo.isRecording)

        {

            job = new ScreenCaptureJob();

            job.CaptureRectangle = Screen.PrimaryScreen.Bounds;

            job.ShowFlashingBoundary = true;

            //provide the location where you want to save the recording.
            job.OutputPath = AutomationLogging.newLocationInResultFolder;

            job.Start();

        }

    }

}

Step 3:

Now your test started and in the background your screen recording going on. once you reach to the [teardown] method. you can decide if you want to keep the recording or not. In my case I want to keep recording only if there is test failure so my developers can review else there is no point of having recording if tests are passing. To do so I have following method in above code which I call in code at the very end.

public static void StopRecordingVideo()
        {
            if (testEInfo.isRecording)
            {
                string filename = job.ScreenCaptureFileName;
                job.Stop();
                if (AutomationLogging.countOfError > 0)
                {
                    MediaItem src = new MediaItem(filename);
                    Job jb = new Job();
                    jb.MediaItems.Add(src);
                    jb.ApplyPreset(Presets.VC1HD720pVBR);
                    jb.OutputDirectory = AutomationLogging.newLocationInResultFolder;
                    string output = ((Microsoft.Expression.Encoder.JobBase)(jb)).ActualOutputDirectory;
                    jb.Encode();
                }

                File.Delete(filename);
            }
        }

Conclusion:

during encoding, you may notice that encoder is eating little more memory and you system may little slow. Try at your end and let me know if you have any question.

API Testing

API stands for Application Programming Interface. API enables two separate software system to communicate with each other. API contains methods and routines used by the system that other systems call and get the result to consume in the own system.

API testing is totally different from GUI Testing and it focuses on Applications business logic layer.  This testing does not care about the look and feel of the application.

Setup of an API Environment:

Usually, setting up the API Test Environment is not that easy because it doesn’t involve any user Interface.  You need to configure the environment based on the application requirements. You need to study the web service document provided by developers and then design the setup. Using API code, you should make the specific call to API and consume the output for doing verification or other depended API calls.Output could be status (say OK or Error) or any kind of Data (object id) that to call another API method.

Ideally API should catch and handle the problem instead of just failed. If the input is not correct it will let you know the status with great detail like ‘Incorrect request” etc. In a very easy way, I can say, you send a request message to web services and after processing, web service sends you back a response based on that input. It could be ‘Bad Request’ if the message sent incorrectly and contains response in a specific structure based on the architecture of the web services. This response should contain enough information to understand the output and correct if there is some problem with the request.

To understand the API call, let’s see a basic example;

Consider, API functions, which are for an e-commerce web service cart web services;

AddItemToCart(ItemType item, float price, int count)

Once you look at the method, AddItemToCart, you need to pass an item of type ‘ItemType’, price in ‘float’ and count of items in ‘int’ data type to add items to the cart. The output should capture all output message and you need to parse them and get the desired response.

Approach to API Testing:

For say, if you going to test end to end scenario for a purchasing an item, you would be using few API calls.

  1. AddItemIntoCart()
  2. Checkout(), if Item added to Cart.
  3. MakePayment() if you are able to add the item to checkout step.
Based on output response of each API method you proceed to the Next.

API Test Tools:

There are various free and paid tools available in the market, which are specifically designed to test various API for the database application, mobile apps, desktop apps etc. You can download free tools as an extension or as standalone and access web services via the tool. These tools can emulate all or part of the environment in which the API would typically operate. It is not necessary that the tool can emulate all the API calls you need, but I am sure you will find the tool that can do most as per your need. SOAPUI, POSTMAN, Parasoft soaTest are few tools, which you can try.