Example Gallery
Complete, runnable automations of real applications. Each one is a plain Python script under
examples/scripts/,
with no test framework involved. Each is shown next to the equivalent FlaUI C# program. The
Python tabs are included from the scripts themselves, so what you read here is what runs.
| Application | Kind | Script | Run it |
|---|---|---|---|
| Bundled WPF / WinForms app | Custom desktop app | quickstart.py |
uv run python examples/scripts/quickstart.py --app wpf |
| Calculator | Windows Store (UWP) | calculator.py |
uv run python examples/scripts/calculator.py |
| Notepad | Win32 (Win 10) / Store (Win 11) | notepad.py |
uv run python examples/scripts/notepad.py |
Want a test suite instead of a script?
The framework examples drive the same bundled app with pytest, unittest, behave and Testplan. The Element Cookbook has one recipe per control.
Bundled WPF / WinForms test app
The repository ships two test applications under test_applications/: a WPF one and a WinForms
one, with the same controls in each. quickstart.py drives either app on either automation
stack, so you can try all four combinations:
uv run python examples/scripts/quickstart.py --app wpf --uia 3
uv run python examples/scripts/quickstart.py --app winforms --uia 2
from flaui.lib.pythonnet_bridge import setup_pythonnet_bridge
setup_pythonnet_bridge() # must run before any C#-backed FlaUI import
from flaui.core.definitions import ControlType, ToggleState
from flaui.lib.enums import UIAutomationTypes
from flaui.modules.automation import Automation
# `with` disposes the automation and closes the application on exit.
with Automation(uia) as automation:
automation.application.launch(executable)
automation.application.wait_while_main_handle_is_missing(3000)
window = automation.application.get_main_window(automation)
cf = automation.cf
print(f"Launched: {window.title}")
text_box = window.find_first_descendant(cf.by_automation_id("TextBox")) # by AutomationId (preferred)
checkbox = window.find_first_descendant(cf.by_name("Test Checkbox")) # by Name (visible text)
buttons = window.find_all_descendants(cf.by_control_type(ControlType.Button)) # by ControlType
print(f"Found {len(buttons)} buttons")
text_box.as_text_box().text = "Hello from FlaUI for Python"
cb = checkbox.as_check_box()
cb.toggle()
print("Checkbox is on:", cb.toggle_state == ToggleState.On)
cb.toggle() # put it back
using FlaUI.Core;
using FlaUI.Core.Definitions;
using FlaUI.UIA3;
using var automation = new UIA3Automation();
using var app = Application.Launch(executable);
var window = app.GetMainWindow(automation);
var cf = automation.ConditionFactory;
Console.WriteLine($"Launched: {window.Title}");
var textBox = window.FindFirstDescendant(cf.ByAutomationId("TextBox"));
var checkBox = window.FindFirstDescendant(cf.ByName("Test Checkbox"));
var buttons = window.FindAllDescendants(cf.ByControlType(ControlType.Button));
Console.WriteLine($"Found {buttons.Length} buttons");
textBox.AsTextBox().Text = "Hello from FlaUI";
var cb = checkBox.AsCheckBox();
cb.Toggle();
Console.WriteLine($"Checkbox is on: {cb.ToggleState == ToggleState.On}");
cb.Toggle();
Calculator
Calculator is a Windows Store (UWP) app. It is launched by its AppUserModelId and driven through its stable button AutomationIds. Store apps need UIA3.
"""Gallery: automate the built-in Windows Calculator (a UWP / Store app).
Shows how to launch a Store app by its AppUserModelId, address buttons by AutomationId, and read a
result back. Calculator's AutomationIds are stable across Windows 10 and Windows 11.
Run it from the project root::
uv run python examples/scripts/calculator.py
"""
from flaui.lib.pythonnet_bridge import setup_pythonnet_bridge
setup_pythonnet_bridge() # must run before any C#-backed FlaUI import
from flaui.core.tools import Retry
from flaui.lib.enums import UIAutomationTypes
from flaui.modules.automation import Automation
CALCULATOR_AUMID = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
def main() -> None:
"""Compute ``12 + 30`` in Calculator and print the display."""
# Store apps want UIA3; UIA2 cannot see inside the XAML island.
with Automation(UIAutomationTypes.UIA3) as automation:
automation.application.launch_store_app(CALCULATOR_AUMID)
window = automation.application.get_main_window(automation)
cf = automation.cf
def press(automation_id: str) -> None:
"""Invoke the Calculator button with the given AutomationId."""
window.find_first_descendant(cf.by_automation_id(automation_id)).as_button().invoke()
press("clearButton")
for key in ("num1Button", "num2Button", "plusButton", "num3Button", "num0Button", "equalButton"):
press(key)
display = window.find_first_descendant(cf.by_automation_id("CalculatorResults"))
# The display's Name reads e.g. "Display is 42"; wait for the result to settle.
Retry.WhileFalse(lambda: display.name.endswith("42"), timeout=2000)
print(display.name)
assert display.name.endswith("42"), display.name
# Leaving the `with` block closes Calculator and disposes the automation.
if __name__ == "__main__":
main()
using FlaUI.Core;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using var automation = new UIA3Automation();
using var app = Application.LaunchStoreApp("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
var window = app.GetMainWindow(automation);
var cf = automation.ConditionFactory;
void Press(string id) => window.FindFirstDescendant(cf.ByAutomationId(id)).AsButton().Invoke();
Press("clearButton");
foreach (var key in new[] { "num1Button", "num2Button", "plusButton", "num3Button", "num0Button", "equalButton" })
Press(key);
var display = window.FindFirstDescendant(cf.ByAutomationId("CalculatorResults"));
Retry.WhileFalse(() => display.Name.EndsWith("42"), TimeSpan.FromSeconds(2));
Console.WriteLine(display.Name); // "Display is 42"
Notepad
On Windows 11, notepad.exe is only a launcher stub: it starts the Store version of Notepad and
exits right away. The script therefore finds the window on the desktop by its class name instead
of using the launched process. It also sets the text through the Value pattern, so no keystrokes
are sent and nothing else on the desktop can get mixed into the input.
"""Gallery: automate Notepad - write text, read it back, then discard it.
Works with both the classic Win32 Notepad (Windows 10) and the Store Notepad (Windows 11). On
Windows 11 ``notepad.exe`` is only a stub that hands off to the Store app and exits, so instead of
trusting the launched process we look the window up on the desktop by its class name.
Run it from the project root::
uv run python examples/scripts/notepad.py
"""
from flaui.lib.pythonnet_bridge import setup_pythonnet_bridge
setup_pythonnet_bridge() # must run before any C#-backed FlaUI import
from flaui.core.definitions import ControlType
from flaui.core.tools import Retry
from flaui.lib.enums import UIAutomationTypes
from flaui.modules.automation import Automation
def main() -> None:
"""Open Notepad, write into it, verify the text, and close it without saving."""
with Automation(UIAutomationTypes.UIA3) as automation:
cf = automation.cf
desktop = automation.automation_base.get_desktop()
automation.application.launch("notepad.exe")
# Classic and Store Notepad both use the top-level window class "Notepad".
# `find_first_child` raises ElementNotFound until the window exists, so retry while it raises.
window = Retry.WhileException(
lambda: desktop.find_first_child(cf.by_class_name("Notepad")),
timeout=10000,
throw_on_timeout=True,
timeout_message="Notepad window did not appear",
).as_window()
window.focus()
# Classic Notepad exposes an Edit control; Store Notepad a Document. Take whichever exists.
editor = window.find_first_descendant(
cf.by_control_type(ControlType.Document) | cf.by_control_type(ControlType.Edit)
)
# Set the text through the Value pattern: no keystrokes, so nothing else on the desktop can
# interleave with it. Use `Keyboard.type(...)` instead when you need real key events.
text_box = editor.as_text_box()
text_box.text = "Hello from FlaUI for Python"
print("Notepad says:", text_box.text)
assert text_box.text == "Hello from FlaUI for Python", text_box.text
# Empty the document again so closing neither prompts to save nor restores the text next time.
text_box.text = ""
window.close()
if __name__ == "__main__":
main()
using FlaUI.Core;
using FlaUI.Core.Definitions;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using var automation = new UIA3Automation();
var cf = automation.ConditionFactory;
var desktop = automation.GetDesktop();
Application.Launch("notepad.exe");
var window = Retry.WhileNull(
() => desktop.FindFirstChild(cf.ByClassName("Notepad")),
TimeSpan.FromSeconds(10), throwOnTimeout: true).Result.AsWindow();
window.Focus();
var editor = window.FindFirstDescendant(
cf.ByControlType(ControlType.Document).Or(cf.ByControlType(ControlType.Edit))).AsTextBox();
editor.Text = "Hello from FlaUI";
Console.WriteLine($"Notepad says: {editor.Text}");
editor.Text = "";
window.Close();
Python vs C# differences you can see here
- In C#,
FindFirstChildreturnsnullwhen nothing matches. The Python wrapper raisesElementNotFoundinstead, so the Python script polls withRetry.WhileExceptionwhere C# usesRetry.WhileNull. using varin C# corresponds towith Automation(...) as automation:in Python. Both dispose the automation and close the app when the block ends.