0
votes

Steps matching can be determined by Scope: https://github.com/techtalk/SpecFlow/wiki/Scoped-bindings . But this is static mechanism. Is there a way to make this mechanism dynamic so that:

  1. In single scenario I have two or more same steps
  2. There are many step definitions for this step decorated with (attribute?)
  3. I have step to change context and base on key set in this step different method will be called

This is like Tag restriction from Scope mechanism + dynamicity.

I test application with multiple tables on different pages. I must verify if:

  • table contains rows (is subset)
  • table have exact rows (items and order) (is equal)
  • table have rows (but order is not important).

I am writing:

When I am on Page1
Then I expect that table contains
| Column1Name | Column2Name | Column3Name |
| row1id             | true                 | address          |
| row2id             | true                 | address          |
When I do some change action on rows
| Column1Name |
| row1id             |
Then I expect that table contains
| Column1Name | Column2Name |
| row1id             | false                |
| row2id             | true                 |

I have only one step definition for “I expect that table contains”. It verifies cells provided in step. This step is used on different pages for different columns and pages. These page shows different business objects, have different columns but ui is quite common. Without this I would have to write steps for each page and each column combination.

But as always there are some specific actions that should be taken before “I expect that table contains”. For instance:

  • different search filter to use on different pages.
  • timeout that is required before action changes appears on ui (services, cache, etc)

I don’t want to write it explicitly because it makes that scenario is overloaded with unnecessary informations.

1
This isn't possible and it sounds like its potentially confusing. Why do you think you want this? What is your use case? There may be a better solution than the one you are trying to findSam Holder
Thanks for answer. I agree with you that it might be confusing. I edited my question and add sample scenario draft and concept. I want to avoid writing to many same/similar steps definitions in my case.bwojdyla

1 Answers

0
votes

As Sam Holder wrote it is not possible but I achived it with injecting behavior to Step class. I register Behaviors with SpecFlow.Autofac.SpecFlowPlugin as named services https://github.com/gasparnagy/SpecFlow.Autofac Here is sample code. Implementaion is much more complex. It is only draft.

There is step that changes context key. Same that is provided above Behavior class.

    public interface ITableBehavior
    {
        void Search(Table table);
    }

    [ContextKey("Page1")]
    public class Page1Behavior : ITableBehavior
    {
        public void Search(Table table)
        {
            throw new System.NotImplementedException();
        }
    }

    [ContextKey("Page2")]
    public class Page2Behavior : ITableBehavior
    {
        public void Search(Table table)
        {
            throw new System.NotImplementedException();
        }
    }

    [Binding]
    public class Step
    {
        private ITableBehavior TableBehavior { get { return BehaviorResolver.Resolve<ITableBehavior>(); } }

        [Then("I expcet that table contains")]
        public void TableContains(Table table)
        {
                TableBehavior.Search(table);
                //table contains implementation
        }
    }