DEV Community

Cover image for TestFly Part 5: BDD testing with Cucumber 7, BaseCucumberSteps and @retryable tags
Hakan GÜL
Hakan GÜL

Posted on Edited on Originally published at hakangul.lovable.app

TestFly Part 5: BDD testing with Cucumber 7, BaseCucumberSteps and @retryable tags

BDD with Cucumber 7 in TestFly

TestFly works with Cucumber 7 without extra boilerplate. CucumberHooks in io.testfly.cucumber manages the driver lifecycle, and CucumberStepLogger streams a step timeline straight into the HTML report. When a scenario fails, TestFly captures a screenshot and embeds it in both reports. To retry a single scenario, tag it with @retryable or @retryable=N.

Runner class (BaseCucumberTest)

The runner extends BaseCucumberTest. Add io.testfly.cucumber to the glue path and register CucumberStepLogger as a plugin.

package com.yourcompany.bdd;

import io.cucumber.testng.CucumberOptions;
import io.testfly.cucumber.BaseCucumberTest;

@CucumberOptions(
    features = "src/test/resources/features",
    glue     = {"com.yourcompany.bdd.steps", "io.testfly.cucumber"},
    plugin   = {"pretty", "io.testfly.cucumber.CucumberStepLogger"}
)
public class CucumberRunner extends BaseCucumberTest {}
Enter fullscreen mode Exit fullscreen mode

Step definitions with BaseCucumberSteps

Step classes extend BaseCucumberSteps and call its helpers (open, find, assertThat) directly. The assertThat check retries on its own until the condition holds.

package com.yourcompany.bdd.steps;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.testfly.cucumber.BaseCucumberSteps;
import org.openqa.selenium.By;

public class LoginSteps extends BaseCucumberSteps {

    @Given("the user is on the login page")
    public void onLoginPage() {
        open("/login");
    }

    @When("they login as {string} with password {string}")
    public void login(String username, String password) {
        find(By.id("username")).type(username);
        find(By.id("password")).type(password);
        find(By.id("submit")).click();
    }

    @Then("the dashboard is visible")
    public void dashboardVisible() {
        assertThat(By.id("dashboard")).isVisible(); // auto-retrying assertion
    }
}
Enter fullscreen mode Exit fullscreen mode

Per-scenario retry tag

Put the tag on the scenario you want retried:

@retryable=2
Scenario: Flaky payment gateway widget
  Given the user is on the payment screen
  When they submit payment details
  Then the transaction should be confirmed
Enter fullscreen mode Exit fullscreen mode

Top comments (0)