Overview - Galad/tranquire GitHub Wiki

Introduction

An actor is the core concept in Tranquire. It represents an entity that exercises the system under test (SUT). Typically a user is represented as an actor. Actors can execute actions that change the state of the SUT, and ask questions about its state. Actions are simple objects that represent a command, while questions are objects that represent a queries.

For instance, when testing a to do list web application, we can create an action that represent adding an item to the list, that we implement with Selenium. Then we can create a question that returns the items contains in the list and verify that it contains the item we just added. Such a test would look like that with Tranquire:

// creates the actor
var john = new Actor("John");
var driver = new ChromeDriver();
john = john.CanUse(WebBrowser.With(driver));
// arrange
var item = "Buy bread"
// act
john.When(ToDoItem.AddAToDoItem(item));
// assert
var actual = john.AsksFor(TheItems.Displayed());
Assert.That(actual, Contains.Item(item));

Actions and questions can be combined to create more complex actions. So you can easily create an action that add multiple items by reusing ToDoItem.AddAToDoItem.

Actor

In Tranquire, an actor is represented with the Actor class. An actor is created the following way:

var john = new Actor("John");

Once the actor is created, it can execute action or ask questions.

An action can be executed with the Given or the When methods. In Tranquire, actions are executed in one of the 2 contexts: Given and When. The goal is to allow different implementations of a particular action in different context. For instance, an action can use the web browser in the When context and use an API in the Given context. This approach allows to faster actions in the Given context.

// arrange
john.Given(ToDoItem.AddAToDoItem(item));
// act
john.When(ToDoItem.DeleteAToDoItem(item));

Actions

An action is implemented using IAction<TResult> or IAction<TAbility, TResult>. You can also inherit from ActionUnit if the action does not produce any value.

Here is an example of an action, implemented with inheriting from ActionUnit

public class AddAToDoItem : ActionUnit
{
    private readonly string item;

    public AddAToDoItem(string item)
    {
        this.item = item;
    }

    public override string Name => "Add the item " + this.item;

    protected override void ExecuteWhen(IActor actor)
    {
        john.Execute(Enter.TheValue(item).Into(ToDoPage.NewToDoItemInput));
        john.Execute(Hit.Enter().Into(ToDoPage.NewToDoItemInput));
    }
}

Questions

A question is implemented using IQuestion<TAnswer> or IQuestion<TAnswer, TAbility>. You can also inherit from Question<TAnswer> or Question<TAnswer, TAbility>.

Here is an example of a question, implemented by inheriting from Question<T>:

public class DisplayedItems : Question<ImmutableArray<string>>
{
    protected override ImmutableArray<string> Answer(IActor actor)
    {
        return actor.AsksFor(Element.Of(ToDoPage.ToDoItem).Many().AsText());
    }

    public override string Name => "Displayed items";
}

Abilities

Sometimes, an action or a question requires a particular ability to be implemented. For instance, actions created with Selenium requires to have a WebDriver (which is represented by the WebBrowser ability in Tranquire.Selenium). You can register an ability on the actor by using the CanUse method:

actor.CanUse(WebBrowser.With(CreateDriver()));
// it also support registering abilities lazily
actor.CanUse(new Lazy<WebBrowser>(() => WebBrowser.With(CreateDriver())));

The instance passed to the CanUse method will be available to all actions and questions that requires it.

To implement an action or a question with an ability, you can implement the IAction<TAbility, TResult> or IQuestion<TAnswer, TAbility> interfaces respectively. The method to implement will have an additional argument called ability. Examples:

protected override void ExecuteWhen(IActor actor, WebBrowser ability);
protected override ImmutableArray<Model.ToDoItem> Answer(IActor actor, WebBrowser ability);
⚠️ **GitHub.com Fallback** ⚠️