Interview Questions

 

Interview Questions on Reusability in Cucumber

How do you make steps reusable in Cucumber?

  • Use parameterized steps with placeholders like {string} or {int}.

  • @When("user login into application with {string} and password {string}")

    public void login(String user, String pass) { ... }


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

parameterization interview questions

Q: What is parameterization in Cucumber?
A: Parameterization in Cucumber allows us to run the same scenario multiple times with different input data. It helps avoid duplication and improves test coverage.


Q: What are different ways to achieve parameterization in Cucumber?
  • Scenario Outline with Examples
  • Data Tables
  • Regular Expressions in Step Definitions

Q: What is the difference between Scenario and Scenario Outline in Cucumber?
  • 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.

Q: How do you use Scenario Outline in a feature file?

Scenario Outline: Login test

When user logs in with "<username>" and "<password>"

Then homepage should be displayed


Examples:

| username | password |

| admin     | 1234     |

| user         | 5678     |



Q: How do you pass multiple data fields in a single step?
A: Use Data Tables. Example:

When user provides credentials

| username | admin |

| password | 1234 |



Q: What is the difference between using Scenario Outline and Data Table?
  • 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.

Q: Can we use Data Tables for multiple rows of test data like Scenario Outline?
A: Yes, but we must iterate through the list of maps or rows manually in the step definition.


Q: How do you define step definitions for dynamic values using regular expressions?

@When("^user logs in with username \"([^\"]*)\" and password \"([^\"]*)\"$")

public void user_logs_in(String username, String password) {

// login logic

}



Q: Can we use parameterization in Background steps?
A: No, Background steps are shared across all scenarios and cannot be parameterized directly. But you can use hooks or utility methods to dynamically set values.

Q: How does Cucumber handle complex objects (like JSON or POJO) in parameterized steps?
A: You can use custom parameter types or parse DataTables into POJOs using @DataTableType.

Q: Give an example of a real scenario where parameterization helped in your project.
"We had a login functionality for multiple user roles (admin, user, guest). Using Scenario Outline, we tested all combinations without repeating steps, which improved coverage and reduced maintenance."

Q: What are the drawbacks of overusing parameterization in Cucumber?
  • May reduce readability if over-complicated
  • Difficult to debug when many rows fail
  • Maintenance overhead with complex data combinations

Q: When would you use 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:

gherkin
@Smoke Scenario: Valid login Given user is on login page When user logs in with "admin" and "1234" Then homepage should be displayed

๐Ÿ”น Q4. How do you execute only tagged scenarios in Cucumber?

Answer:
By specifying the tag in your runner file:

java
@CucumberOptions(tags = "@Smoke")

โœ… Intermediate-Level Questions


๐Ÿ”น Q5. Can you use multiple tags on a scenario?

Answer:
Yes. A scenario can have multiple tags:

gherkin
@Smoke @Regression @Login Scenario: Multiple tag example

๐Ÿ”น 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:

java
@CucumberOptions(tags = "not @WIP")

๐Ÿ”น 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:

java
@Before("@Regression") public void setupForRegression() { // Setup steps }

๐Ÿ”น 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 fileWritten in step definition Java class
Readable to business usersUsed for technical setup (e.g., DB, API, driver setup)
Runs before each scenarioCan 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.