If you are using Jbehave framework, you know that it has very simple structure of .story file content.
Which consists of steps – Given,When,Then. And only steps are required part of the story. But this framework provides us to add meta-information to our tests for better understanding. One of the way to do this is use Scenario: sections.
12345
Scenario: log in as autorized userGiven user on authorization pageWhen he logs in as John with password 123Then opens user's page
It is very useful feature, which divide story into sections and makes it more readable/understandable.
But you can use Scenario to create your own steps library automatically.
To do this you need maven-thucydides-jbehave-pluginsrc. Add it to your pom.xml build section with goal generate-steps. See example below:
This plugin starts working on phase process-test-classes (by default).
And as result will generate new When steps for each Scenario section.
Let’s see his work on example. If you have APassingBehavior.story file with scenario shown above inside,
then plugin execution create class with two new steps.
packagenet.thucydides.maven.plugin.test;importnet.thucydides.maven.plugin.test.LoginSteps;importjava.lang.String;importnet.thucydides.core.annotations.Steps;importnet.thucydides.core.pages.Pages;importnet.thucydides.core.steps.ScenarioSteps;importorg.jbehave.core.annotations.When;publicclassAPassingBehaviorStepsextendsScenarioSteps{privatestaticfinallongserialVersionUID=1L;@StepsprivateLoginStepsloginSteps;publicAPassingBehaviorSteps(Pagespages){super(pages);}@When("log in as autorized user $login $pass")publicvoidlogInAsAutorizedUser(Stringlogin,Stringpass){loginSteps.givenUserOnAuthorizationPage();loginSteps.whenHeLogsInWithPassword(login,pass);loginSteps.thenOpensUsersPage();}@When("log in as autorized user")publicvoidlogInAsAutorizedUser(){loginSteps.givenUserOnAuthorizationPage();loginSteps.whenHeLogsInWithPassword("John","123");loginSteps.thenOpensUsersPage();}}
In result:
steps will have name as scenario title
first step will require to enter all parameters from inside steps explicitly
second step will use parameters from scenario
you can rewrite scenario to one step
1
When log in as autorized user
or
1
When log in as autorized user John 123
Also you can create parameterized Scenario Steps using ‘$’ sign, like in Jbehave.
Add parameters from any inside step to scenario title.
12345
Scenario: log in as $login userGiven user on authorization pageWhen he logs in as John with password 123Then opens user's page
packagenet.thucydides.maven.plugin.test;importnet.thucydides.maven.plugin.test.LoginSteps;importjava.lang.String;importnet.thucydides.core.annotations.Steps;importnet.thucydides.core.pages.Pages;importnet.thucydides.core.steps.ScenarioSteps;importorg.jbehave.core.annotations.When;publicclassAPassingBehaviorStepsextendsScenarioSteps{privatestaticfinallongserialVersionUID=1L;@StepsprivateLoginStepsloginSteps;publicAPassingBehaviorSteps(Pagespages){super(pages);}@When("log in as $login $pass")publicvoidlogInAslogin(Stringlogin,Stringpass){loginSteps.givenUserOnAuthorizationPage();loginSteps.whenHeLogsInWithPassword(login,pass);loginSteps.thenOpensUsersPage();}@When("log in as $login")publicvoidlogInAslogin(Stringlogin){loginSteps.givenUserOnAuthorizationPage();loginSteps.whenHeLogsInWithPassword(login,"123");loginSteps.thenOpensUsersPage();}}