There are situations where we might need to call a step or steps from within a step definition, so that
Number of steps within feature files can be reduced
Number of duplicate steps can be used
Hence the scenarios will look something like this, the second scenario step definition as you can see has a step (Given I login and enter user details) performs all the operation of scenario one by calling steps within step definition
Here is the complete video of the above discussion
Here is the complete code of the above video
[Given(@"I login and enter user details")]
public void GivenILoginAndEnterUserDetails()
{
//Create a column header
string[] colHeader = { "UserName", "Password" };
string[] row = { "admin", "admin" };
//Created a table with values
var table = new Table(colHeader);
table.AddRow(row);
Given("I login to application", table);
string[] colHeaderForUF = { "Title", "FirstName", "Initial" };
string[] rowForUF = { "Mr.", "Karthik", "KK" };
table = new Table(colHeaderForUF);
table.AddRow(rowForUF);
Given("I enter following details", table);
}
Thanks for reading the post and watching the video!!!
Please leave your comments and let me know if there is anything I should update in this post.
Thanks,
Karthik KK
Post Author:
Karthik kk
5 Replies to “Calling steps from step definition”
Hi Karthik,
I try to call a step from another step, but get a syntax error from C# not recognizing Given as a keyword in:
using TechTalk.SpecFlow;
…
namespace …
{
[Binding]
public class HomePageSteps
{
…
[Then(@”the user opens application”)
public void ThenTheUserOpensApplication()
{
Given(“the user navigates to the Home Page”);
….
How do I make C# understand that Given is a keyword for SpecFlow?
Thanks … gyorgy …
Hey Karthik, This code snippet is really helpful, can we use same semantics for Java & Cucumber framework? to carryout the same expectation of calling steps in an another step.
Can you please share the syntax with typescript cucumber as well for the above scenarios. Where the reduce the number of steps or does it support typescript?
Hi Karthik,
I try to call a step from another step, but get a syntax error from C# not recognizing Given as a keyword in:
using TechTalk.SpecFlow;
…
namespace …
{
[Binding]
public class HomePageSteps
{
…
[Then(@”the user opens application”)
public void ThenTheUserOpensApplication()
{
Given(“the user navigates to the Home Page”);
….
How do I make C# understand that Given is a keyword for SpecFlow?
Thanks … gyorgy …
You actually need to inherit from Steps class in your HomePageSteps, something like this
public class HomePageSteps
Here is the complete detail in the video of below time
https://youtu.be/DLoPdffwqsk?t=499
Thanks,
Karthik KK
Hey Karthik, This code snippet is really helpful, can we use same semantics for Java & Cucumber framework? to carryout the same expectation of calling steps in an another step.
Its not supported in Cucumber for Java, its very specific to Specflow with C#
Hi Karthik,
Can you please share the syntax with typescript cucumber as well for the above scenarios. Where the reduce the number of steps or does it support typescript?