Writing First Feature
If you are following the lessons correctly, you have almost completed 80% of the course. With whatever we learned so far, let's put everything together in practice.
Create a new feature file called login.feature and login.java as below and copy and paste the code below.
Let's keep the folder structure as follows.
- src/test/java/org/example/features/login.feature
- src/test/java/org/example/features/stepDefinitions/login.java
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully
@Given("the user is on the login page") public void navigateToLoginPage() { // Code to navigate to the login page System.out.println("User is on the login page"); } @When("the user enters valid credentials") public void enterValidCredentials() { // Code to enter valid login credentials System.out.println("User enters valid credentials"); } @Then("the user should be logged in successfully") public void verifySuccessfulLogin() { // Code to verify successful login System.out.println("User is logged in successfully"); }
Now right click on the login.feature file and click on run from IDE. You should see all the functions in the step definitions are executed as below.
User is on the login page
User enters valid credentials
User is logged in successfully
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.441s
Let's add one more scenario to the above feature file. We re now adding scenario for invalid login attempt. The updated feature file will look as below.
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully Scenario: Invalid login attempts Given the user is on the login page When the user enters incorrect credentials Then the login should fail with an error message
Since we have added new scenario to the feature, we need to update the step definition file to accommodate the changes. Here we have Given step is same as previous scenario, but other two are different. Hence we will add two more functions to our step definition file.
The updated step definition file will be as follows.
@Given("the user is on the login page") public void navigateToLoginPage() { // Code to navigate to the login page System.out.println("User is on the login page"); } @When("the user enters valid credentials") public void enterValidCredentials() { // Code to enter valid login credentials System.out.println("User enters valid credentials"); } @Then("the user should be logged in successfully") public void verifySuccessfulLogin() { // Code to verify successful login System.out.println("User is logged in successfully"); } @When("the user enters incorrect credentials") public void enterIncorrectCredentials() { // Code to enter incorrect login credentials System.out.println("User enters incorrect credentials"); } @Then("the login should fail with an error message") public void verifyLoginFailure() { // Code to verify login failure with an error message System.out.println("Login failed with an error message"); }
Now right click on the login.feature file and click on run from IDE. You should see all the functions in the step definitions are executed as below.
User is on the login page
User enters valid credentials
User is logged in successfully
User is on the login page
User enters incorrect credentials
Login failed with an error message
2 Scenarios (2 passed)
6 Steps (6 passed)
0m0.403s
Now we have learnt how to create a feature file and step definition file. Also how to add multiple scenario in a feature. In the future chapters we will learn how to parameterize and reuse the steps.