Cucumber – Java Testing

To run Cucumber test with Java, following are the steps.

Step 1 βˆ’ Install Java βˆ’

Step 2 βˆ’ Install Eclipse IDE βˆ’

Step 3 βˆ’ Install Maven βˆ’

  • Edit Path variable and include Maven.
  • Download MAVEN plugin from Eclipse
    • Open Eclipse.
    • Got to Help β†’ Eclipse Marketplace β†’ Search maven β†’ Maven Integration for Eclipse β†’INSTALL

Step 4 βˆ’ Configure Cucumber with Maven.

  • Create a Maven project.
    • Go to File β†’ New β†’ Others β†’ Maven β†’ Maven Project β†’ Next.
    • Provide group Id (group Id will identify your project uniquely across all projects).
    • Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
    • Click on Finish.

Step 5 βˆ’ Open pom.xml βˆ’

  • Go to the package explorer on the left hand side of Eclipse.
  • Expand the project CucumberTest.
  • Locate pom.xml file.
  • Right-click and select the option, Open with β€œText Editor”.

Step 6 βˆ’ Add dependency for Selenium βˆ’ This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.

  • Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
  • Inside the dependencies tag, create dependency tag. (<dependency></dependency>)
  • Provide the following information within the dependency tag.
<dependency> 
   <groupId>org.seleniumhq.selenium</groupId> 
   <artifactId>selenium-java</artifactId> 
   <version>2.47.1</version> 
</dependency>

Step 7 βˆ’ Add dependency for Cucumber-Java βˆ’ This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.
  • Provide following information within the dependency tag.
<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-java</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>

Step 8 βˆ’ Add dependency for Cucumber-JUnit βˆ’ This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.
  • Provide the following information within the dependency tag.
<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-junit</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>

Step 9βˆ’ Add dependency for JUnit βˆ’ This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.
  • Provide the following information within the dependency tag.
<dependency> 
   <groupId>junit</groupId> 
   <artifactId>junit</artifactId> 
   <version>4.10</version> 
   <scope>test</scope> 
</dependency>

Step 10 βˆ’ Verify binaries.

  • Once pom.xml is edited successfully, save it.
  • Go to Project β†’ Clean βˆ’ It will take a few minutes.
  • You will be able to see a Maven repository.

Step 11 βˆ’ Create a package under src/test/java named as cucumberJava.

Step 12 βˆ’ Create feature file

  • Select and right-click on the package outline.
  • Click on β€˜New’ file.
  • Give the file a name such as cucumberJava.feature.
  • Write the following text within the file and save it.Feature: CucumberJavaScenario: Login functionality existsGiven I have open the browserWhen I open Facebook websiteThen Login button should exits

Step 13 βˆ’ Create step definition file βˆ’

  • Select and right-click on the package outline.
  • Click on β€˜New’ file.
  • Give the file name a name such as annotation.java.
  • Write the following text within the file and save it.
package CucumberJava; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 

public class cucumberJava { 
   WebDriver driver = null; 
	
   @Given("^I have open the browser$") 
   public void openBrowser() { 
      driver = new FirefoxDriver(); 
   } 
	
   @When("^I open Facebook website$") 
   public void goToFacebook() { 
      driver.navigate().to("https://www.facebook.com/"); 
   } 
	
   @Then("^Login button should exits$") 
   public void loginButton() { 
      if(driver.findElement(By.id("u_0_v")).isEnabled()) { 
         System.out.println("Test 1 Pass"); 
      } else { 
         System.out.println("Test 1 Fail"); 
      } 
      driver.close(); 
   } 
}

Step 14 βˆ’ Create a runner class file.

  • Select and right-click on the package outline.
  • Click on β€˜New’ file.
  • Give the file name as runTest.java.
  • Write the following text within the file and save it.
package cucumberJava;
 
import org.junit.runner.RunWith; 
import cucumber.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(format = {"pretty", "html:target/cucumber"}) 

public class runTest { }

Step 15 βˆ’ Run the test using option βˆ’

  • Select runTest.java file from the package explorer.
  • Right-click and select the option, Run as.
  • Select JUnit test.

You will observe the following things upon execution βˆ’

  • An instance of Firefox web browser will open.
  • It will open the Facebook login page on the browser.
  • It will detect the login button.
  • The browser will close.
  • In the JUnit window, you will see a scenario with green tick mark, which indicates success of the test execution.

This Post Has One Comment

  1. superslot

    Thanks for the article.Thanks Again. Awesome.

Leave a Reply