In this topic, we will discuss on using White Framework in Visual Studio for automating a wpf application
To give an intro about white framework, this framework is .net based to automate win32/winform/wpf apps. Typically an object oriented API.
- https://github.com/TestStack/White. Solution name is WpfTestApplication
- download Snoop 2.8.0 from https://snoopwpf.codeplex.com/ this is used for spying objects in UI
- Visual Studio
- Nuget Package – TestStack.White
Open the solution in visual studio and compile it to get the WpfTestApplication.exe. The app looks like the below
When you access our WpfTestApplication.exe, you need to refresh it by Clicking the button near the dropdown, and you see the mainwindow-wpftestapplication.
Steps for creating the automation project in VS
Create a new project by navigating to File -> New -> Project. Select Tests – Unit Test Project.
Add TestStack.white as reference by adding through manage nuget package.
Once you have installed the nuget package, you can see the below highlighted reference in the project.
How to find the property of an element.
We have the WPF application in the left side and Snoop Tool on the right side. When you select a checkbox in the application, you can view the properties/events/data context/methods associated with the control in the snoop tool as described in the screenshot below.
Sample Code looks like the below.
Once the testmethods are written, You can run the test in the test explorer. Right click on the method and click on ‘Run Selected Tests’.
As like any of the UI automation frameworks, white framework allows us to select an element using few properties as below.
Few Listed Properties.
Launch the application.
Getting the context of the mainwindow.
Window mainWindow = application.GetWindow(“MainWindow”);
Clicking on a Button
TestStack.White.UIItems.Button btnbuttonintoolbar = mainWindow.Get
(SearchCriteria.ByText("Button in toolbar")); btnbuttonintoolbar.Click();
Clicking on a Tab
TestStack.White.UIItems.TabItems.TabPage tabInput = mainWindow.Get
(SearchCriteria.ByText("Input Controls")); tabInput.Click();
Get the list of items in a listbox and selecting the first item.
ListBox lnumber = mainWindow.Get
Get the list of items in a ComboBox and selecting the first item.
ComboBox cSelect = mainWindow.Get
Clicking on a CheckBox
TestStack.White.UIItems.CheckBox chkSecond = mainWindow.Get
(SearchCriteria.ByText("A checkbox")); chkSecond.Click();
Inputting a text in a textbox.
TestStack.White.UIItems.TextBox txtBox = mainWindow.Get
(SearchCriteria.ByAutomationId("TextBox")); txtBox.Text="Hi This is a Text Box";
Clicking on a radio button
TestStack.White.UIItems.RadioButton rdoBtn1 = mainWindow.Get
Clicking on a hyper link
TestStack.White.UIItems.Hyperlink hlink = mainWindow.Get
(SearchCriteria.ByText("Link Text")); hlink.Click();
Navigating through a slider
TestStack.White.UIItems.Slider sldrOne = mainWindow.Get
(SearchCriteria.ByAutomationId("Slider")); sldrOne.LargeIncrementButton.Click(); sldrOne.LargeDecrementButton.Click();
Expand a tree node
TestStack.White.UIItems.TreeItems.TreeNode nodeOne = mainWindow.Get
(SearchCriteria.ByText("Root")); nodeOne.Select(); nodeOne.Expand();
This post first appeared on MSDN Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The MSDN Blogs., please read the originial post: here