Feature files

In this section we will discuss on Feature files. While started working with cucumber, the first thing  we will encounter s going to be the Feature file.  The feature file is more like a plain text file but with .feature extension. If you have basic knowledge on Specflow background you might have noticed that there will be an code behind file for every feature file along with step definition file something like the below flow diagram.

Feature files

But in cucumber we will not have any code behind file for the feature file rather the IDE like Eclipse or IntelliJ are more intelligent enough to map it.

But of you are going to run cucumber file out from an IDE, something like command like, then surely you need to use a different approach called testRunner with RunWith attribute(which will be discussed later in this course)

The Step definition files are yet another java class file which adds all the steps written with feature file.

Again, if you are from Specflow background, you might have noticed that each step definition fiels will be decorated with [Bindings] attribute, where in cucumber its not required

Feature files
Feature files
Feature: Login with Correct User name and Password
Scenario: Login Verification     
Given I go to the User Login Page     
When I enter the user name as Pratik and password as Pratik       And I click login button     
Then I should see the userform page

Explanation:

Feature: This line is basically used for writing business functionality 
Scenario: This line is basically used for writing the scenario to test.
Given: This line is basically used for writing the Pre condition for the testcase.
When: This line is basically used for writing the action we need to perform.
And: This line is basically used for writing the multiple  action we need to perform.
Then: This is basically used for writing the expected outcome or result.

Here we need to create the Test runner Package and then Runner.java class file under it.

Package TestRunner;
import org.junit.runner.Runwith;
import cucumber.api.CucmberOption;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOption(features = “Features”, glues ={StepDefinition})

public class Runner{

}

Code Explanation:

@RunWith: This indicates the Test Runner class to start executing the Test.
@CucumberOption: This annotation describes the some properties like Feature file, Step definition etc

Step Definition script:

public class Login{
	@Then(“^I should see the userform page”)
	Public void iShouldSeeTheUserformPage() throws Throwable{
		throw new PendingException();
}
@Given(“^I go to the User login Page”)
Public void iNavigateToTheLoginPage() throws Throwable{
		throw new PendingException();
}
@When(“^username Pratik and password Pratik ”)
Public void iEneterTheUserNameAsPratikAndPasswordAsPratik() throws Throwable{
		throw new PendingException();
}
@And(“^I click login button”)
Public void iClickLoginButton () throws Throwable{
		throw new PendingException();
}
}

In above example we have created the BDD test case for login application, For this we have define the Given statement as “I navigate to the login Page” so that this will be treated as pre condition. Once this is satisfied, test case will execute the when and then statement. Then statement will consider as expected output. In our scenario, the expected condition is “I should see the userform page”. So once all Give and When statement satishfied this wull check the Then Statement. If Then statement passed, then we will consider the test case as passed.

Leave a Reply

Your email address will not be published. Required fields are marked *