Saturday, 7 November 2015

Winforms UI Automation using C#

In this post we are going to see how to automate the Windows and there operations with out human interaction,Generally this will use in the UI Automation, with out human intervention we are going to do some process in Windows.In this sample i am using the sample exe which is created in winforms where this exe can give result of Add, Sub, Mul, Div Calculation based on the value given in the TextBox, Now we are going to automate this form using the White Framework.


First Download the White Framework, then starts coding ....

I already created the Exe which need to Test, that Exe present in the Following location C:\Users\Goku\Documents\visual studio 2015\Projects\Calc\Calc\bin\Debug\Calc.exe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.ListBoxItems;
using TestStack.White.UIItems.WindowItems;
using  System.Diagnostics; 
namespace Automatiom
{
    class WinformAutomation
    {
        public static string FilePath = 
      @"C:\Users\Goku\Documents\visual studio 2015\Projects\Calc\Calc\bin\Debug\Calc.exe";
        Application _application;
        Window _window;

        public void LaunchApplication()
        {
            _application = Application.AttachOrLaunch(new ProcessStartInfo(FilePath));
            _window =  _application.GetWindow(SearchCriteria.ByText("CalcForm"), 
                              InitializeOption.NoCache);
        }

        public void DoOperation()
        {
            TextBox _box1 =  _window.Get<TextBox>(SearchCriteria.ByAutomationId("numtext1"));
            _box1.Text = "5";
            TextBox _box2 = _window.Get<TextBox>(SearchCriteria.ByAutomationId("numtext2"));
            _box2.Text = "6";
        ComboBox OperSelect = _window.Get<ComboBox(SearchCriteria.ByAutomationId("OperList"));

            OperSelect.SetValue("Mul");
        }

        internal void Click()
        {
          Button _btn =   _window.Get<Button>(SearchCriteria.ByAutomationId("DoneBtn"));
            _btn.Click();
        }
    }
}


From the code you can see first application is launched , then we are taking the instance of the particular windows in which we are going to do operations, Next create a instance of each controls through the window instance here i am using the automation Id to get the controls reference, normally control name is mention in automation id.

Finally get the instance of button and fires the click event.To find the automation id of each control we can use the UISPy tool which get the id as well as many properties of all windows present inside the Operating System.

Sample :



When we launch the application this will trigger a CalcForm.Exe to launch and the place the value in the textbox's ,select the value in combobox and finally clicks the button.

Output:
********




























No comments:

Post a Comment