How to reuse the step definition in other step definition in SpecFlow

2,050 views
Skip to first unread message

Sreenath S

unread,
Feb 29, 2012, 10:10:17 AM2/29/12
to SpecFlow
Hi All,

I am trying to use the step in Second_step_definition.cs file and this
step was already created in First_step_definition.cs file.

I have created a step as follows in First_step_definition.cs (for
First feature)

[Given(@"I have navigated to google email page")]
public void GivenIHaveNavigatedToGoogleEmailPage()
{
IWebdriver driver = new
InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.gmail.com");
Thread.Sleep(4000);
}

Then, I have created Second_step_definition.cs (for Second feature)

[Given(@ "I have clicked on inbox link")]
public void GivenIhaveClickedOnInboxLink()
{
}

Sreenath S

unread,
Feb 29, 2012, 10:18:51 AM2/29/12
to SpecFlow
Hi All,

I am beginner for Specflow learning. I am trying to use the step in
IWebdriver driver1 = new InternetExplorerDriver();
driver.Navigate().FIndElement(By.Id("inboxId")).click();
}

Now, I would like to use the step defined [Given(@"I have navigated to
google email page")] in the First_step_definition.cs in the
Second_step_definition.cs. [Reuse purpose, to avoid the duplicating
the same step again]

In this scenario, how can I access/use the step (which was defined in
First_step_definition.cs) to Second_step_definition.cs file.

Could someone please guide me.

Thanks
Sreenath

Gáspár Nagy

unread,
Mar 6, 2012, 4:36:27 AM3/6/12
to SpecFlow
One option is just call the GivenIHaveNavigatedToGoogleEmailPage()
method simply.

Another way would be to derive your step definition class from
TechTalk.SpecFlow.Steps, and call:
Given("I have navigated to google email page")
from the second step definition method.

Br,
Gaspar

PS: you can check the BookShop sample for other ideas:
https://github.com/techtalk/SpecFlow-Examples/tree/master/ASP.NET-MVC
Reply all
Reply to author
Forward
0 new messages