Skip to content

Behave (BDD) Example

A complete, runnable behave suite lives in the repository at examples/behave/. It automates the bundled WPF test application with Gherkin scenarios.

Install + run

behave is not a project dependency — install it first:

uv pip install behave
uv run behave examples/behave/features

The feature (features/simple_controls.feature)

Feature: WPF simple controls
  As a FlaUI for Python user
  I want to automate the bundled WPF test application
  So that I can verify common control interactions in plain language

  Scenario: The application launches with the expected title
    Given the WPF test application is running
    Then the window title is "FlaUI WPF Test App"

  Scenario: Enter text into the text box
    Given the WPF test application is running
    When I enter "hello from behave" into the text box
    Then the text box contains "hello from behave"

  Scenario: Toggle the test checkbox
    Given the WPF test application is running
    When I toggle the test checkbox
    Then the test checkbox state is inverted

Setup (features/environment.py)

before_all initialises the bridge, launches the app once, and stores the window and condition factory on the behave context:

"""Behave environment hooks for the FlaUI for Python example suite.

``before_all`` initialises the PythonNet bridge, launches the bundled WPF test application, and
stores the automation/window on the behave ``context``. ``after_all`` disposes everything.
"""

from pathlib import Path

# The bridge must be initialised before any C#-backed FlaUI type is used.
from flaui.lib.pythonnet_bridge import setup_pythonnet_bridge

setup_pythonnet_bridge()

from flaui.lib.enums import UIAutomationTypes
from flaui.modules.automation import Automation


def _find_test_app() -> Path:
    """Locate the bundled WPF test application by walking up from this file.

    :raises FileNotFoundError: If the bundled executable cannot be found.
    :return: The path to ``WpfApplication.exe``.
    """
    for parent in Path(__file__).resolve().parents:
        candidate = parent / "test_applications" / "WPFApplication" / "WpfApplication.exe"
        if candidate.exists():
            return candidate
    raise FileNotFoundError("Could not locate the bundled WPF test application under test_applications/.")


def before_all(context) -> None:
    """Launch the application once for the whole behave run.

    :param context: The behave run context.
    """
    context.automation = Automation(UIAutomationTypes.UIA3)
    context.automation.application.launch(str(_find_test_app()))
    context.automation.application.wait_while_main_handle_is_missing(3000)
    context.window = context.automation.application.get_main_window(context.automation)
    context.cf = context.automation.cf




def after_step(context, step) -> None:
    """Save a screenshot when a step fails, named after the scenario and step.

    :param context: The behave run context.
    :param step: The step that just ran.
    """
    if step.status == "failed":
        from flaui.core.capturing import Capture

        artifacts = Path("artifacts")
        artifacts.mkdir(exist_ok=True)
        name = "".join(c if c.isalnum() else "_" for c in f"{context.scenario.name}-{step.name}")
        with Capture.main_screen() as image:
            image.to_file(str(artifacts / f"{name}.png"))




def after_all(context) -> None:
    """Kill and dispose the application after the run.

    :param context: The behave run context.
    """
    context.automation.application.kill()
    context.automation.automation_base.dispose()

Steps (features/steps/simple_controls_steps.py)

"""Behave step implementations for the WPF simple-controls feature.

The application is launched once in ``environment.py`` and exposed on the behave ``context`` as
``context.window`` (the main window) and ``context.cf`` (the condition factory).
"""

from behave import given, then, when


@given("the WPF test application is running")
def step_app_running(context) -> None:
    """Assert the shared main window is available.

    :param context: The behave scenario context.
    """
    assert context.window is not None


@then('the window title is "{title}"')
def step_window_title(context, title: str) -> None:
    """Assert the window title matches.

    :param context: The behave scenario context.
    :param title: The expected window title.
    """
    assert context.window.title == title


@when('I enter "{value}" into the text box')
def step_enter_text(context, value: str) -> None:
    """Type ``value`` into the Simple Controls text box.

    :param context: The behave scenario context.
    :param value: The text to enter.
    """
    text_box = context.window.find_first_descendant(condition=context.cf.by_automation_id("TextBox")).as_text_box()
    text_box.text = value


@then('the text box contains "{value}"')
def step_text_box_contains(context, value: str) -> None:
    """Assert the text box currently holds ``value``.

    :param context: The behave scenario context.
    :param value: The expected text.
    """
    text_box = context.window.find_first_descendant(condition=context.cf.by_automation_id("TextBox")).as_text_box()
    assert text_box.text == value


@when("I toggle the test checkbox")
def step_toggle_checkbox(context) -> None:
    """Toggle the test checkbox, remembering its original state.

    :param context: The behave scenario context.
    """
    checkbox = context.window.find_first_descendant(condition=context.cf.by_name("Test Checkbox")).as_check_box()
    context.checkbox_original = checkbox.is_checked
    checkbox.toggle()


@then("the test checkbox state is inverted")
def step_checkbox_inverted(context) -> None:
    """Assert the checkbox state flipped, then restore the original state.

    :param context: The behave scenario context.
    """
    checkbox = context.window.find_first_descendant(condition=context.cf.by_name("Test Checkbox")).as_check_box()
    assert checkbox.is_checked != context.checkbox_original
    # Leave the shared application as we found it.
    checkbox.toggle()

Bridge before steps

behave imports step modules at startup, so setup_pythonnet_bridge() runs at the top of environment.py (loaded first) to guarantee the bridge is ready before any C#-backed type is touched.