Selenium and Cucumber 2: Setting up Test Automation Project

By: Slav Kurochkin

In previous article we have written couple test cases using Gherkin syntax. Remember BDD it is software development methodology and not just a tool, even though we trying to understand technical aspect of test implementation with Selenium and Cucumber, ideally you would want to get entire team on board and follow the best practices of BDD development.

Now lets setup Selenium and Cucumber test automation project. You can use any Java IDE, I personally prefer Intellij IDEA, there is community edition available for free, and of cause if you want some advanced feature you can install licensed version.In our project we going to use Maven as project management tool. So start by creating new maven project. Once project is done expand test package and add few classes like on the screenshot bellow, we will discuss later what each of them does.

Selenium and Cucumber Test Automation

When your project setup is done you would need to add few dependencies (Cucumber, Selenium and JUnit) to pom.xml file, here is how it should look like:

Now lets modify our RunCukesTest class, in this class we telling JUnit that tests will run with Cucumber class, by using @RunWith(Cucumber.class) anotation, also we can specify @CucumberOptions, such as: output format, features path, where to store the tests which we wish to rerun etc.

Place your feature (you can find it here) file to resource folder in test package, once it is there you should be able to run your tests, just right click on feature file and hit green play button. The tests will fail, but what is more important for us cucumber will generate "Glue code", which we can easy copy and paste to Stepdefs class. I would recommend you download and install cucumber plugins for Intellij IDEA, it will help generate glue code, without using copy paste.

Selenium and Cucumber Test Automation

When glue code in Stepdefs class you would need to start implementing Selenium tests and call it inside of those methods. In next articles we will discover best practices of putting those tests together, but for now lets just keep it simple.

As you can see on screenshot below I already have some test methods inside of Cucumber methods and last two still haven't been touched:

So how does Gherkin and Cucuumber glue works together, as you can see it is linked to each other using Regular Expression, and keywords Given When And Then is helping cucumber to separate test steps one from another.

And finally lets add some selenium PageObjects classes, for example in our project we testing search functunality, for now lets just call it SearchTest class, but later we will need to refactor the name for test automation framework class naming consistency. You can find more information about PageObjects and PageFactory patterns in Selenium docs

As you can see I'm using chromedriver to run my Selenium tests, you can simply download it and locate in main resources folder, don't forget specify path in your tests.

After everything have been set, you can run your Cucumber tests the same way you have done it at the beginning of the blog post.

View Comments

Leave a Comment

Use markdown or basic HTML and be nice.