Wednesday, April 1, 2015

Using TestStack.White: Automating applications using WHITE framework

I came across this tool suggested by one of the developers in my team and later agreed to do a PoC for my application using it. Having experienced the comfortableness from UFT, this tool was quite challenging to learn objects and code around it. But it's really an interesting tool that you can scale your automation pack to any level. Of course with the help of Visual studio. Opensource is 1 factor that interested me and my team. While looking for tutorials and some code samples i came across samples which was written in traditional WHITE. I mean traditional because of the fact that Test Stack has took over WHITE to develop it further. So in order to interest people who are interested in WHITE, here is some quick guide to work with basic calculator application. Here i'm gonna show you how to configure White and work with it. As usual i will take the calculator application for my setup and demo. I have installed the express edition of the visual studio 2012. 

Step 1:

Download and install "Microsoft Visual Studio Express 2012 for Windows Desktop" from the below link. Below is the link at the time of writing this blog.

http://www.microsoft.com/en-gb/download/details.aspx?id=34673


After installation you will see following item listed in your desktop.








Step 2:
Open Visual Studio by clicking the above icon.


Step 3:
Create a New project







































Step 4:
Open Tools - > Library package manager - > Manage NuGet packages for solution.
























Step 5:
Search for White in the top left searchbox and you will be displayed with the results of  TestStack.White files.




























Click on Install button to install the White framework to your project.
Click Ok on the projects popup. Then you will shown with the progress of white installation.
Then you will find the White is installed by green mark shown & a readme file shown at background which specifies successful installation of TestStack.White to your project.

Remember this is not installed in your machine but in your project. This technically means adding the required dlls of TestStack.White to your project to enable automation.
























Now your project is ready for automation testing. But in order to run the scripts you can either select Test project in Step 3 or install NUNIT package in your project to run tests. You can install NUNIT as well for the project to enable NUNIT functionalities. There are arguments on choosing MSTest or NUNIT which i will leave it at your discretion.

Step 6:

Install Nunit the same way as TestStack.White. For MSTEST you can skip this step.


























Step 7:
Now double check in your solution explorer whether TestStack.White is installed. I'm using MSTEST for my project.






Until Now you have successfully installed Teststack.White for your project. But in order to write tests you will need to know the properties of the object you work with. There are many tools available to read the object properties. But here i'm going to use UIAutomationVerify for learning objects.

Step 8:
Download the SDK for windows where you will find the UIA verify from the below link based on your windows version.
https://msdn.microsoft.com/en-us/windows/desktop/ff851942.aspx

But i have downloaded an older version of it to avoid hassles.

Step 9:
Now open Calculator application and learn the objects properties like below. At the left pane you will see list of active windows opened and the objects within it. At the right pane you will properties of each object. Each object will at the left pane will have <object type> <Name> <AutomationId>. It is your decision to use AutomationId or any other properties to identify an object. Here i will use the AutomationId property for identification.


Step 10:
Now you are ready to write tests. I will perform a simple add operation and verify the result.

using System;
// import the below for using MSTest framework keywords like [TestClass]
using Microsoft.VisualStudio.TestTools.UnitTesting;
//import the below for using white functions
using TestStack.White;
//import the below for using ProcessStartInfo to launch the process
using System.Diagnostics;
//import the below for using Window keyword
using TestStack.White.UIItems.WindowItems;
//import the below for using UIItems like Button, panel etc
using TestStack.White.UIItems;
//import the below for using SearchCriteria keywords
using TestStack.White.UIItems.Finders;

namespace WhiteDemo
{
    [TestClass]
    public class UnitTest2
    {
        public Application app;
        [TestMethod]
        public void InvokeApplication()
        {
            //Launching the application
            string apppath = @"C:\Windows\system32\Calc.exe";
            var prc = new ProcessStartInfo(apppath);
            app = Application.Launch(prc);

            //Getting the window object 
            Window WndCalc = (Window)app.GetWindow("Calculator");

            //Getting the controls
            //By Automation id
            Button btnFour = WndCalc.Get<Button>(SearchCriteria.ByAutomationId("134"));
            //By Text
            Button btnFive = WndCalc.Get<Button>(SearchCriteria.ByText("5"));
            Button btnMultiply = WndCalc.Get<Button>(SearchCriteria.ByAutomationId("92"));
            //By automation id and index
            Button btnEquals = WndCalc.Get<Button>(SearchCriteria.ByAutomationId("121").AndIndex(0));

            //Performing calculation
            btnFour.Click();
            btnMultiply.Click();
            btnFive.Click();
            btnEquals.Click();
            Label txtResult = WndCalc.Get<Label>(SearchCriteria.ByAutomationId("158"));
            //Assert will check if the expected equals Actual
            Assert.AreEqual("20", txtResult.Name);

        }

    }
}

Step 11:
After you have written tests, you will see your method listed under Test Explorer tab. Right click on it and select Run Selected Tests to run your test or right click on the function directly in the code editor window.






3 comments:

Unknown said...

Hello Arun,
The Article on Automating applications using WHITE framework is nice.It give detail information about Automation Applications. Thanks for Sharing the information about it. Software Testing Company

dhinesh said...

This information is really fabulous thanks for sharing valuable information.
Full Stack Training in Chennai | Certification | Online Training Course| Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai | Full Stack Training | Certification | Full Stack Online Training Course

Dorothy said...

Nice Post.. really helpful to clear my small concepts. thanks :)Keep doing more, waiting to read your next blog Performance testing services. Visit here for more information about Vulnerability assessment services and penetration testing services.