White framework for automating WPF Application using Visual Studio

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.

 

Tools/App used

  1. https://github.com/TestStack/White. Solution name is WpfTestApplication
  2. download Snoop 2.8.0 from https://snoopwpf.codeplex.com/ this is used for spying objects in UI
  3. Visual Studio
  4. Nuget Package - TestStack.White

Open the solution in visual studio and compile it to get the WpfTestApplication.exe. The app looks like the below

sample1

Snoop Tool

When you access our WpfTestApplication.exe, you need to refresh it by clicking the button near the dropdown, and you see the mainwindow-wpftestapplication.

snoop

Steps for creating the automation project in VS

Create a new project by navigating to File -> New -> Project. Select Tests - Unit Test Project.

create-project

Add TestStack.white as reference by adding through manage nuget package.

sample2

Once you have installed the nuget package, you can see the below highlighted reference in the project.

reference

 

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.

snoop1

Sample Code looks like the below. samplecode

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'.

testexplorer

As like any of the UI automation frameworks, white framework allows us to select an element using few properties as below.

properties

Few Listed Properties.

Launch the application.

Use White.Application.Launch(app_path)

Getting the context of the mainwindow.

Window mainWindow = application.GetWindow("MainWindow");

Clicking on a Button 

 TestStack.White.UIItems.Button btnbuttonintoolbar = mainWindow.Get<White.UIItems.Button>(SearchCriteria.ByText("Button in toolbar"));
btnbuttonintoolbar.Click();

Clicking on a Tab

 TestStack.White.UIItems.TabItems.TabPage tabInput = mainWindow.Get<White.UIItems.TabItems.TabPage>(SearchCriteria.ByText("Input Controls"));
tabInput.Click();

Get the list of items in a listbox and selecting the first item.

 ListBox lnumber = mainWindow.Get<ListBox>("ListBoxWithVScrollBar");
lnumber.Select(0);

Get the list of items in a ComboBox and selecting the first item.

 ComboBox cSelect = mainWindow.Get<ComboBox>("AComboBox");
cSelect.Select(0);

Clicking on a CheckBox

 TestStack.White.UIItems.CheckBox chkSecond = mainWindow.Get<White.UIItems.CheckBox>(SearchCriteria.ByText("A checkbox"));
chkSecond.Click();

Inputting a text in a textbox.

 TestStack.White.UIItems.TextBox txtBox = mainWindow.Get<White.UIItems.TextBox>(SearchCriteria.ByAutomationId("TextBox"));
txtBox.Text="Hi This is a Text Box";

Clicking on a radio button

 TestStack.White.UIItems.RadioButton rdoBtn1 = mainWindow.Get<White.UIItems.RadioButton>(SearchCriteria.ByAutomationId("RadioButton1"));
rdoBtn1.Click();

Clicking on a hyper link

 TestStack.White.UIItems.Hyperlink hlink = mainWindow.Get<White.UIItems.Hyperlink>(SearchCriteria.ByText("Link Text"));
hlink.Click();

Navigating through a slider

 TestStack.White.UIItems.Slider sldrOne = mainWindow.Get<White.UIItems.Slider>(SearchCriteria.ByAutomationId("Slider"));
sldrOne.LargeIncrementButton.Click();
sldrOne.LargeDecrementButton.Click();

Expand a tree node

 TestStack.White.UIItems.TreeItems.TreeNode nodeOne = mainWindow.Get<White.UIItems.TreeItems.TreeNode>(SearchCriteria.ByText("Root"));
nodeOne.Select();
nodeOne.Expand();
 

Comments

  • Anonymous
    June 03, 2017
    Hello! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the fantastic work!
    • Anonymous
      June 12, 2017
      thanks buddy !
  • Anonymous
    February 18, 2018
    I am just starting to learn about all of this. Thanks!
    • Anonymous
      February 27, 2018
      thank you !
  • Anonymous
    February 19, 2018
    Hi, Is there any specific framework to login to remote server and run exe file and select some options in windows forms and click on run button. I love this blog. ThanksMouli