Interview Questions on Reusability in Cucumber
How do you make steps reusable in Cucumber?
Use parameterized steps with placeholders like
{string}
or{int}
.
Can you reuse step definitions across multiple scenarios or feature files?
Yes. As long as the step text matches (or accepts dynamic values), the same step definition will be called across scenarios and feature files.
What happens if two step definitions match the same step?
Cucumber will throw an AmbiguousStepDefinitionsException. Make sure your steps are uniquely identifiable or scoped correctly.
How do you handle similar steps with slight variations?
Use parameterization to abstract variations. If needed, use Regex or write separate methods only for major differences.
What are other ways to make code reusable in a Cucumber framework?
- Page Object Model for UI logic
- Utility/helper classes for browser actions, wait conditions
- Hooks (
@Before
,@After
) for setup/cleanup - Data-driven scenarios with Examples/Table
- Scenario Outline with Examples
- Data Tables
- Regular Expressions in Step Definitions
- Scenario is used for a single set of data.
- Scenario Outline is used to run the same steps with multiple sets of data provided in an Examples table.
Scenario Outline: Login test
When user logs in with "<username>" and "<password>"
Then homepage should be displayed
Examples:
| username | password |
| admin | 1234 |
| user | 5678 |
When user provides credentials
| username | admin |
| password | 1234 |
- Scenario Outline is best for running the same scenario multiple times with different data sets.
- Data Table is best for providing structured data in a single scenario.
@When("^user logs in with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void user_logs_in(String username, String password) {
// login logic
}
- May reduce readability if over-complicated
- Difficult to debug when many rows fail
- Maintenance overhead with complex data combinations
Scenario Outline
instead of a DataTable?A: Use
Scenario Outline
when you want to run the same scenario multiple times with different sets of data. Use DataTable
when you want to pass structured input to a step inside a single scenario.Q1. What are tags in Cucumber?
Answer:
Tags in Cucumber are annotations (prefixed with @
) used to group scenarios or features, allowing selective test execution. They help manage and organize large test suites.
๐น Q2. Where can you apply tags in a feature file?
Answer:
You can apply tags:
-
Above the Feature to apply to all scenarios
-
Above individual Scenarios or Scenario Outlines
๐น Q3. What is the syntax to tag a scenario in a feature file?
Answer:
๐น Q4. How do you execute only tagged scenarios in Cucumber?
Answer:
By specifying the tag in your runner file:
โ Intermediate-Level Questions
๐น Q5. Can you use multiple tags on a scenario?
Answer:
Yes. A scenario can have multiple tags:
๐น Q6. What is the difference between AND, OR, and NOT with tags?
Answer:
-
@Tag1 and @Tag2
: Runs scenarios with both tags. -
@Tag1 or @Tag2
: Runs scenarios with either tag. -
not @Tag3
: Excludes scenarios with this tag.
๐น Q7. How to exclude a specific tag while executing scenarios?
Answer:
๐น Q8. How do tags help in CI/CD automation?
Answer:
Tags allow you to create logical test groups (like @Smoke
, @Sanity
, @Regression
), which can be triggered selectively in different stages of your CI/CD pipeline, saving time and resources.
โ Advanced/Behavioral Questions
๐น Q9. Can you create custom tags?
Answer:
Yes, any string prefixed with @
can be used as a custom tag. Example: @Login
, @AdminFlow
, etc.
๐น Q10. Have you used tags to organize a large test suite? How?
Answer:
โYes, in my project we had over 200 scenarios. We used tags like @Smoke
, @Regression
, @API
, @UI
, and @Critical
. Based on the tag, our CI pipeline would run quick smoke tests on every PR and full regression tests nightly.โ
๐น Q11. Can tags be used with Hooks in Cucumber?
Answer:
Yes. Hooks (@Before
, @After
) can be configured to run only for specific tags, like:
๐น Q12. Can the same tag be reused across multiple feature files?
Answer:
Yes, the same tag can be applied across different scenarios and features. It helps execute all related tests together.
๐น Q13. Can you tag steps in Cucumber?
Answer:
โ No. Cucumber does not support tagging individual steps. Tags are only for Feature, Scenario, or Scenario Outline.
Q1. What is the Background
keyword in Cucumber?
Answer:
The Background
keyword is used to define common steps that need to be executed before each scenario in a feature file. It helps avoid repetition of the same Given
steps across multiple scenarios.
๐น Q2. Where do you place the Background
section in a feature file?
Answer:
It is placed after the Feature
keyword and before the first Scenario
in the feature file.
๐น Q3. How many Background
blocks can you have in one feature file?
Answer:
Only one Background
block is allowed per feature file.
๐น Q4. Do Background
steps execute before every scenario?
Answer:
Yes, Background
steps are executed before each scenario and scenario outline example row in that feature file.
๐น Q5. What is the difference between Background
and Before
hooks?
Answer:
Background | @Before Hook |
---|---|
Written in feature file | Written in step definition Java class |
Readable to business users | Used for technical setup (e.g., DB, API, driver setup) |
Runs before each scenario | Can be customized using tags to run before specific scenarios |
โ Intermediate-Level Questions
๐น Q6. Can Background
contain steps like When
or Then
?
Answer:
Technically yes, but it's not recommended. Background
should be used for preconditions (Given
) only, to set the context.
๐น Q7. Will the Background
run for @Ignore
or @WIP
tagged scenarios?
Answer:
No, if the scenario is skipped due to filtering tags, the Background
will not run for those scenarios.
๐น Q8. Can I use Background
inside a Scenario Outline
?
Answer:
Yes. The Background
will run once for every set of examples (data row) in the Scenario Outline
.
โ Advanced/Behavioral Questions
๐น Q9. Whatโs a real-world example where youโve used Background
effectively?
Answer:
"In our e-commerce automation suite, every scenario required the user to be logged in. Instead of repeating the login steps in every scenario, we defined them once in the Background
. This made our tests cleaner and reduced maintenance effort."
๐น Q10. When would you choose NOT to use Background
and use @Before
hook instead?
Answer:
If the setup logic is technical or should only apply to scenarios with a specific tag (e.g., @API
, @Smoke
), I would use a @Before("@tag")
hook in the step definition class instead of Background
.
๐น Q11. Can we parameterize steps in Background
like we do in Scenario Outline
?
Answer:
No. The Background
section cannot use parameters or Examples
like Scenario Outline
. It's intended for static, common preconditions.
๐น Q12. How do Background
steps affect test reports?
Answer:
They appear in the report as pre-steps for each scenario and are marked as passed/failed independently of the scenario steps.