Automatization

Build the right tools right

Thucydides+JBehave+Maven Generate New Steps From Scenarios

| Comments

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.

1
2
3
4
5
Scenario: log in as autorized user
 
Given user on authorization page
When he logs in as John with password 123
Then 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-plugin src. Add it to your pom.xml build section with goal generate-steps. See example below:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
         <plugin>
            <groupId>net.thucydides.maven.plugin</groupId>
            <artifactId>maven-thucydides-jbehave-plugin</artifactId>
            <version>0.9.223</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate-steps</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

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.

APassingBehaviorSteps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package net.thucydides.maven.plugin.test;

import net.thucydides.maven.plugin.test.LoginSteps;
import java.lang.String;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.pages.Pages;
import net.thucydides.core.steps.ScenarioSteps;
import org.jbehave.core.annotations.When;

public class APassingBehaviorSteps extends ScenarioSteps {

    private static final long serialVersionUID = 1L;

    @Steps
    private LoginSteps loginSteps;

    public APassingBehaviorSteps(Pages pages) {
        super(pages);
    }

    @When("log in as autorized user $login $pass")
    public void logInAsAutorizedUser(String login, String pass) {
        loginSteps.givenUserOnAuthorizationPage();
        loginSteps.whenHeLogsInWithPassword(login, pass);
        loginSteps.thenOpensUsersPage();
    }

    @When("log in as autorized user")
    public void logInAsAutorizedUser() {
        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.

1
2
3
4
5
Scenario: log in as $login user
 
Given user on authorization page
When he logs in as John with password 123
Then opens user's page

And rerun plugin.

APassingBehaviorSteps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package net.thucydides.maven.plugin.test;

import net.thucydides.maven.plugin.test.LoginSteps;
import java.lang.String;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.pages.Pages;
import net.thucydides.core.steps.ScenarioSteps;
import org.jbehave.core.annotations.When;

public class APassingBehaviorSteps extends ScenarioSteps {

    private static final long serialVersionUID = 1L;

    @Steps
    private LoginSteps loginSteps;

    public APassingBehaviorSteps(Pages pages) {
        super(pages);
    }

    @When("log in as $login $pass")
    public void logInAslogin(String login, String pass) {
        loginSteps.givenUserOnAuthorizationPage();
        loginSteps.whenHeLogsInWithPassword(login, pass);
        loginSteps.thenOpensUsersPage();
    }

    @When("log in as $login")
    public void logInAslogin(String login) {
        loginSteps.givenUserOnAuthorizationPage();
        loginSteps.whenHeLogsInWithPassword(login, "123");
        loginSteps.thenOpensUsersPage();
    }

}

So now you can use your login step.

1
When log in as John

I hope this will be usefull for someone.

Comments