Select Page
Automation Testing

Windows Application Testing using FlaUI

We can use FlaUI libraries to automate win32, WinForms, WPF and Windows Store apps.

FlaUI Examples
Listen to this blog

It is an Open-source UI based automation library to test your application which runs on Windows OS. It wraps almost all of the libraries used in the Microsoft UI Automation Test Library. We can use FlaUI libraries to automate win32, WinForms, WPF and Windows Store apps.

The below are the libraries provided by FlaUI, we will see the uses of those libraries against each one of them:

FlaUI.core – This Library will has base elements used in the concrete implementations of FlaUI.

FlaUI.UIA2 – This Library is used to automate win32 and winforms applications.

FlaUI.UIA3 – This Library is used to automate WPF and windows store apps.

Prerequisites: – You should have Visual Studio IDE installed in your machine.

Installation of FlaUI:

1. Create a New Project in Visual Studio

2. Right-click on the project and select “Manage NuGet Packages” (Using NuGet Package Manager)

3. Select the ‘Browse’ tab and Search for FlaUI, it will display the FlaUI libraries.

4. Install the below libraries from NuGet package manager

FLAui.core, Flaui.uia2, Flaui.uia3.

NOTE: The downloaded libraries are limited to the project, which means it is not available by default for all projects.

How to launch the application using FlaUI:

1) To start testing one should implement the static method “Launch” from the “Application” class which is available in FlaUI.Core library.

2) You should pass the application path (the application you installed in your machine and you want to automate) as a parameter to the Launch method as given in the below example.

Example:

var msApplication = Application.Launch(@"C:Program FilesMicrosoft OfficeOffice16WINWORD.EXE");
var automation = new UIA3Automation();
var mainWindow = msApplication.GetMainWindow(automation);
  

The above code tells you

1. How the launch method is implemented using the Application class, and how the application path is passed as a parameter (First Line).

2. In the second line, we are specifying the Automation Library Type (UIA3) used to automate the app.

3. The third line describes getting the main window instance from the application instance.

Searching an Element in the Application:

1) To find the element in the application, we need to specify the treeScope of the AutomationElement.

2) We can specify the AutomationElement by providing conditions like ConditionFactory, ControlType, and action to be performed as explained in the below example.

ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());
mainWindow.FindFirstDescendant(cf.ByName("Text Editor")).AsTextBox().Enter("FlaUI Tutorial");
  

Code Explanation:

1. The first line is creating the “ConditionFactory” instance, the “ConditionFactory” is the class from “FlaUI.Core”, “ConditionFactory” identifies the element based on the condition.

2. We are specifying the TreeScope {FindFirstDescendant()}, ConditionFactory{ByName()}, ControlType {AsTextBox()} and action {Enter()} of the automation element.

The above helps us to install FlaUI and run an application. We must know how to inspect an element using FLAUInspect. We need the Chocolatey package manager to install Flauinspect, we will see the step by step process of the implementation below.

1. Chocolatey is the package manager for windows. The Flauinspect is a package under the Chocolatey package manager. So it is mandatory to install Chocolatey to inspect the AutomationElement.

2. Go to https://chocolatey.org/install and follow the instructions to install Chocolatey.

3. After installing chocolatey, run this command “choco install flauinspect” in command prompt to install Flauinspect.

4. To launch Faluinspect, run this command “flauinspect”, it will open the Flauinspect application.

Working with Flauinspect:

There are three ways for inspecting an element

Open Flauinspect > go to “Mode” >

1. Hover Mode (Press ctrl and move your mouse pointer focus on the target element)

2. Focus Tracking ()

3. Show XPATH, (combine with any mode listed above) and it will show only the absolute XPath.

Capture Screenshot with FlaUI:

Flaui provide a “Capture” class for the screenshot

Capture.screen();
// It will capture the full screen

Capture.Element("That Element ");
//It will capture the specific element 

Capture.Rectangle("Specify the height, width and length")
//It will capture the specific block you specified in the parameter.
  

We can store the screenshot in a file by using the below lines of code:

var image = Capture.Screen();
image.ApplyOverlays(new MouseOverlay(image.DesktopBounds));
image.ToFile(@"c:tempscreen.png");
  

NOTE: ToFile(“Your_Location”) method can be used to store the screenshot in a user-specified location.

Perform Mouse Action using FlaUI

The Flaui provides the Mouse Class with different methods as follows:

1. Mouse left click

Mouse.LeftClick();
// left click where the mouse pointer is currently pointed to.
  

2. Point

Point point = new Point(2435, 234 )
// This will provide the location of the mouse pointer (X-axis and Y-axis).
  

3. Middle Click

Mouse.Click(MouseButton.Middle, point);
  

4. MoveTo and MoveBy and Position

Mouse.MoveTo(point)
// Will position the mouse pointer to the New location
Mouse.MoveBy(point)
// it will take the current location and add the point specified in the method then drag the mouse to the location specified.
Mouse.Position(point)
// the traces of the mouse pointer are not displayed.
  

5. Scroll

Mouse.Scroll(10)
// positive value to scroll up ; Negative Value to scroll down

Mouse.HorizontalScroll(40);
Mouse.VerticalScroll(30);
  

Perform Keyboard Action using FlaUI

To perform any action the application via Keyboard, the FlaUI provides the Keyboard class with the following methods:

Keyboard.Type("Hello Flaui");   // for a string
Keyboard.Type(VirtualKeyShort.Key_B, VirtualKeyShort.Key_C);
Keyboard.Press(VirtualKeyShort.BACK)  // backspace

//For Combinations of multiple keystrokes (To press a key and release a key)
Keyboard.Pressing(VirtualKeyShort.CONTROL);
Keyboard.Press(VirtualKeyShort.Key_A);
Keyboard.Release(VirtualKeyShort.CONTROL);

//Another Way for the combinations key
Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL,VirtualKeyShort.KEY_A);

  

NOTE: VirtualKeyShort is available in “FlaUI.Core.WindowsAPI” package

How to highlight the element in FlaUI:

Flaui provides the “DrawHighlight” method which will highlight the AutomationElement.

Automation_Element.DrawHighlight();
  

Thank you for reading the above article, we would love to hear your comments. Please write to us if you face any issue in using FlaUI, we are happy to help. Happy Testing!


Frequently Asked Questions

  • What is FlaUI?

    FlaUI is an open-source .NET library based on the native UI automation libraries of Microsoft that can be used to automate UI-based testing of Windows applications such as win32, WinForms, WPF, and Windows Store apps.

  • Is FlaUI open source?

    Yes, FlaUI is indeed an open-source UI-based automation library that can be used to perform Automated UI testing of Windows applications such as win32, WinForms, WPF, and Windows Store apps.

  • How do I install the FlaUI library?

    1. Create a new project in Visual Studio.
    2. Right-click on the project and select “Manage NuGet Packages”.
    3. Select the ‘Browse’ tab and Search for FlaUI.
    4. Install the FLAui.core, Flaui.uia2, Flaui.uia3 libraries.

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility