Create a Step Definition File - ChrisMell/OEL-wiki GitHub Wiki

Creating a Step Definition File

In the src/test/java directory, create a new package if one is needed. Otherwise, you can work in a preexisting one.

Adding a New Step Definition

Let's say you have a Gherkin step that says Given user selects option buy where user is the name of the actor and buy is a parameter you want to pass to the function. You can create a new step definition by hovering the mouse over the step and clicking on Create step definition on the popup. From here, you can either create a new Java class file or add the step definition to a preexisting file.

Create Step Definition

You should see a new Cucumber tag and Java function that looks like this.

New Step Definition

Next, you need to modify the Cucumber tag to look for parameters and pass them to the function. Replace the word user with {actor} and the word buy with {word}. You'll also need to add the parameters to the function as well. As a convention, all functions should at least have an actor parameter even if it's not used in the Java code. You can optionally change the function name at this point.

Your Cucumber tag and Java function should look something like this.

@Given("{actor} selects option {word}")
public void selectsOption(Actor actor, String option) {
}

After you make this change, IntelliJ should automatically highlight the parameters in the Gherkin step.

Finished Step Definition

At this point, you can begin working on adding the Java code to perform the step. The next section will provide action steps unique to Serenity that you are likely to use.

Common Serenity Action Steps

This page has common tools included in Serenity libraries which will be useful for automating the test cases. More information on these tools can be found on the Serenity BDD Book.

Click

Use this library to click on elements which are clickable such as buttons and dropdowns.

Click.on(PackageObjects.PAGEOBJECT_TARGET_VARIABLE)

More information on clicking can be found here.

Enter

Use this library to type a value into a text field. A general example looks like this

Enter.theValue("Text").into(PackageObjects.PAGEOBJECT_TARGET_VARIABLE)

More information on entering text can be found here.

SelectFromOptions

Use this to open a dropdown menu and make a selection.

SelectFromOptions.byVisibleText("Text").from(PackageObjects.PAGEOBJECT_TARGET_VARIABLE)

Note that dropdowns can be be tricky. More information on working with dropdowns can be found here.

Ensure

Use this to perform fluent assertions whenever a particular value needs to be verified.

Ensure.that(PackageObjects.PAGEOBJECT_TARGET_VARIABLE).isDisplayed()

You will need to include import static net.serenitybdd.screenplay.matchers.WebElementStateMatchers.*; so the isDisplayed() method works. More methods for Serenity ensure can be found here.

WaitUntil

Use this to add an implicit wait for when you want the test execution to wait for a certain property to be true.

WaitUntil.the(PackageObjects.PAGEOBJECT_TARGET_VARIABLE, isClickable())

You will need to include import static net.serenitybdd.screenplay.matchers.WebElementStateMatchers.*; so the isClickable() method works. More information on waits can be found here.