Monday 23 April 2018

Cucumber: hello World application in Eclipse

In my previous post, I explained how to setup cucumber in command line mode, in this post I am going to show you how to execute a cucumber application in eclipse.

Below step-by-step procedure explain complete working example.

Step 1: Open Eclipse. Create new maven project ‘cucumber_tutorial’.

Right click on project explorer -> New -> Other


Select ‘Maven Project’ and press Next.

Select the check box 'Create a simple project (skip archetype selection)'.

Press Next.

Update Group Id and Artifact Id as ‘cucumber_tutorial’.

Press Finish button.

Total project structure looks like below.

Step 2: Update pom.xml for maven dependencies. I am using below maven dependencies.
 <dependencies>
  <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
  <dependency>
   <groupId>io.cucumber</groupId>
   <artifactId>cucumber-core</artifactId>
   <version>2.3.1</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
  <dependency>
   <groupId>io.cucumber</groupId>
   <artifactId>cucumber-java</artifactId>
   <version>2.3.1</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
  <dependency>
   <groupId>io.cucumber</groupId>
   <artifactId>cucumber-jvm-deps</artifactId>
   <version>1.0.6</version>
   <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
  <dependency>
   <groupId>io.cucumber</groupId>
   <artifactId>gherkin</artifactId>
   <version>5.0.0</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/junit/junit -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
  <dependency>
   <groupId>io.cucumber</groupId>
   <artifactId>cucumber-junit</artifactId>
   <version>2.3.1</version>
   <scope>test</scope>
  </dependency>


 </dependencies>

I faced some proxy problem while downloading these libraries. I downloaded them manually and added to my project explicitly.

Step 3: Create new class ‘LoginUtil.java’ under the package ‘com.sample.util’.

LoginUtil.java
package com.sample.util;

public class LoginUtil {

 /**
  * 
  * @param userName
  * @param password
  * @return true if the login successful, else false
  */
 public static boolean isLoginSuccessful(String userName, String password) {

  if (userName == null || userName.isEmpty() || password == null || password.isEmpty()) {
   return false;
  }

  if ("krishna".equals(userName) && "password123".equals(password)) {
   return true;
  }

  return false;
 }
}

Create a package ‘com.sample.features’ and define the file Login.feature.

Login.feature
Feature: Login

 Login should be quick and friendly.
 
 Scenario: Successful Login
  Users should be logged in successfully by providing correct username and password.
  
  Given I have chosen to Login
  When I log in with user name "krishna" and password "password123"
  Then I should be logged in successfully
  And I should see a personalized greeting message

Create a class ‘LoginTest.java’ in the package ‘com.sample.test’.

LoginTest.java
package com.sample.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.sample.util.LoginUtil;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class LoginTest {
 private String userName;

 @Given("^I have chosen to Login$")
 public void iHaveChosenToLogin() throws Exception {
  System.out.println("Opening login page");
 }

 @When("^I log in with user name \"([^\"]*)\" and password \"([^\"]*)\"$")
 public void iLogInWithCorrectAnd(String userName, String password) throws Exception {

  if (LoginUtil.isLoginSuccessful(userName, password)) {
   this.userName = userName;
   System.out.println("Login successful");
   return;
  }
  assertFalse("Wrong Credentials. userName " + userName, true);
 }

 @Then("^I should be logged in successfully$")
 public void iShouldBeLoggedInSuccessfully() throws Exception {
  System.out.println("Logged in Successfully");
 }

 @Then("^I should see a personalized greeting message$")
 public void iShouldSeeAPersonalizedGreetingMessage() throws Exception {
  System.out.println("Welcome " + userName);
  assertTrue(true);
 }
}

Define the package 'com.sample.main' and define class TestRun.java.

TestRun.java
package com.sample.main;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, features = "src/test/java/com/sample/features", glue = "com.sample.test", plugin = {
  "pretty" })
public class TestRun {

}


Run the application as junit test, you can able to see below messages in console.
Feature: Login
 Login should be quick and friendly.

  Scenario: Successful Login                                          # src/test/java/com/sample/features/Login.feature:5
  Users should be logged in successfully by providing correct username and password.
Opening login page
    Given I have chosen to Login                                      # LoginTest.iHaveChosenToLogin()
Login successful
    When I log in with user name "krishna" and password "password123" # LoginTest.iLogInWithCorrectAnd(String,String)
Logged in Successfully
    Then I should be logged in successfully                           # LoginTest.iShouldBeLoggedInSuccessfully()
Welcome krishna
    And I should see a personalized greeting message                  # LoginTest.iShouldSeeAPersonalizedGreetingMessage()

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.262s



Previous                                                 Next                                                 Home

2 comments:

  1. not working dude. Please check steps. It will be nice if you can do it again and provide video link. I tried with FULL internet access and it is not downloading required dependencies. Some issue is there. Please fix and try to repost

    ReplyDelete
    Replies
    1. Hi Sagar, I too faced dependencies downloading problem while trying, thats why i said, 'I faced some proxy problem while downloading these libraries. I downloaded them manually and added to my project explicitly.' in step 2. Check the image just before step-3 and download them explicitly and try

      Delete