Friday, 6 September 2013

HTML 5 Web Sockets

HTML 5 introduced a new specification for the Bi-Directional communication or the Full Duplex connection between the Server and the client. It is the one of the most awaited technology after AJAX which solves the various problems of connection management and communication between the server and the client.
Web Sockets are basically exposed by JavaScript in HTML 5; also it can work independent of the technologies on Client and Server, almost all the browsers support the HTML 5 Web sockets feature as of now (All the latest versions) and the only restriction on server side is that server should support Web Sockets connection. 

HTML 5 Web Sockets helps in achieving real time communication on stateless model of Web, although earlier we used to achieve this by some conventional techniques like polling, long polling, etc. or with the help of some other libraries like SignalR (Which is nowadays known as ASP.Net SignalR and is also recommended by Microsoft).

To achieve real time communication (required in Gaming applications, chat engines etc.) between client and the server (Stateless model), we actually used to poll the server for the updated response every few seconds (Polling and Long Polling, I will explain both in a moment). The problem with this technique was that for every request that is sent to the server from client, there is a Request and Response object created for that, in addition to this authentication/authorization and many other things comes into picture for each and every request. Also the creating request and Response object in itself is a very heavy process, and the whole page is sent back as response to the client which includes headers and much other unnecessary stuff which becomes overhead. With HTML 5 Web sockets all of these overheads are minimized.

Conventional Techniques of Achieving Real-Time Communication


Polling – Polling is a technique in which Client keeps on sending a HTTP request to the server after a set time of interval and the use the response coming from the server to update the information. So there is always some lag in the display of the updated information and additionally server treats every single request as a new request and thus it includes overhead. One of the Major disadvantages is that even if the update is not available on the server, client will keep polling to the server for updated information. In this technique there is no way you can configure client to make request to the server when update is available because of the stateless model on which the Web works upon.

Long Polling – Long Polling is a technique in which Client keeps on sending a HTTP request to the server and then Server waits for set amount of interval to check if there is any updates available which it needs to send to the client, if so then it created the response and sends the updated response to the client otherwise it sends the response to the client to end the request. It gives us some benefits if the updates are not that frequent but if the updates are very frequent say every few m-seconds or every second then it is of no use over Polling.

To overcome this overhead came, HTML 5 Web Sockets to the rescue. Here I will explain how to work with HTML 5 Web Sockets with a sample application. Apart from all the conventional techniques of achieving Real-Time communication which works on HTTP Protocol, Web Socket Connection and communication works on Web Socket protocol.

Web Socket Attributes:


Socket.readyState – This tells us the actual state of the Socket, if the connection is opened, closed or not. State is defined with the help of Integer values (0 – Connection is not established till now, 1 – Connection Established, 3 – Connection is closed)

Web Socket Events:

  1. Open – Fired when connection is opened
  2. Message – Fired when message is received from the server
  3. Error – Fired when there is some error which have occurred in the socket connection
  4. Close – Fired when the connection gets closed

Web Socket Methods:

  1. Send – Used for sending data
  2. Close – Used for closing the connection

Here in this sample I will create a HTML Page on HTML 5 standards and on server side I will create a HTTP Handler to which client will make Web Socket request and this handler will support the Web Sockets request or connections (For this one thing is mandatory that the Server should support or accept the Web Sockets Request). The sample I am creating is a type of Chat Engine (I will let you know how to get it tested without making much effort, Please remember this is a sample application to show you how to use of WebSockets, and is not the best implementation to achieve this chat functionality in real applications, you can always have better design then this for your applications).

It will be good; I think to create a HTTP Handler first. All my .Net/c# code for HTTP Handler will be based upon .Net Framework 4.5 hosted on IIS 8. This is because IIS 8 provides the low level support for Web Socket Connections and .Net 4.5 provide us some managed libraries (System.Web.WebSockets) for Web Socket objects.

Creating a HTTP Handler

Note: You can find detailed comments inline.

Open Microsoft Visual Studio 2012 -> Add New Project (Class Library). Here I have created a project of type class library named “HandlerProject”. I have created a class in this project name “WebSocketHandler” which inherits from “HttpTaskAsyncHandler”, In .Net 4.0 when we used to create a HTTP Handler we used to inherit it from an interface “IHttpHandler”, but now .Net 4.5 provides a way to create a Async handler by just inheriting this base class where we need have to implement “BeginProcessRequest” and “EndProcessRequest” instead we just have to focus on “ProcessRequestAsync”, Also I have used a new feature of .Net 4.5 async/await(If you don’t know about this feature, please refer this link: http://www.codeproject.com/Articles/599756/Five-Great-NET-Framework-4-5-Features as this is out of the scope of my article). In this Class Library we need to import reference of DLL “System.Web”.

Class Library Code:


using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.WebSockets;

namespace HandlerProject
{
    //Handler Class which is main entry point.
    public class WebSocketHandler : HttpTaskAsyncHandler
    {
        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        //Socket Object, Although i have created a Static Dictionary of Scoket objects just to show the sample working. What i do is create this Socket object for each user and
        //keeps it into the dictionary. You can obviously change the implementation in real time.
        private WebSocket Socket { get; set; }

        //Overriden menthod Process Request async/await featur has been used.
        public override async Task ProcessRequestAsync(HttpContext httpContext)
        {
            //task is executed
            await Task.Run(() =>
            {
                //Checks if it is a Web Socket Request
                if (httpContext.IsWebSocketRequest)
                {
                    httpContext.AcceptWebSocketRequest(async delegate(AspNetWebSocketContext aspNetWebSocketContext)
                    {
                        Socket = aspNetWebSocketContext.WebSocket;

                        //Checks if the connection is not already closed
                        while (Socket != null || Socket.State != WebSocketState.Closed)
                        {
                            //Recieves the message from client
                            ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
                            WebSocketReceiveResult webSocketReceiveResult = await Socket.ReceiveAsync(buffer, CancellationToken.None);

                            //Here i have handled the case of text based communication, you can also put down your hode to handle byte arrays etc.
                            switch (webSocketReceiveResult.MessageType)
                            {
                                case WebSocketMessageType.Text:
                                    OnMessageReceived(Encoding.UTF8.GetString(buffer.Array, 0, webSocketReceiveResult.Count));
                                    break;
                            }
                        }
                    });
                }
            });
        }

        //Sends message to the client
        private async Task SendMessageAsync(string message, WebSocket socket)
        {
            await SendMessageAsync(Encoding.UTF8.GetBytes(message), socket);
        }

        //Sends the message to the client
        private async Task SendMessageAsync(byte[] message, WebSocket socket)
        {
            await socket.SendAsync(
                new ArraySegment<byte>(message),
                WebSocketMessageType.Text,
                true,
                CancellationToken.None);
        }

        //This message is fired and parent can forget about this, what this method do is gets the message and push it to the different clients which are connected
        protected void OnMessageReceived(string message)
        {
            Task task;

            if (message.IndexOf("JOINEDSAMPLECHAT") == 0)
            {
                WebSocketDictionary.Sockets[message.Replace("JOINEDSAMPLECHAT:", string.Empty)] = Socket;
                foreach (string key in WebSocketDictionary.Sockets.Keys)
                {
                    task = SendMessageAsync(string.Concat(message.Replace("JOINEDSAMPLECHAT:", string.Empty), " Joined Chat."), WebSocketDictionary.Sockets[key]);
                }
            }
            else
            {
                if (message.IndexOf("BROADCAST") == 0)
                {
                    foreach (string key in WebSocketDictionary.Sockets.Keys)
                    {
                        task = SendMessageAsync(message.Replace("BROADCAST:", string.Empty), WebSocketDictionary.Sockets[key]);
                    }
                }
            }
        }
    }
}

Creating HTML Page in a WebSite

Note: You can find detailed comments inline.

Now Add a New Web Site and host it on to IIS 8. The port number on IIS which I have kept for my application is 801. Now add a New page HTML Page in this Web site. Code Given Below:

HTML Page:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Web Socket Sample</title>
    <script type="text/javascript">
    
    var webSocket;
    var username;

    //Check is made if the WebSocket are supported on the browser or not and if they are supported then connection is established. As this is the sample i have created the
    //connection here on page load but you can also create the connection on click of the join button.
   function WebSocketTest()
   {
        if ("WebSocket" in window)
        {
            webSocket = new WebSocket("ws://localhost:801/default.Socket");
            webSocket.onopen = function()
            {
                //Connection Opened, if you want to do something while opening connection do it here
            };
        }
        else
        {
            alert("WebSocket NOT supported by your Browser!");
        }
   }

   WebSocketTest();
  
   //When user joins in by clicking in Join button, Message is sent to the server that the user joined in which is broadcasted for every user
   function JoinUser()
   {
        username = document.getElementById('txtUserName').value;
        var joinButton = document.getElementById('btnJoin');
        webSocket.send("JOINEDSAMPLECHAT:" + username);
        username.disabled = true;
        joinButton.disabled = true;
   }
  
   //When the user writes it any message it is broadcasted to every user.
   function SendMessage()
   {
        var message = document.getElementById('txtMessage').value;
        webSocket.send("BROADCAST:" + username + ": " + message);
   }

    //Fired when message is recieved from the server and displays it in the user window.
    webSocket.onmessage = function (evt)
    {
        var messages = document.getElementById('divMessages');
        var received_msg = evt.data;
        messages.innerHTML = messages.innerHTML + received_msg + '</br>';
    };

    //fired when the connection gets closed
    webSocket.onclose = function()
    {
        alert("Connection is closed");
    };

    //Fired when there comes some error in the web socket connection
    webSocket.onerror = funtion (error)
    {
        alert(error.data);
    };

    </script>

</head>
<body>
    Username:
    <input type="text" id="txtUserName" />&nbsp;<input type="button" id="btnJoin" value="Join" onclick="JoinUser();" /><br />
    Message:
    <input type="text" id="txtMessage" />&nbsp;<input type="button" id="btnBroadcaseMessage" value="Broadcast" onclick="SendMessage();" /><br />
    <div id="divMessages">
    </div>
</body>
</html>

Now we need to configure Handler for this WebSite so just open the “Web.Config” file and paste the following code into it for configuring the Handler.

Web.Config:


<configuration>
    <system.web>
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <system.webServer>
    <handlers>
      <add name="WebSocketHandler" path="*.Socket" verb="*" type="HandlerProject.WebSocketHandler" preCondition="integratedMode"/>
    </handlers>
  </system.webServer>
</configuration>

And with this we are done, we are ready to use this chat application. Please find below the screenshots of the same. What I have done here is I have opened two instances of Internet Explorer and Joined the chat with two different users and you can see the communication going on.

Screenshot:

Also I have attached a sample application code, which you can use. Just download it, Host the Website on IIS and configure the handler, the only thing after that you need to do is if your hosting this sample application or website on some other port(Other than 801), just make changes in HTML Page(change the port number where we are creating Web socket Connection).
Hope it helps in understanding Web Sockets.

Tuesday, 3 September 2013

Integration Testing With Fitnesse

Fitnesse is an application testing tool which allows you to test your business logic. As the name suggests FITnesse, it’s a Functional Integration Testing (FIT) tool. Although some people use it as Unit Testing tool but it is totally an integration testing tool which helps developers and testers to do the functional testing of their code when all the layers get integrated with each other, One can create test cases for UAT (User Acceptance Testing) or FAT (Functional Acceptance Testing) and get then executed on the integrated environment.

This tool is very helpful one or more number of teams are working on different layers, each doing the Unit Testing of their code, so you always need to test how the application is performing in the integrated environment, so this is tool which is best fitted to execute all the acceptance test cases and check if the application if performing well in integrated environment.

You must be wondering as of now, can we do UI Automation testing, the answer is no it is not a UI Automation tool but yes it can be integrated with various another tools like Selenium for the UI Automation testing.

This tool standalone only helps in testing the layer just below your UI either it is Façade or a Business Logic Layer.

As I mentioned above that this tool can also be used by Quality Testers because one don’t need to know any specific technology or language to create down the test cases and execute them, it works on a very simple scripting which can also be done by testers by just spending few hours learning the scripting concepts even without any knowledge of programming languages.
The best part I like is that without putting much effort, the technical leads can check if the server validations are all into place or not. In today’s world we often say that validation should be both Client Side and Server Side (I will not say all the validations but yes many of them are required on both sides), With UI Automation tools you don’t often end with checking your Server Side validations but as in this tool you are directly interacting with your Business Logic so one can easily check these on Server Side and can get the leakage fixed.

We will see how easy it is to test a sample class created by developers, by just using the Dashboard of Fitnesse.

So just before starting, we need Fitnesse on our machines if you don’t have, please follow the link and download the same.


Download “fitnesse-standalone.jar”.

Scope of my article is to help you work with Fitnesse for .Net.

You also need to download the FitSharp which actually helps Fitnesse to run the methods in the library, so it basically acts as an engine which allows Fitnesse to execute the test cases or you can say the code and generate a required output page for the results.

There are two types of testing engines supported by Fitnesse.

  1. FIT – FIT is the test engine which helps in invoking test cases, interprets Pages (Wiki Pages) and generates results. More than a testing engine it is a testing framework in itself on which the Fitnesse is built upon. The only problem which was faced by the Developers or the code writer is that to make test fixtures they need to inherit their class with ColumnFixture class which in itself is a very heavy construct.
  2. SLIM – Because of the limitation of inheritance in FIT engine, comes the SLIM engine. SLIM is Simple List Invocation Method which runs on a separate server which is ultimately invoked by the Fitnesse Server. The only responsibility of SLIM is to invoke the test cases or the methods. Now the developer can simply write their libraries as they want to without worrying about the inheritance model. Additionally it is very light-weight.
FitSharp can be downloaded from github, follow the URL:


While making the download, please make sure that for which .Net Framework version you want it to run with. Don’t feel panic if you have different components of the application which are on different .Net Framework version because you can make multiple hosting’s in that case each testing a particular component using the FitSharp DLL(s) of that version.

Now after downloading just unzip the FitSharp package. Now copy down below mentioned DLL(s) and EXE(s) at the same path where your JAR is present. Create a Folder named FitSharp (I have created this folder so as to keep the directory structure neat and clean) at the same location where your JAR is and copy down the following DLL(s) and EXE(s) there.

  1. Runner.exe
  2. RunnerW.exe
  3. fitSharp.dll
  4. fit.dll
Now as soon as you do this you are done with all the components you required and we can go ahead with the installation.

Installing Fitnesse

Before installing Fitnesse with the JAR file please make sure that the Java is installed on your system.
Now for installing the Fitnesse, open the command Prompt with Administrator Permission. Run the following command:

Java –jar “<Path of your Fitnesse Jar file>” –p 8025

Please see screenshot for reference:


Details of the command:

So what we are doing here is installing the JAR file with the help of Java installed on our system. Path of the Jar file is the physical location where you have copied the JAR. In my case it is present in “C” drive.

8025 is the port is mentioned, by default the installation is done on Port 80, so if you are having another Web site or Web Application running on Port 80 or you want to run Fitnesse on some other port of your choice just mention the Port as –p <PortNumber>.

Now the Fitnesse is ready to be used, to check if it is running or not, enter following URL:


In my example it is:


And you can see it is running:

We are done with the installation of Fitnesse, now we have to configure down Fitnesse to use FitSharp testing engines, which can be very easily done from the dashboard only. Here below I will show you how you can configure booth FIT and SLIM engine and how will we create test classes or fixture for the same. And then after that we will see that how to create different test pages and write down scripts to execute test cases by writing scripts or say creating Decision Tables (term used in Fitnesse).

First we will look for FIT:

FIT Test Engine

For configuring FIT as test engine for Fitnesse, open the Front Page of the Fitnesse as shown in the screenshot above. At the bottom of this page you will see an option “root”. Click on this link and it will open a page for you as shown below:



Click on the Edit Button and write down the following commands to configure FIT Engine.

!define COMMAND_PATTERN {%m -r fitnesse.fitserver.FitServer,FitSharp\fit.dll %p}
!define TEST_RUNNER {Fitsharp\Runner.exe}

All the paths mentioned above for the DLL(s) and EXE(s) are relative to JAR file of the Fitnesse. Here we are using fit.dll which says that by default FIT Engine will be used and in the above case we haven’t specified any particular test engine which by default tells Fitnesse to use FIT engine.

See the screenshot for settings:


Now we are done with the Fitnesse test engine configuration with FIT, let’s now look into how to create a Test Class for it.

For this open Visual Studio, create a class library project (Framework used should be same as for which you have downloaded FitSharp package). Add a reference of “fit.dll” in your project and add a class in it, refer following code for example.

using fit;

namespace SampleLibrary
{
    public class UserTest : ColumnFixture
    {
        public string UserName { get; set; }
        public string Password { get; set; }

        public bool IsAuthenticated()
        {
            if (UserName == "Abhishek" && Password == "test")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

}

So here you can see that the Test class is being inherited by ColumnFixture.

Now SLIM

For configuring SLIM as test engine for Fitnesse, open the Front Page of the Fitnesse as shown in the screenshot above. At the bottom of this page you will see an option “root”. Click on this link and it will open a page for you as shown below:


Click on the Edit Button and write down the following commands to configure FIT Engine.

!define TEST_SYSTEM {slim}
!define COMMAND_PATTERN {%m -r fitSharp.Slim.Service.Runner,FitSharp\fitsharp.dll %p}
!define TEST_RUNNER {FitSharp\Runner.exe}

All the paths mentioned above for the DLL(s) and EXE(s) are relative to JAR file of the Fitnesse. Here we are using fitSharp.dll and SLIM is defined as Test Engine.

See the screenshot for settings:


Now we are done with the Fitnesse test engine configuration with SLIM, let’s now look into how to create a Test Class for it.

For this open Visual Studio, create a class library project (Framework used should be same as for which you have downloaded FitSharp package). Add a class in it, refer following code for example.

namespace SampleLibrary
{
    public class UserTest
    {
        public string UserName { get; set; }
        public string Password { get; set; }

        public bool IsAuthenticated()
        {
            if (UserName == "Abhishek" && Password == "test")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

}

With this we are done with the configuration part, now let’s see how to create Test Pages in Fitnesse, for this go to the Front Page and Edit that Page. Now add the name of the Page which you want to create in the end or in on the top or may be anywhere you want the reference of your test page as shown in the screenshot.


In the above screenshot you can see that I have created a new page named “UserTest”. When you will click on save you will see that there is with your page name, and your page is not clickable, this shows that the page is new and now you can define your test on this page. In the example below I will show you that how I will test the code in UserTest class of mine but before that we should understand what the relevance of adding a page is.

In Fitnesse Page can be of few types i.e. Static, Suite or a Test. One can do the settings by opening the page changing the properties from Tools -> Properties

  1. Test – This is the simple test page on which you will write down your script or will create Decision tables to carry out test runs.
  2. Static – Static Page are the pages on which we can’t execute any test runs. So what’s the use of this page, this page is useful suppose in a scenario that the new library is being developed by the time it gets developed Testing team can write down the scripts to test the library but they don’t want the test run to be executed after they have created that page(Yes, test run is done by clicking “Test” on top of that page so you must be wondering why not a create a Test Page and don’t click on this button, but suppose think of a scenario in which there is an automated system which runs the test cases so that system will not now that if the test run needs to be executed on this page or not, and so we can make it as Static, Fitnesse will decide that the test run need not to be executed for this page).
  3. Suite – Suite is nothing but the group of Test pages, it basically is the way of keeping all the related Test Pages in one set and when we want a particular set of test cases to be executed we just run the test on Suite instead of running test cases on each test page separately.
Now coming back to creating a new page, now click on the”?” sign shown with your page name on Front Page of Fitnesse. As soon as you will click on “?”, your page will get opened now it’s time to write down some scripts on this page to run the test cases. Now we will see how to import your library which needs to be tested and how to create the decision table for executing the test cases. Although there are many scenarios which can be there with your test cases or your test cases need to cover, but it will not be possible to explain all of them. I will just be presenting your with an simple example and for other scenarios of yours you can click on the link “User Guide” on the Front Page.

As soon as you will open yours newly created page it will present you with a Rich Text Editor, to do the configuration of the test libraries and create Decision Tables. For doing the configuration on this Page and tell it the path of test libraries (Libraries which needs to be tested, in my case it is Sample Library). So to make it easy and keep it separate from the development process, what we will do it create a Folder named “Projects” (This name can be of your choice) at the same path where the Fitnesse JAR file is present, Now Build your project and copy the DLL(s) of your project into this folder. This is done to keep the things separate from the development process and also we can give the relative path of this Projects folder in the page configuration (Relative paths are relative to the location of the JAR file.).

Add following on your page to configure the path of the library and importing down a particular namespace in that library for which we will create the Decision tables. This is also an Decision table which we have created for the import, just to very safe and tell Fitnesse that the new Decision Table is getting started we add sign of “!” in the front of the first row of the table.

!path Projects\SampleLibrary.dll

!|import             |
|SampleLibrary  |

So here Path is the relative path of the DLL and the table row below the import row of the table tells the name of the namespace in that library. Now it’s time to create a decision table for Running down your methods and see the expected results. See the code below for that:

!|UserTest                                                       |
|UserName|Password|IsAuthenticated?            |
|"Abhishek"|"test"|true                                      |
|"Abhishek"|"Jain"| true                                    |

So what is done in above decision table is First Row tells the name of the class, in Second row we specify the sequence of the parameters or Properties of that class in library, order in which we are going to provide the inputs, As everything is Pipe (“|”) separated in Fitnesse, so the first parameter is Username and second is the Password and Last one is the Method which needs to be executed.


In third row we have created the first set according to our scenarios, in which we are defining the Properties values and the final expected result. And yes we are done with our First Test case, now it’s time to execute it by saving this on the Test page.

Now just click on the “Test” button to run the test cases and you can see the results. Just see the sample Result Page:


Now here you can see the result both of my test cases passed. With this I think I am done, please let me know in case something is missed or you need some more help on Fitnesse.
Hope it helps, please provide the valuable feedback.