How to install cucumber java?
Prerequisite for installing Cucumber
1.
jdk (Latest jdk recomanded).
3.Eclipse plugins
i. TestNG plugin - http://beust.com/eclipse
ii. Cucumber eclipse plugin - http://cucumber.github.com/cucumber-eclipse/update-site
You need to download few cucumber related jars.
1. cucumber-core-1.2.5
2. cucumber-html-0.2.3
3. cucumber-java-1.2.5
4. cucumber-jvm-deps-1.0.5
5. cucumber-testng-1.2.5
6. gherkin-2.12.2
7. selenium-server-standalone-3.4.0
8. testng-6.11
To configure cucumber java you need to follow few steps
Step 1 : Open eclipse and configure workspace
Step 2 : Create a new project in eclipse
You can create an new project from File > New >Java Project . You need to provide a name for that project , say "TestCucumber" and tap on Finish button. You will find "TestCucumber" is created in left side panel of eclipse IDE.
Step 3 : Configure java build path from eclipse .
You need to configure java build path . Means you need to add TestNG library and required external jars with this new project.
a. Left click on the project.
b. Click on the Build Path > Configure Build Path ..
c. Click on Add library and and TestNG and Click Next > Finish . TestNG will be added for this project.
d. Click on Add External JARs,, and navigate to downloaded location where there cucumber related jars are there.
e. Click on Open the Click on Ok button .. All required jars will be added with the project.
Step 4: Create 3 package in side the TestCucumber/src
a. com.Cucumber.features : This package consist all feature files.
b com.Cucumber.steps : Actual java program where selenium steps are written.
c. com.Cucumber.Runners : This file consist configuration for the test.
There I am creating a sample Cucumber project
Sample feature file
Feature: Login Application
As a user
I want to login to the application
Scenario: Valid user name and password
Given I launch the url "http://www.fb.com"
When I enter password and username
And I click on login button
Then I should see the Error message
You need to create a file under com.Cucumber.features package . File extension should be .feature
Name of the sample feature file is
demo.feature
Now you need to create actual selenium file under com.Cucumber.steps package.
Sample step java file
package com.Cucumber.steps;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class DemoLoginSteps {
WebDriver driver;
@Given("^I launch the url \"([^\"]*)\"$")
public void i_launch_the_url(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.fb.com");
}
@When("^I enter password and username$")
public void i_enter_password_and_username() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.name("email")).sendKeys("test");
driver.findElement(By.name("pass")).sendKeys("cucumber");
}
@When("^I click on login button$")
public void i_click_on_login_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.xpath("//*[@data-testid='royal_login_button']")).click();
}
@Then("^I should see the login page$")
public void i_should_see_the_Error_message() throws Throwable {
try{
// Write code here that turns the phrase above into concrete actions
if(driver.findElement(By.name("email")).isDisplayed()){
Assert.assertTrue(true);
}else
Assert.assertTrue(false);
}catch(Exception NoSuchElementException){
Assert.assertTrue(false);
}finally{
driver.quit();
}
}
}
Name of the step file is
DemoLoginSteps
You can generate step code automatically, but you need to clear few lines of code like " throw new PendingException(); "from each methods and you need to write down the actual selenium steps as per your requirements. Auto generated file is nothing but a skeleton of the methods.
How to generate step file automatically
You just need to left click on the feature file and click on the Run As Cucumber feature . You will file unimplemented methods are displayed on eclipse console screen.
Create a RunTest Class under om.Cucumber.Runners package (You need to create om.Cucumber.Runners package under src folder)
Sample runner class
CucumberOptions(
features = "src/com/Cucumber/features/Login.feature",
glue = "com.Cucumber.steps",
plugin = { "pretty", "html:target/cucumber-report" },
monochrome = true)
public class RunTest extends AbstractTestNGCucumberTests {
}
Name of runner class is
RunTest
feature : Location of feature file in your project
glue : package name for the step java file
plugin : Reporting purpose
monochrome :This option can either set as true or false. If it is set as true, it means that the console output for the Cucumber test are much more readable. And if it is set as false, then the console output is not as readable as it should be. For practice just add the code ‘monochrome = true‘ in TestRunner class.
Create a testng file under project to run the RunTest file.
Testng XML
You can run the test case without writing the xml file but testng xml will help you to create CI integration very easily.
How to run test case without testng xml
You need to run left click on the feature file and choose Run As Cucumber Feature.
How to run test case using testng xml
You just need to left click on the testng xml file and choose Run As TestNG suite.