What is Cucumber framework?
The Cucumber framework is a tool used for testing software using Behavior Driven Development (BDD). It allows the execution of automated acceptance tests written in a human-readable language.
What are the advantages of Cucumber framework?
- Supports BDD approach, which improves collaboration between developers, testers, and business stakeholders.
- Allows writing test cases in plain English using Gherkin syntax.
- Supports multiple programming languages like Java, Ruby, .NET, etc.
- Improves test case readability and maintainability.
- Encourages reusability of code through step definitions.
- Easy to set up and integrate with tools like Selenium and JUnit/TestNG.
How to run multiple feature files in Cucumber?
package org.softpost;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"classpath:multicolumn.feature", "classpath:outline.feature"},
glue = "org.softpost",
plugin = {"html:target/cucumber-reports"}
)
public class MultipleFeatureTest {
}
How to generate Cucumber reports in Eclipse?
Use the plugin option in @CucumberOptions to generate reports. Example:
package runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/functionalTests",
glue = {"stepDefinitions"},
plugin = {"pretty", "html:target/cucumber-reports"},
monochrome = true
)
public class TestRunner {
}
List various keywords used in Cucumber to write a scenario.
The keywords used are: Given, When, Then, And, But, and Background.
What is a feature file in Cucumber and how to create one?
A feature file contains scenarios written in Gherkin syntax. Follow the below steps:
- Create a file with .feature extension.
- Write scenarios using Gherkin keywords.
What is BDD in Cucumber?
BDD (Behavior Driven Development) is a software development approach that allows test cases to be written in plain English using Gherkin syntax, making them understandable to all stakeholders.
What are the two files required to execute a Cucumber test scenario?
- Feature file
- Step definition file
Explain step definitions and how to write them in Cucumber.
Step definitions contain the actual automation code for steps written in the feature file. Example in Java:
@Given("^I am on www.yahoo.com$")
public void i_am_on_yahoo() {
driver.get("https://www.yahoo.com");
}
How to run Cucumber tests in parallel?
Use the cucumber-jvm-parallel-plugin or a test runner like TestNG to parallelize execution of feature files or scenarios.
How to use tags in Cucumber?
Tags can be used to group and execute specific scenarios. Example:
@RegressionTest
Scenario: Login should succeed with valid credentials
...
Run with:
@CucumberOptions(tags = "@RegressionTest")
What are hooks in Cucumber?
Hooks are blocks of code that run before or after each scenario. They are defined using @Before and @After annotations in step definition files.
Explain types of hooks in Cucumber.
- Before - Runs before each scenario.
- After - Runs after each scenario.
What languages are supported by Cucumber JVM?
- Java
- Scala
- Groovy
- Clojure
- Kotlin
What is the use of dry-run in Cucumber?
The dryRun option is used to check whether every step in the feature file has a corresponding step definition. It doesn't run the tests but verifies step bindings.
What is Gherkin and how is it related to Cucumber?
Gherkin is the language used to write Cucumber feature files. It uses a simple, plain-text syntax structured around keywords like Given, When, Then, And, and But. Gherkin helps in defining test cases in a human-readable format, making collaboration easier between technical and non-technical team members.
How is Cucumber different from Selenium?
Selenium is a browser automation tool primarily used for UI testing. Cucumber is a BDD framework used to write automated acceptance tests in plain language. Cucumber can use Selenium WebDriver for interacting with the browser, but it provides an abstraction layer that focuses on describing behavior rather than browser interactions.
What is the purpose of the Background keyword in Cucumber?
The Background keyword in a Cucumber feature file allows you to define steps that are common to all scenarios in the file. These steps are executed before each scenario, avoiding repetition and improving readability.
Can we use Cucumber for API testing?
Yes, Cucumber can be used for API testing by integrating it with libraries like REST-assured or HttpClient. The API requests and assertions are written in step definitions, while feature files describe the test cases in Gherkin.
What is Scenario Outline in Cucumber?
Scenario Outline is used to run the same scenario multiple times with different sets of input data. It uses the Examples keyword to define a table of test data that drives multiple executions of the scenario.
How can you share state between steps in Cucumber?
You can share state between step definitions in Cucumber by using class-level variables or dependency injection frameworks like PicoContainer or Spring. This is useful for maintaining data across multiple steps within a scenario.
What is the use of glue in @CucumberOptions?
The glue parameter in @CucumberOptions specifies the package(s) where Cucumber should look for step definitions, hooks, and other related code.
How do you ignore a scenario in Cucumber?
To ignore a scenario in Cucumber, you can tag it with @Ignore and ensure that this tag is excluded in the runner configuration. Alternatively, you can comment out the scenario in the feature file.
What is the difference between Given, When, Then in Cucumber?
- Given: Precondition or setup step.
- When: Action or event to trigger a behavior.
- Then: Outcome or result verification.
Can Cucumber be integrated with TestNG or JUnit?
Yes, Cucumber can be integrated with both TestNG and JUnit. JUnit is commonly used with the @RunWith(Cucumber.class) annotation. For TestNG, you can use custom runner classes to support features like parallel test execution.