Storyteller 5.1.0


Next

Importing and Currying Grammars

Previous

Verifying Sets

Embedded Sections Edit on GitHub


A useful technique with Storyteller is to use an Embedded Section that allows you to embed the usage of one Fixture into the usage of a second Fixture.

To make this concrete, let's say that you have a Fixture to interact with the main page of a web application portal project. The main page probably has several distinct areas of functionality, including a modal popup that allows a user to set their usage preferences. When you design your specification language for the application portal, you con't necessarily want to include the language for the preferences directly into your main Fixture. Instead, you create a second Fixture just to interact with the preferences popup like so:


public class PreferencesFixture : Fixture
{
    [FormatAs("Enable notifications? {enabled}")]
    public void EnableNotifications(bool enabled)
    {
        
    }

    [FormatAs("Enable GPS Tracking? {enabled}")]
    public void EnableGPS(bool enabled)
    {
        
    }
}

To embed that PreferencesFixture into the main application fixture, you use the Fixture.Embed<T>(title) method to create an embedded section like this:


public IGrammar Preferences()
{
    return Embed<PreferencesFixture>("Set the current user's preferences")
        .Before(c =>
        {
            // Do something to open the preferences dialog
        })
        .After(c =>
        {
            // Do something to close the preferences dialog
        });
}

In usage, the embedded section looks like a nested box within the section that contains it, like this:

The Before() and After() methods are optional. In both cases, the signature takes in an Action<ISpecContext> that will be executed every time the embedded section grammar is executed.

Possible Usages

  • To reuse grammar functionality across logical areas
  • A team at my shop uses embedded sections to describe sections in a big, branching questionaire
  • Interacting with a modal or popup dialog in a user interface testing project
  • To specify a complex message for API testing
  • To specify variable options without cluttering up the main Fixture

Before and After Actions

Note! As of 4.2, Storyteller also allows you to use asynchronous actions as the "Before" and "After" actions

When you create an Embedded Section grammar, you can specify actions that will be executed before and after the steps in the embedded section. Note that the Before() action is executed before the nested Fixture's SetUp() method. Likewise, the After() method is executed after the nested Fixture's TearDown() method.


public class EmbeddedGrammarFixture : Fixture
{
    public EmbeddedGrammarFixture()
    {
        this["Colors"] = Embed<RecordingFixture>("In the recording fixture");

        this["BeforeColors"] = Embed<RecordingFixture>("In the recording fixture")
            .Before(c =>
            {
                c.Fixture.AddTrace("Before");
            });

        this["AfterColors"] = Embed<RecordingFixture>("In the recording fixture")
            .After(c =>
            {
                c.Fixture.AddTrace("After");
            });
    }
}