Skip to content

marcinooo/robot-framework-state-machine

Repository files navigation

State Machine for Robot Framework

https://dl.circleci.com/status-badge/img/gh/marcinooo/robot-framework-state-machine/tree/main.svg?style=svg

Author:marcinooo
Tags:Robot Framework, Python, State Machine, Library
abstract:Implementation of state machine in robot framework.

Library contains implementation of state machine to control or test software components which can be in many states.

Library documentation can be found here.

A good starting point is to check examples:

Install from PyPI:

$ pip install robotframework-statemachinelibrary

Install from github:

$ pip install git+https://github.com/marcinooo/robot-framework-state-machine

An example of using the library for LED blinking (of course, the library was created for more complex tasks 😉).

*** Settings ***
Library  StateMachineLibrary

*** Tasks ***
Blink
    [setup]  Task Setup
    Run State Machine  start_from=Turn On Light  max_updates=10

*** Keywords ***
Task Setup
    Create State Machine  name=blink-machine
    Add State  state=Turn On Light   on_update=On Update Turn On Light
    Add State  state=Turn Off Light  on_update=On Update Turn Off Light

Turn On Light
    Log To Console  I am ON

On Update Turn On Light
    Sleep  0.25s
    Go To State  state=Turn Off Light

Turn Off Light
    Log To Console  I am OFF

On Update Turn Off Light
    Sleep  0.25s
    Go To State  state=Turn On Light

Flow diagram:

Flow diagram for above code

First of all import the library:

Library  StateMachineLibrary

Create a state machine:

Create State Machine  name=blink-machine

You can create as many as you want state machines. Each state machine should have a unique name.

Register keywords that should be executed in the given state (Turn On Light) and during its update (On Update Turn On Light):

Add State  state=Turn On Light   on_update=On Update Turn On Light

Both keywords must be defined:

*** Keywords ***
# ...
Turn On Light
    # Here you can put logic (e.g.: led controller)
    Log To Console  I am ON

On Update Turn On Light
    Go To State  state=Turn Off Light

Keywords On Update... should indicate the next state:

Go To State  state=Turn Off Light

Call the same keyword to enter the first state.

Force transition to a next state:

Update State

You can pass data between states in context (recommended method) or using global variables.

*** Keywords ***
# ...

Turn On Light
    Update Context  led_status=ON

Turn Off Light
    # ...
    &{context}=    Get Context
    Log To Console    LED is ${context["led_status"]}

license (MIT)

About

State machine implementation for Robot Framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published