Testing / Cucumber Interview Questions
What is a step definition in Cucumber?
A step definition is a piece of code, in whichever language the Cucumber implementation targets (Java, Ruby, JavaScript, and so on), that implements the actual behavior for a specific Gherkin step by matching that step's text to a pattern.
@Given("a registered user {string} with password {string}") public void a_registered_user_with_password(String username, String password) { userRepository.create(username, password); } @When("{string} logs in with password {string}") public void logs_in_with_password(String username, String password) { loginResult = loginService.login(username, password); } @Then("the login should succeed") public void the_login_should_succeed() { assertTrue(loginResult.isSuccess()); }
When Cucumber runs a scenario, it looks for a step definition whose pattern matches each Gherkin line's text, extracts any parameters from that match (like "alice" and "secret123" above), and invokes the corresponding method with those values, one step definition method executing per Gherkin step.
More Related questions...