To run Cucumber test with Java, following are the steps.
Step 1 β Install Java β
- Download jdk and jre fromhttp://www.oracle.com/technetwork/java/javase/downloads/index.html
- Accept license agreement.
- Install JDK and JRE.
- Set environment variable as shown in the following picture.
Step 2 β Install Eclipse IDE β
- Make sure JAVA is installed on your machine.
- Download Eclipse from https://eclipse.org/downloads/
- Unzip and Eclipse installed.
Step 3 β Install Maven β
- Download Maven βhttps://maven.apache.org/download.cgi
- Unzip the file and remember the location.
- Create environment variable MAVEN_HOME as shown in the following image.
- 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.
Thanks for the article.Thanks Again. Awesome.