Automation of Windows Native Applications

Automation of Windows Native Applications:

In current world of software quality assurance industry it is need of testing to be accurate and speedy. Automation helps us here.
When there is requirement where software must be user friendly and easy usable UI automation plays verify important role. Whether it is a web application or a windows form application UI automation makes QA's job more easier and productive. In this blog I am focusing on how UI automation implemented for Windows native applications/ Form applications.

To automate UI of any application I will recommend to remember following mnemonic:
WFVA

- Here W stands for Wait, When any application runs you wait for the element to get visible/ enable (Of course for that you need to find the element and keep on validating it)
For this you can have your own logic, following is sample logic for the same:
int tryCount = 0;
AutomationElement element;
do
{
try
{
// find element
element = <find_element_logic>
// wait
thread.sleep(500);
}
catch {}
}
while((element == null && !element.IsEnabled) && tryCount < 20);

- F stands for Finding the element, once the element appear on screen get the element object.
AutomationElement rootElement = AutomationElement.RootElement;
element = rootElement.FindFirst(TreeScope.Child, new Propertycontions(ControlType.Edit, "Email:"))

- V stands for validate element, after finding the element validate the element before performing any action on it. This is very important step as we are minimizing the various kinds of exceptions that can be thrown on performing an action before validating. E.g. such as NullReferanceException on performing action on null object. InvalidOperationException on performing action on element with invalid state etc.
// Verify that element should not be null and disabled/ off screen before performing any action.
If (element != null && element.IsEnabled && !element.IsOffScreen)
{
// Perform action
}

- A stands for action, after validating an element you are good to go with performing any action on the element.
// Get control pattern for the found element ‘element’
ValuePattern vp = (ValuePattern)element.GetCurentPattern(ValuePattern.Pattern);

// Perform action using the methods available for particular pattern
vp.SetValue(“mayuresh49@gmail.com”);

Following tools are required for this automation:

1. UISpy/ Inspector/ Spy ++ (Comes as VS plug in) :

For finding Windows native application elements any of the above mentioned tool can be used. Element can be identified based on following properties:

Element Identification:

- AutomationId
- ControlType
- Name
- ClassName
- LocalizedControlType
etc.

It is also important to find element with correct identifying property which reduces your LOC and time to identify elements.

On finding the element we can find other properties of element by using following AutomationElement properties:

Element Visibility and State :

- BoundingRectangle
- ClickablePoint
- IsOffScreen
- ClassName
- IsEnabled
- HasKeyboardFocus
etc.

Element Control Patterns and actions:

- ValuePattern: for the text box, text area controls etc.
- InvokePattern: for button, checkbox etc.
- SelectionItemPattern: Radio button group etc.
- ExpandCollapsePattern
etc.

Element Actions:

- SetValue: for the text box, text area controls etc.
- Invoke:  for button, checkbox etc.
- Select: Radio button etc.
- Expand
- Collapse
etc.

2. Nunit test framework 

Source: Can be added as a plugin from VS extension manager or download the Nunit.zip and add nunit references from lib directory.

For every test case to execute, we need to setup prerequisites for that test case to run. After running the test case you set the environment to default settings OR you clean/ revert the changes that were made by the test.
In any automation test suite it is best practice to have each test case a unit and one test case should not be dependent on another (It may change on requirement how you want to execute your test suite - If setup need to be done at suite level/ test cases made dependent to execute them sequentially in order to execute scenarios in less time).

Nunit provides us to setup the test prerequisites, cleanup the environment changes etc. by providing the set of attributes such as [TestFixtureSetup], [Setup], [TestFixture], [Test], [TestFixtureTearDown], [TearDown] etc.

It runs in following sequence:
[TestFixtureSetup] > [TestFixture] > [Setup] > [Test] > [TearDown]  > [TestFixtureTearDown]

Nunit also provides the assertions to assert the result you found for a test step while executing a test. Assertions are nothing but the verification. Following are some assert methods:

- Assert.IsTrue(condition, message);  
  If given condition is true it will pass else it will throw assertion exception with provided message.

- Assert.IsFalse(condition, message);

- Assert.AreEqual(obj1, obj2, message);
If given objects obj1, obj2 are equal it will pass else it will throw assertion exception with provided message. etc

3. Windows Automation library

Source: UIAutomationClient.dll, UIAutomationTypes.dll, UIAutomationCore.dll, UIAutomationProvider.dll etc.

Steps to automate the application:

- Find root element
AutomationElement element = AutomationElement.RootElement;

- Find the sub/ child/ descendant element using root element:
AutomationElement element = element.FindFirst(TreeScope.Descendent, new PropertyCondition(ControlType.Edit, “Email:”));

- Identify the pattern of element from UISpy and get the pattern of current element in appropriate pattern
// Get control pattern for the found element ‘element’
ValuePattern vp = (ValuePattern)element.GetCurentPattern(ValuePattern.Pattern);

- Perform the action using the pattern 
// Perform action using the methods available for particular pattern
vp.SetValue(“mayuresh49@gmail.com”);


Comments