INTRODUCTION
We are learning test automation tool. How it helped to tester to test the application in simple way. Cucumber allows automation functional validation that is easily read and understood. Cucumber was initially implemented in Ruby and then extended to Java framework. Cucumber is a testing tool that supports Behavior Driven Development (BDD) framework.
What is CUCUMBER ?
Cucumber is a testing tool that supports Behavior Driven Development (BDD) framework. Cucumber was initially implemented in Ruby and then extended to Java framework. Both the tools support native JUnit. Behavior Driven Development gives us an opportunity to create test scripts from both the developer’s
Required Dependencies :
Maven Dependency:
Cucumber :
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Cucumber-Junit :
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Selenium :
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Cucumber Annotations :
- @Given
- @When
- @Then
- @RunWith
- @CucumberOptions
Cucumber Hooks :
Cucumber hooks are code that runs before or after each scenario. It helps us to follow code workflow and reduce code redundancy.
- @Before
Example :
@Before
public void userAuthentication(){
System.out.println("Starting the test");
}
- @After
Example :
@After
public void userAuthorisation() {
System.out.println("Starting the test");
}
Cucumber Framework for Selenium :
Cucumber BDD framework mainly consists of three major parts – Feature File, Step Definitions, and the Test Runner File.
1) Feature File :
A Cucumber feature file is a plain-text file used to describe the behavior of a software application in a behavior-driven development (BDD) approach. It define the application's features in a human-readable format. The feature file typically has a ".feature" extension.
Here is example of Feature file.
Feature: Login Functionality
As a user
I want to log in to my account
Need to enter the credentials to access the website.
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
And I click the login button
Then website dashboard will appear.
In above example, scenario consists of a series of steps written using Gherkin keywords (Given, When, Then, And) to describe the actions taken during the test.
2) Step Definition :
step definitions are provided to execute the actions described in the feature file. The step definitions contain the code that interacts with the application and performs the required actions to validate the scenarios. When you run the application , each feature file line is executes code inside the step definition. For step definition we use @Given , @When and @Then annotations to executed the code.
Code inside the step definition is executed with annotation added on the method. Inside annotation we have added the conditions of feature file.
Example :
public class PortalLoginStepDefination {
@Given("I am on the login page")
public void loginportal() {
}
@When("I enter valid credentials")
public void credential() {
}
@When("I click on login button")
public void clickButton() {
}
@Then("website dashboard will appear.")
public void dashBoardAppear() {
}
}
3) Runner file :
The test runner file is a Java class that specifies which feature files and step definition classes to execute during the test execution. Test runner file used annotation provided by cucumber. Inside annotation, Path of step-definition and feature file is added.
Here is example for runner file.
@RunWith(Cucumber.class)
@CucumberOption(features = "src/test/source/features" // provide the path of feature file
glue = "com.login.setpdefinition" // provide the path of stepdefinition package)
Public class Runner {
}
To run the tests, you simply execute the Runner.class as you would run any JUnit test class. Cucumber will then read the feature files, match the steps to their corresponding step definitions, and execute the scenarios accordingly.
SUMMARY :
This is how cucumber help to test the application in simple way. No need to have manual test scenario.
all the required scenario added in feature file and required java code is added in step-definition file.
0 Comments
Post a Comment