Navigate to Specflow step from within Step Definitions

天涯浪子 提交于 2019-12-12 03:43:03

问题


So I have a few tests where i've reused steps from within steps.

But i'm now having a nightmare on the maintenance front, in that I cant easily navigate between the steps.

Here's an example:

    [Given(@"I have an order")]
    public void GivenIHaveAnOrder()
    {
        Given("an open store");
        Given("I am an existing customer");
        Given("I am on homepage");
        When("I search for a component");
        When("I add the component to my basket");
    }

How do I navigate to one of those internal steps?

If I wanted to navigate to the "When("I search for a component");" step I cant.

If I were on the feature file I could simply right click the step and "go to definition" but I cant do that here. Does anyone have a solution?


回答1:


I assume that you are calling the steps with the Given/When- functions, because they are in a different binding class. Am I right?

There is a better way of doing it, than using this functions.

Did you had a look at the driver concept and context injection? Have a look here: http://www.specflow.org/documentation/Context-Injection/

Simply extract your logic of your steps to a driver class and get an instance from it in the different step classes:

class Driver 
{
    public void AnOpenStore()
    {
        ...
    } 
}

[Binding]
public class StepClass1
{
     private Driver _driver;   

     public StepClass1(Driver driver)
     {
          _driver = driver;
     }

     [Given(@"I have an order")]
     public void IHaveAnOrder()
     {
          _driver.AnOpenStore();
     }
}

[Binding]
public class StepClass2
{
     private Driver _driver;   

     public StepClass2(Driver driver)
     {
          _driver = driver;
     }

     [Given(@"an open store")]
     public void AnOpenStore()
     {
          _driver.AnOpenStore();
     }
}

When you arrange your step implementations like that, the reusing of other steps is much more easier.



来源:https://stackoverflow.com/questions/38723867/navigate-to-specflow-step-from-within-step-definitions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!