- WHITE Framework
- Visual Studio IDE
Test case: Verify that user can add two numbers by using calculator.
- Open the Calculator application
- Click on 2 (Two) button
- Click (+) button
- Click on 3 (three) button
- Click (=) equals button
- Verify the result
- Open Visual Studio 2013
- Click on File>New>Project
- New project window is displayed.
- Select .Net Framework 4
- Select Unit Test Project as Project type
- Enter project name as “CalculatorTest”
- Browse the project directory
- Click Ok button and new project Calculator test is created.
Step 3 : Download WHITE and add to project as reference
- Go to Solution Explorer
- Right click on the option Add reference
- Click on Manage NuGet Packages…
- NuGet package manager window is displayed.
- Enter “White” as search text and press enter button from keyboard.
- TestStack.White package is displayed and click on Install button.
- Installation starts.
- After finishing installation click close button to close the package manager window.
- To verify that proper reference added we need to expand the References and see appropriate dll installed.
Step 4 : Writing code for Setting up Application
private TestStack.White.Application application;
[TestInitialize] public void TestSetUp() { string ApplicationPath= @"C:\Windows\system32\calc.exe"; application = TestStack.White.Application.Launch(ApplicationPath); }
using TestStack;
- Open the Calculator application.
This is written at public void TestSetUp() method.// Define application Main window Window mainWindow = application.GetWindow("Calculator");We need define the main window. We have Window class under TestStack.White.UIItems.WindowItems namespace. mainWindow object is used in detecting other UI object of Calculator application window. Our application.GetWindow() method returns the object of Calculator window. It takes window title “Calculator” as argument.
Note that we need to add the following namespace.using TestStack.White.UIItems.WindowItems;
- Click on 2 (Two) button
//Click on 2 (Two) button TestStack.White.UIItems.Button btnTwo=mainWindow.Get(SearchCriteria.ByText("2")); btnTwo.Click();
Here a button object object is defined under TestStack.White.UIItems.Button class. Note that we also add search criteria for Two (2) button in calculator. Common Syntax to define an object and adding search criteria is
UIClassName objectName=windowObject.Get<UIClassName>(SearchCriteria.ByParameter(value));
Here,
UIClassName : There are separate classes for every UI object in WHITE. We have to find out which UI object and implement it among list of classes from WHITE.
objectName : Name of UI Object.
windowObject : It is the object of main window where the UI object possess.
SearchCriteria : This class contains search properties of the UIClassName.
ByParameter : It is the different types of property name of that UI class.
value : Property value of the UI Object.
Not that here we use ByText(“2”)) search property that contains the value 2. This is the same as “Name” properties od an UI object.
Also note that we need to add following namespace for Search Criteria.
using TestStack.White.UIItems.Finders;
We can determine the properties and values by using VisualUIAVerify tool. You can download the tool from this site and get the documentation from this site. After downloading the VisualUIAVerify solution and you have to open it using Visual Studio and Build it. Then run exe file from bin folder.
To detect the search properties of an UI object using VisualUIAVerify tool, you need to run the application and then move mouse pointer to UI object and pressing CTRL key from Keyboard. It draws a blue color rectangle and show the property list and values under the application.
I will discuss in details on how to detect search properties of an UI object using VisualUIAVerify tool on my future posts.
- Click (+) button
//Click (+) button TestStack.White.UIItems.Button btnPlus = mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("Add")); btnPlus.Click();
- Click on 3 (three) button
//Click on 3 (three) button TestStack.White.UIItems.Button btnThree=mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("3")); btnThree.Click();
- Click (=) equals button
//Click (=) equals button TestStack.White.UIItems.Button btnEquals = mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("Equals")); btnEquals.Click();
- Verify the result
//Verify the result 5 TestStack.White.UIItems.Label txtResult = mainWindow.Get<TestStack.White.UIItems.Label>(SearchCriteria.ByAutomationId("150")); Assert.AreEqual("5", txtResult.Text);
Note that we have used ByAutomationId property which is common and unique to all controls. It is recommended to use this property as search property on most the cases if it is present and defined for that control.
Also we use AreEqual() method to verify expected result with the actual result. This method is under Assert class of VS Unit Test Framework.
[TestCleanup] public void TearDown() { application.Close(); }
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using TestStack; using TestStack.White.UIItems.WindowItems; using TestStack.White.UIItems.Finders; namespace CalculatorTest { [TestClass] public class UnitTest1 { private TestStack.White.Application application; [TestInitialize] public void TestSetUp() { string ApplicationPath= @"C:\Windows\system32\calc.exe"; application = TestStack.White.Application.Launch(ApplicationPath); } [TestMethod] public void TestMethod1() { // Define application Main window Window mainWindow = application.GetWindow("Calculator"); //Click on 2 (Two) button TestStack.White.UIItems.Button btnTwo=mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("2")); btnTwo.Click(); //Click (+) button TestStack.White.UIItems.Button btnPlus = mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("Add")); btnPlus.Click(); //Click on 3 (three) button TestStack.White.UIItems.Button btnThree=mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("3")); btnThree.Click(); //Click (=) equals button TestStack.White.UIItems.Button btnEquals = mainWindow.Get<TestStack.White.UIItems.Button>(SearchCriteria.ByText("Equals")); btnEquals.Click(); //Verify the result 5 TestStack.White.UIItems.Label txtResult = mainWindow.Get<TestStack.White.UIItems.Label>(SearchCriteria.ByAutomationId("150")); Assert.AreEqual("5", txtResult.Text); } [TestCleanup] public void TearDown() { application.Close(); } } }
- Go to Build menu and click on Build > Build Solution
- Go to Test menu and click on Test>Windows>Test Explorer
- Right click on Method name and click on Run Selected Test.
- Test will be run and result is displayed.
That’s all for now. I will discuss more on WHITE framework on my future post. Please follow my post.
Happy Windows UI Automation with WHITE !!!
Guys, Everybody ctrlC,V to same calculator – It would be great article if it explains how all UI elements, or modal dialogboxes handling.. beyond basics.. appreciate your efforts…
Can you please let me know, how to fetch ‘Value’ field using White
Hola
What is the Coded UI difference with white?
And what is the best option to learn an automated and desktop ERP?
Gracias
Perú