Before creating Selenium Maven testing Project for cucumber testing in Eclipse, first we must have the following dependencies in our computer system:

  • Java
  • Eclipse
  • Cucumber Eclipse Plugin
  • Web driver Java Client
  • Selenium

Here we are creating a Maven project to test the registration feature through the data table:

Step 1

In order to create a Maven project first, Go to File → New → Others → Maven → Maven Project → Next.

After clicking the Next, a window will prompt. In this window, we need to provide group Id (group Id identifies the project uniquely across all projects). We have provided group Id as "com.javatpoint." You can give any name on your own choice.

Also, provide artifact Id (artifact Id represents the name of the project). We have provided artifact Id as "CucumberTesting." You can give any name on your own choice.

Click on Finish.

Step 2

Open pom.xml by using the following steps:

  • Go to package explorer on the left side of the Eclipse window.
  • Expand the project which is created for data table testing in cucumber then select its pom.xml file.
  • Now open pom.xml and add the following dependencies.

Add dependency inside pom.xml for Selenium: This will indicate to Maven, which Selenium jar files will be downloaded from the central repository to the local repository.

  • In pom.xml file, create a dependencies tag (<dependencies></dependencies>), inside the project tag.
  • Now, inside the dependencies tag, create a dependency tag (<dependency></dependency>), and provide the following information within it.
  1. <dependencies>  
  2. <dependency>   
  3.    <groupId> org.seleniumhq.selenium </groupId>  
  4.    <artifactId> selenium-java </artifactId>   
  5.    <version> 2.47.1 </version>   
  6. </dependency>  
  7. </dependencies>     

Step 3

Add dependency inside pom.xml for Cucumber-Java: It will indicate to Maven; which Cucumber files will be downloaded from the central repository to the local repository.

  • Now, inside the dependencies tag (<dependencies></dependencies>), create a dependency tag (<dependency></dependency>), and provide the following information within it.
  1. <dependencies>  
  2. <dependency>   
  3.  <groupId> info.cukes </groupId>  
  4.  <artifactId> cucumber-java </artifactId>   
  5.  <version> 1.0.2 </version>  
  6.  <scope> test </scope>   
  7. </dependency>  
  8. </dependencies>  

Step 4

Add dependency for Cucumber-Junit: It will indicate to Maven, which Cucumber JUnit files will be downloaded from the central repository to the local repository.

  • Now, inside the dependencies tag (<dependencies></dependencies>), create a dependency tag (<dependency></dependency>), and provide the following information within it.
  1. <dependencies>  
  2. <dependency>   
  3. <groupId> info.cukes </groupId>  
  4. <artifactId> cucumber-junit </artifactId>  
  5.  <version> 1.0.2 </version>     
  6.  <scope> test </scope>   
  7. </dependency>  
  8. </dependencies>  

Step 5

Add dependency for Junit: It will indicate to Maven, which JUnit files will be downloaded from the central repository to the local repository.

  • Now, inside the dependencies tag (<dependencies></dependencies>), create a dependency tag (<dependency></dependency>), and provide the following information within it.
  1. <dependencies>  
  2. <dependency>  
  3.  <groupId> junit </groupId>     
  4.  <artifactId> junit </artifactId>  
  5. <version> 4.10 </version>  
  6. <scope> test </scope> </dependency>  
  7. </dependencies>  

After completing all dependencies, verify binaries.

  • Once pom.xml is completed successfully, then save it.
  • Go to your Project → Clean - It can take a few minutes.

Now, create a package named dataTable under src/test/java folder of your project.

Step 6

Create a Feature file:

  • Inside the package dataTable, create a feature file, named dataTable.feature.
  • Inside the feature file, write the following text.
Feature - Data table
Verify that the new user registration is successful after passing correct inputs.
Scenario:
Given the user on the user registration page.
When user enter invalid data on the page
| Fields                 | Values              |
| First Name             | Preeti              |
| Last Name              | Sharma              |
| Email Address          | someone@gmail.com   |
| Re-enter Email Address | someone@gmail.com   |
| Password               |PASSWORD             |
| Birthdate              | 02                  |
Then the user registration should be successful.
  • Save this file.

Step 7

Creation of the step definition file:

  • Create the step definition file inside the package dataTable with extension ".java" and named as 'dataTable.java.'
  • Inside the step definition file, write the following code.
  1. package dataTable;   
  2. import java.util.List;  
  3. import org.openqa.selenium.By;   
  4. import org.openqa.selenium.WebDriver;   
  5. import org.openqa.selenium.WebElement;   
  6. import org.openqa.selenium.firefox.FirefoxDriver;   
  7. import org.openqa.selenium.support.ui.Select;  
  8. import cucumber.annotation.en.Given;   
  9. import cucumber.annotation.en.Then;   
  10. import cucumber.annotation.en.When;   
  11. import cucumber.table.DataTable;  
  12. public class StepDefinition {   
  13.    WebDriver driver = null;  
  14.    @Given("^I am on user registration page$")   
  15.    public void goToFacebook() {   
  16.       //Intiate web browser instance. driver = new FirefoxDriver();  
  17.       driver.navigate().to("https://www.javaTpoint.com/");   
  18.    }   
  19.       
  20.    @When("^I enter valid data on the page$")   
  21.    public void enterData(DataTable table){   
  22.       //Initialize data table   
  23.       List<list> data = table.raw();  
  24.       System.out.println(data.get(1).get(1));   
  25.         
  26.       //Enter data  
  27.       driver.findElement(By.name("firstname")).sendKeys(data.get(1).get(1));  
  28.       driver.findElement(By.name("lastname")).sendKeys(data.get(2).get(1));  
  29.       driver.findElement(By.name("registered_email__")).sendKeys(data.get(3).get(1));       
  30.       driver.findElement(By.name("registered_email_confirmation__")).  
  31.          sendKeys(data.get(4).get(1));   
  32.       driver.findElement(By.name("registered_passwd__")).sendKeys(data.get(5).get(1));   
  33.         
  34.       Select dropdownB = new Select(driver.findElement(By.name("birth_day")));   
  35.       dropdownB.selectByValue("12");   
  36.           
  37.       Select dropdownM = new Select(driver.findElement(By.name("birth_month")));  
  38.       dropdownM.selectByValue("7");   
  39.           
  40.       Select dropdownY = new Select(driver.findElement(By.name("birth_year")));  
  41.       dropdownY.selectByValue("1992");   
  42.           
  43.       driver.findElement(By.className("_59mt")).click();   
  44.       // Click submit button driver.findElement(By.name("websubmit")).click();   
  45.    }   
  46.       
  47.    @Then("^User registration should be successful$")   
  48.    public void User_registration_should_be_successful() {  
  49.       if(driver.getCurrentUrl().equalsIgnoreCase("https://www.javaTpoint.com/")){  
  50.          System.out.println("Test Pass");   
  51.       } else {   
  52.          System.out.println("Test Failed");   
  53.       }   
  54.       driver.close();   
  55.    }   
  56. }  

Step 8

After creating the step definition file now, we need to create a runner class file.

  • Create a runner class inside the package dataTable with extension ".java" and named as RunTest.java.
  • Inside runner class RunTest.java, write the following code.
  1. package dataTable;   
  2. import org.junit.runner.RunWith;   
  3. import cucumber.junit.Cucumber;   
  4. @RunWith(Cucumber.class)   
  5. @Cucumber.Options(format = {"pretty""html:target/cucumber"})  
  6. public class RunTest { }  

Save this file, and run the test by using the following options:

  • Select the runner class i.e., RunTest.java file inside your package.
  • Right-click on it, and select the option, Run as → JUnit.

If your execution is successful, you will observe the following things:

  • JavaTpoint website gets loaded.
  • We will see the home page or the page provided by the respective website.
  • Data can be entered on the registration page.
  • Submit button will be clicked.