You are on page 1of 5

# Cucumber Interview Ques & Ans

Ques 1) Cucumber
Cucumber is a tool based on BDD(Behavioural Driven Development) Methodology.

Ques 2) Feature file


Feature file provide high level of description of Application Under Test(AUT)
first line of feature file starts with Feature
Keyword for writing scenario
Feature: Login feature

Scenario: Validating login feature


Given
When
Then
In case of (Parameterization)
Scenario Outline
Given
When
Then
Examples:
| UserName | Password | Location |

@RegTest @MobileTest
Scenario: Validate App Login Functionality
Given User is on login screen
When User enters username Max and Password Payne
Then User redirected to app dashboard

Ques 3) Purpose of scenario outline


Scenario outline is a way of parameterization of scenarios. This is ideally used when the same
scenario needs to be executed for multiple sets of data, however, the test steps remain the same.
Scenario Outline must be followed by the keyword ‘Examples’,
@MobileTest
Scenario Outline: Validate App Login Functionality
Given User is on login screen
When User username <name> and password <passwd> and address <addr>
Then User redirected to app dashboard

Examples:

| name | passwd | addr |


| Rob | van | Nyc |
| Dev | John | La |
Ques 4) Purpose of Step Definition
A step definition file in Cucumber is used to segregate the feature files from the underlying code.
Each step of the feature file can be mapped to a corresponding method on the Step Definition file
While feature files are written in an easily understandable language like, Gherkin, Step Definition
files are written in programming languages such as Java, .Net, Ruby, etc.

public class stepDef {

@Given("User is opening browser")


public void user_is_opening_browser() {
System.out.println("pre-requsite given");
}

@When("User entered url in browser")


public void user_entered_url_in_browser() {
System.out.println("pre-requsite when");
}

@Then("User is landed on website home page")


public void user_is_landed_on_website_home_page() {
System.out.println("pre-requsite Then");

Ques 5) Purpose of Test Runner file


TestRunner class is used to provide the link between the feature file and the step definition file.
A TestRunner class is generally an empty class with no class definition.

package Runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions( features ="src/test/java/feature",
glue={"stepDefinitions"},strict = true, monochrome = true,
tags= {"@MobileTest or @RegTest"}
// plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json"}
)
public class testRunner {
}
Ques 6) Difference between Background & Hooks

S.N Background Hooks


1 It is used to run as pre-requisite for all the scenarios It is also used to run as pre-requisite for all scenarios
2 It is used to run all the tags without excluding any of It is used to run only selected or mentioned tags in
the tags (@Mobile @Web @Reg @Sanity) test runner(@Mobile @Reg @Sanity)
3 It is created in feature file itself, No need of separate Separate Java class has to be created in case of
java class in this Hooks
4 Background: @Before("@WebTest")
Given User is opening browser public void WebBrowserOpen()
When User entered url in browser {
Then User is landed on website home page System.out.println("Pre requsite
Before WebBrowserOpen case");
@RegTest @MobileTest }
Scenario: Validate App Login Functionality
Given User is on login screen @After("@WebTest")
When User enters username Max and Password public void WebBrowserClosed()
Payne {
Then User redirected to app dashboard System.out.println("Pre requsite
After WebBrowserClosed case");
}

Ques 7) Purpose of keywords for writing the scenario in cucumber


• “Given” keyword is used to specify a precondition for the scenario.
• “When” keyword is used to specify an operation to be performed.
• “Then” keyword is used to specify the expected result of a performed action.
• “And” keyword is used to join one or more statements together into a single statement.

Ques 8) Starting point of execution of feature file


TestRunner is the starting point of execution

Ques 9) Should any be code written in Test Runner class


No code should be written under the TestRunner class. It should include the tags @RunWith and
@CucumberOptions.

Ques 10) Use of feature property in Cucumber option tag


Features property is used to let the Cucumber framework identify the location of the feature files.

Ques 11) Use of glue property under Cucumber option tag


Glue property is used to let the Cucumber framework identify the location of step definition files.
Ques 12) Passing data using Data table
1) Feature File
Feature: Login Functionality

@MobileTest
Scenario: Validate App Login Functionality
Given User is on login screen
When User enters following details
| Serious | Sam | sam@gmail.com | Nyc |
Then User redirected to app dashboard

2)Step Definition
@Given("User is on login screen")
public void user_is_on_login_screen() {
System.out.println("Login Screen");
}

When("^User enters following details$")


public void user_enters_following_details(DataTable data) throws Throwable {
List<List<String>> obj = data.asLists();
System.out.println(obj.get(0).get(0));
System.out.println(obj.get(0).get(1));
System.out.println(obj.get(0).get(2));
System.out.println(obj.get(0).get(3));
}

@Then("User redirected to app dashboard")


public void user_redirected_to_app_dashboard() {
System.out.println("App Dashboard");
}
3)TestRunner
package Runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
features ="src/test/java/feature",
glue={"stepDefinitions"},strict = true, monochrome = true,
tags= {"@MobileTest or @RegTest"}
// plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json"}
)
public class testRunner {
}

You might also like