In this blog we will explore how to set up IntelliJ to create a Maven project for using Cucumber with Selenium WebDriver Java

Pre-requisites

JDK Install

Install Java Development Kit from

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Add a Windows environment variable (system properties, WinKey + Pause -> Advanced System Settings -> Environment Variables) JAVA_HOME to point at the JDK directory.

Maven Install

Download Maven from https://maven.apache.org/download.cgi and extract the zip to a location of your choice.

Add the path to the \bin folder of the Maven install to the Windows PATH environment variable (system properties, WinKey + Pause -> Advanced System Settings -> Environment Variables)

Open a new command prompt (Winkey + R then type cmd) and run mvn -v to verify the installation.

Install IntelliJ

Install from: https://www.jetbrains.com/idea/download/#section=windows I’m using the community edition in this blog

 

IntelliJ Configuration

JDK Path

As Maven uses the JDK (not JRE) we will add the path to the JDK in IntelliJ. Open IntelliJ, and use

File->Project Structure… Click on SDKs on left, click the ‘+’ add button and add the path to your JDK install from earlier.

Cucumber & Gherkin Plugins

IntelliJ, File->Settings and click on Plugins

Now click the Browse Repositories button and search for Cucumber. Install the plugin, then re-start IntelliJ

 

Project Setup

In IntelliJ, choose File->New Project. Choose Maven as the type. leave ‘Create from Archetype’ unchecked, and click Next

complete group id (e.g. com.edgewords.maven), the artefact id (whatever you want to call the project) and click Next

Choose a location to save the project and finish.

Expand the project structure, and under src/test/java we will create some packages to store our code bindings and runners.

Right-click on the java folder under test, and add new package. Call it bindings

Same again, another package called runners

Right-click on the test folder, and add new directory, call it resources.

Right-click on the new resources folder and choose Mark Directory as->Test Resources Root

You should now have:

 

POM File

We now need to specify all the project dependencies using the Maven POM.

Open the POM.xml file and add all of the code as shown below:

 



    4.0.0

    com.edgewords
    tests
    1.0-SNAPSHOT

    
        UTF-8
    

    
        
            junit
            junit
            4.12
            test
        

        
            org.seleniumhq.selenium
            selenium-java
            3.11.0
            test
        

        
            info.cukes
            cucumber-junit
            1.2.5
            test
        

        
            info.cukes
            cucumber-java
            1.2.5
            test
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
            

            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.21.0
                
                    
                        MyRunner.java
                    
                
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-surefire-report-plugin
                2.21.0
            
        
    


 

Obviously keep your project definition at the top as it was, it is only from the <properties> section onwards that you need to add.

 

Now on the right-hand side of the IntelliJ IDE, click Maven Projects tab to show Maven lifecycle. Now click re-import Maven Projects button to update from the POM.

 

Feature File

Right-click on our test resources folder and choose New->File. Call it basket.feature

Write your feature, example:

@run
Feature: basket
  #shopping basket features

  Scenario: add to basket
  Given that i am on the shopping website
  When i add an item to the basket
  Then i can view the item in my basket

 

Execute the Feature

Now right-click on the feature and run the feature

The Feature has no code bindings so in the Run pane at the bottom of IntelliJ, it will suggest some boiler-plate code bindings for you. Select these and copy to the clipboard.

Right-click on our bindings package in the Project Explorer and choose New->Java Class.

Paste in your boiler-plate code.

Now we need our imports to fix any errors. You can click in a line of code error and use Alt-Enter to fix imports, or here are the imports we will be using for this feature:

import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue;

 

We now need to write our WebDriver code as well. Here is the completed code for you:

package bindings;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue;

public class Basket {
    WebDriver driver;

    @Given("^that i am on the shopping website$")
    public void that_i_am_on_the_shopping_website() throws Throwable {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://www.edgewordstraining.co.uk/demo-site/");
        driver.manage().window().maximize();
    }

    @When("^i add an item to the basket$")
    public void i_add_an_item_to_the_basket() throws Throwable {
        driver.findElement(By.cssSelector("#masthead [type='search']")).click();
        driver.findElement(By.cssSelector("#masthead [type='search']")).sendKeys("cap");
        driver.findElement(By.cssSelector("#masthead [type='search']")).sendKeys(Keys.ENTER);
        driver.findElement(By.cssSelector(".entry-summary button")).click();
    }

    @Then("^i can view the item in my basket$")
    public void i_can_view_the_item_in_my_basket() throws Throwable {
        driver.findElement(By.linkText("Cart")).click();
        assertTrue(driver.findElement(By.linkText("Cap")).isDisplayed());
        driver.findElement(By.linkText("×")).click();
        // explicit wait for item to be removed as may use AJAX
        WebDriverWait wait=new WebDriverWait(driver, 20); //explicit wait if we want to use
        WebElement returnToShopLink;
        returnToShopLink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Return to shop")));
        returnToShopLink.click();
        //driver.findElement(By.linkText("Return to shop")).click();
        driver.quit();
    }

}

 

We now need to tell IntelliJ where the feature files & code bindings are before we can execute Feature files. In IntelliJ menu, Run->Edit Configurations

Click on the feature name on the left and provide the Glue (package that the code bindings will reside) and the Feature or Folder Path (where are Features files reside)

Now we can execute the Feature again, and this time it should open a Web Browser and perform the test!!

 

Run from Maven

Now we have a working example, let’s execute using Maven. To do this we need a runner script.

Right-click on the runners package and New->Java Class, call it MyRunner and add the following code:

package runners;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"html:reports/cucumber-html-report",
"json:reports/cucumber.json",
"pretty"},
tags = {"@runit,@run","~@ignore"},
features = {"src/test/resources"},
glue = {"bindings"}
)

public class MyRunner {

}

 

In the Maven POM.xml, we specified that we want to execute this class:


    org.apache.maven.plugins
    maven-surefire-plugin
    2.21.0
    
        
            MyRunner.java
        
    

 

So now we can use Maven for execution. Open the Maven panel on the right-hand side in IntelliJ and double-click the test phase in the lifecycle. It should run.

In the bottom of IntelliJ you can also open the Terminal window and type mvn test and hit enter this should also run!

 

Results

We have created several reports with the code in this blog. We have used the Surefire reporting plugin (specified in the POM.xml), if you look in your project explorer, target/surefire-reports you will see them in there.

We have also created some reports using our Cucumber Runner Class. You will find these using Windows explorer under the project there will be a reports folder with html and json cucumber reports.

 

Training

Edgewords provides classroom, online and on-site training in BDD Cucumber (and SpecFlow) as well as Selenium WebDriver (Java & C#), take a look at our course schedules or phone for a quote.