Cucumber
Course Index
Index

Cucumber Interview Questions

1. What is Cucumber?

Cucumber is a testing tool that supports Behavior-Driven Development (BDD). It allows writing test scenarios in plain language (Gherkin) that can be understood by both technical and non-technical team members. It bridges the communication gap between stakeholders by providing a common platform for collaboration.

2. What is Gherkin, and why is it used in Cucumber?

Gherkin is a plain-text language used to write test scenarios in Cucumber. It uses simple syntax with keywords like Given, When, Then, And, and But to describe behavior. Gherkin allows non-technical stakeholders to understand the tests.

3. What is a feature file in Cucumber?

A feature file is a file where all the scenarios for a particular feature are written in Gherkin syntax. It acts as documentation and a test script. Feature files have a .feature extension.

4. What is a step definition file in Cucumber?

Step definition files contain the actual code implementation for each step mentioned in the feature file. They connect the plain-text Gherkin steps to the programming code that executes the behavior.

5. What are the advantages of using Cucumber?

  • Encourages collaboration between technical and non-technical stakeholders.
  • Provides a clear understanding of test scenarios through plain language.
  • Bridges the gap between requirements and implementation.
  • Integrates well with various tools like Selenium for UI testing.

6. What are Given, When, Then, And, and But keywords in Cucumber?

  • Given: Describes the initial context or preconditions for a scenario.
  • When: Represents an action or event.
  • Then: Specifies the expected outcome or result.
  • And/But: Used to combine multiple steps for better readability.

7. What is the purpose of the @CucumberOptions annotation?

The @CucumberOptions annotation is used to configure various options for the Cucumber runner. Examples include:

  • features: Path to feature files.
  • glue: Path to step definition files.
  • plugin: Configure reporting options.
  • tags: Specify scenarios to execute.

8. What is a Scenario Outline in Cucumber? How is it different from a Scenario?

  • A Scenario describes a single example or test case.
  • A Scenario Outline allows parameterization, meaning you can run the same scenario multiple times with different sets of data using an Examples table.

9. How do you share data between steps in Cucumber?

You can use:

  • ScenarioContext or TestContext objects.
  • Dependency Injection with tools like PicoContainer or Spring.

10. What are hooks in Cucumber? What are they used for?

Hooks are special blocks of code executed before or after each scenario or step. Common hooks are:

  • @Before: Runs before each scenario.
  • @After: Runs after each scenario. Hooks are used for setup (e.g., launching a browser) and teardown (e.g., closing a browser).

11. How do you handle parameterization in Cucumber?

Use placeholders in step definitions and feature files. Example:

Gherkin
Copy
Given I login with username "user" and password "pass"

Step definition:
Java
Copy
@Given("I login with username {string} and password {string}")
public void login(String username, String password) {
    // Use parameters in the code
}

12. What is the role of a test runner in Cucumber?

The test runner triggers the execution of Cucumber tests. It:

  • Specifies the location of feature files and step definitions.
  • Configures reporting and other execution settings.
  • Maps the glue code to the feature files.

13. How do you tag scenarios in Cucumber? How are they used?

Tags are added to scenarios or feature files to group or filter tests during execution.
Example:

Gherkin
Copy
@SmokeTest
Scenario: Verify login functionality

Use the @CucumberOptions annotation to specify tags for execution:
Java
Copy
@CucumberOptions(tags = "@SmokeTest")

14. How can you integrate Cucumber with Selenium?

  • Write step definitions to include Selenium WebDriver actions (e.g., opening a browser, interacting with elements).
  • Configure Selenium WebDriver in hooks (e.g., launch browser in @Before and close browser in @After).

Example:
Java
Copy
@Given("I open the application")
public void openApplication() {
    WebDriver driver = new ChromeDriver();
    driver.get("http://example.com");
}

15. How do you generate reports in Cucumber?

Use the plugin option in @CucumberOptions to generate reports. Examples include:

  • pretty: Prints readable logs in the console.
  • html: Generates an HTML report.
  • json: Creates a JSON report for further processing with tools like Extent Reports.

16. What is a Background in Cucumber?

The Background keyword is used to define common steps that are executed before each scenario in a feature file.
Example:

Gherkin
Copy
Background:
  Given I am logged in

Scenario: Add product to cart
  When I add a product
  Then I see the product in the cart

17. How do you handle parallel execution in Cucumber?

  • Use Cucumber plugins like cucumber-jvm-parallel-plugin to generate separate runners for parallel execution.
  • Ensure thread-safety for shared resources (e.g., WebDriver).

18. How would you handle dynamic data in Cucumber tests?

  • Use placeholders or parameters in feature files.
  • Use data tables or external files (like CSV/Excel) to manage dynamic data.

19. How do you debug failing scenarios in Cucumber?

  • Use Cucumber’s detailed logs.
  • Add breakpoints in the step definitions and use a debugger.
  • Capture screenshots or logs during failure using hooks.