Testing / Cucumber Interview Questions
What is the difference between imperative and declarative Gherkin style?
Imperative-style scenarios spell out every low-level UI or API interaction step by step, closely mirroring exactly how a user would click through a workflow. Declarative-style scenarios instead describe the outcome or intent in business terms, leaving the low-level mechanics to be handled inside the step definitions rather than spelled out in the Gherkin itself.
# Imperative style Scenario: login Given the user navigates to "/login" And the user enters "alice" in the username field And the user enters "secret123" in the password field And the user clicks the "Login" button Then the user should see the dashboard # Declarative style Scenario: login Given "alice" is a registered user When "alice" logs in with valid credentials Then "alice" should see the dashboard
Declarative scenarios are generally preferred for a few concrete reasons: they read more naturally to business stakeholders, since the step text describes intent rather than UI mechanics; and they're far more resilient to change, since a UI redesign that moves a button or renames a field only requires updating the corresponding step definition's implementation, not every scenario that happens to reference a login flow. Imperative style still has a place for very low-level, mechanics-focused tests where the exact interaction sequence genuinely is the point being tested.
More Related questions...