Creating a cucumber project in eclipse

     1 .       Create a maven project in eclipse using following link.


     2.       Add the below dependencies in your pom.xml.

<dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-picocontainer</artifactId>
 <version>1.1.5</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-junit</artifactId>
 <version>1.1.5</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
 </dependency>

 <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-server</artifactId>
   <version>2.41.0</version>
  </dependency>
 
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.39.0</version>
        </dependency>

      3.       The project structure would be as shown below.

 



     4.       Here in the project structure, we need to add the below things :
 
    a)      src/main/java – Write your java code.
    b)      src/main/resources – Place your resource file in this folder.
    c)      src/test/java – Place your test code in this folder.
    d)      src/main/resources – Place your test resources in this folder.

    5.       For a cucumber project you need
a)      Feature File
b)      Step Definitions
c)       Runner file

    6.       Now first create the feature file under src/main/resources folder.

     Steps to create feature file is as below:
a)      Right click on the src/main/resources folder and create a new file.
b)      Create a file with extension .feature. Name the file as HelloStepsDefs.feature

 


c)      Now write the Gherkin language code in the feature file.  


Feature: Hello World

  Scenario: Say hello
    Given I have a hello app with "Hello"
    When I ask it to say hi
    Then it should answer with "Hello World"

7.       Now run the feature file directly. Right click on the feature file and run as à cucumber feature. You will get an error as below.

 


    9.       Copy the above steps and to add the java code this can invoke the steps written in the feature file.

Steps to write the java code
a)      Right click on src/test/java and create a new class file. This class file will be step definition file.

package test.cucumber;

import junit.framework.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class HelloStepDefs {
     
     
      @Given("^I have a hello app with Hello$")
      public void I_have_a_hello_app_with_Hello() throws Throwable {
         System.out.println(" I have a app which says hello");
      }

      @When("^I ask it to say hi$")
      public void I_ask_it_to_say_hi() throws Throwable {
            System.out.println(" I say Hi");
      }

      @Then("^it should answer with Hello World$")
      public void it_should_answer_with_Hello_World() throws Throwable {
            System.out.println(" It answers with Hello World");
        
      }

}

9. Now try to run the feature file again. Run as -- > Cucumber Feature. It shows success this  time.

 

    10.       To create reports and run the test easily. Now create a runner file in src/main/java folder structure.
a)      Right click on src/main/java and create a new class file. This class file will be the runner file. Name the class as RunCukesTest
b)      Write the below code in the runner file.

package test.cucumber;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
// Associates Cucumber-JVM with the JUnit runner
@RunWith(Cucumber.class)
@CucumberOptions(format = {"html:target/cucumber-html-report", "json:target/cucumber-json-" +"report.json"})
public class RunCukesTest {
}
    11.       Now run the runner file as Run as -- > Junit Test case. You will see the below result.

 


1    12.       You will see results and reports in target folder of the project. You can find the HTML reports in the folder structure  of your project
   Eg : D:\Workspace\cucumber-jvm\target\cucumber-html-report\
  
    Open the index.html. It looks like report as shown below.