Enroll in Selenium Training

Till now we have understood what Cucumber is and what development model it follows. Now let's try to create first Cucumber Selenium Java test and I assume that you have some basic understanding of Selenium WebDriver and its basic commands.

I hope you have been following the complete tutorial and I expect that by now you have completed the following steps, which are the prerequisite for writing Cucumber Selenium test:

  1. Download & Install Java
  2. Download and Install Eclipse
  3. Install Cucumber Eclipse Plug-in
  4. Download Cucumber
  5. Download Selenium WebDriver Client
  6. Configure Eclipse with Selenium & Cucumber

Create Folder Structure

Before moving head for writing the first script, let's create a nice folder structure of the project.

  1. Create a new Package by right click on the 'src' folder and select New > Package.


  1. Name it as ‘cucumberTest’ and click on Finish button.


  1. Create another Package and name it as ‘stepDefinition’, by right click on the 'src' folder and select New > Package.

  2. Create a new Folder this time by right click on the project 'OnlineStore' and select New > Folder.


  1. Name it as ‘Feature’ and click on Finish button.


Selenium Java Test

Lets first write a simple Selenium Test script for LogIn functionality and then convert that script into Cucumber script to understand it better.

  1. Create a new Class file in the 'cucumberTest' package and name it as 'SeleniumTest', by right click on the Package and select New > Class. Check the option 'public static void main' and click on Finish button.

Now the Eclipse Window must look like this:


Selenium Test Script

Now write a simple script performing the following steps in Selenium.

  1. Launch the Browser
  2. Navigate to Home Page
  3. Click on the LogIn link
  4. Enter UserName and Password
  5. Click on Submit button
  6. Print a successful message
  7. LogOut from the application
  8. Print a successful message
  9. Close the Browser

Selenium Test Script

package cucumberTest;

import java.util.concurrent.TimeUnit;

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

public class SeleniumTest {
		private static WebDriver driver = null;
	public static void main(String[] args) {
		// Create a new instance of the Firefox driver

        driver = new FirefoxDriver();

        //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //Launch the Online Store Website


        // Find the element that's ID attribute is 'account'(My Account) 


        // Find the element that's ID attribute is 'log' (Username)

        // Enter Username on the element found by above desc.


        // Find the element that's ID attribute is 'pwd' (Password)

        // Enter Password on the element found by the above desc.


        // Now submit the form. WebDriver will find the form for us from the element 


        // Print a Log In message to the screen

        System.out.println("Login Successfully");

        // Find the element that's ID attribute is 'account_logout' (Log Out)

        driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();

        // Print a Log In message to the screen

        System.out.println("LogOut Successfully");

        // Close the driver




Note: If the Selenium version is less than 3.0, above test will work for you. If the version is above 3.0, in that case, please look at the chapter How to Use Gecko Driver in Selenium 3

Now, to start the test just select Run > Run As > Java Application Or Right Click on Eclipse code and Click Run As  > Java Application. After a few seconds, a Mozilla browser will open and you will see that with the help of your script, Selenium will Launch the Online Store demo application, perform Sign in.

The above Selenium test will be converted into a Cucumber Feature File in the next chapter.

Configure Eclipse with Cucumber
Configure Eclipse with Cucumber
Previous Article
Cucumber Feature File
Cucumber Feature File
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Virender Singh's Photo
Virender Singh

Similar Articles