BDD with Cucumber and Selenium for Living Test Documentation


If you are already a fan of Selenium Webdriver with Java and need some upgrades for BDD (Behavior Driven Development) with cucumber for human-readable test scenarios, this article is for you!

I will show you, how you do this with IntelliJ IDE Maven project. First of all, you need the following plugins has been installed and enabled for your IDE. (Preferences->Plugins)

  • Cucumber For Java
  • Gherkin

There is a useful documentation for Cucumber settings in this link:

https://www.jetbrains.com/help/idea/2017.1/cucumber.html

You should use most popular test runners Junit or TestNG for your test asserts. It’s not a problem. I suppose that you have already added this to your pom.xml file. And the other things.

Here we should go!

We will automate a web application’s login screen. There will be positive and negative test cases. We need 4 files for BDD.

  1. Feature File: Will be written with Gherkin syntax Given-When-Then things.. And will be human readable.
  2. Step Definition File: Should be automatically generated from feature file. It will link your feature file with regex to this. So you should call/write some java codes.
  3. Page File: Selenium Webdriver codes go here. Write your functions and call them from step definition file.
  4. Feature Runner File: This file will be your test suite file. You should run all test scenarios with this file for CI/CD integrations. So you need to specify your feature file location (Another name is Glue)

Cucumber Feature File

  • Create a new file has named with “LoginScenario.feature
  • Write your tests like the following.
  • Create step definitions by pressing “Alt+Enter” key combinations one by one for each scenario. So It will ask you for creating a new java file. You should create a file named with “LoginStepDefinitions” and use for all Login Scenarios.

The first Successfully login test scenario will run once but the other negative cases will run twice. So if you run these all Login tests, 5 tests will run in the suite. Because There are data tables. The test scenarios will run for each data. (1+2+2)

Step Definition File

  • Create a “LoginStepDefinitions.java” file if it has not created yet.
  • I recommend that using functions, not Selenium/Java codes for not duplicated reusable coding.
  • I suppose that you know how to open a browser window in selenium webdriver. I escaped it.
  • There will be regex parts of your feature file like Given-When-Then things. You should write or call your Selenium-Java codes.

@Given(“^I am on the www.garanti.com.tr Home Page$”)
public void iAmOnGarantiBankHomePage() {
}

This is a basically working screenshot from demo project. You should use cucumber’s own before-after methods instead of JUnit/TestNG.

Page File

This file should include any of the background Selenium and Java codes like driver.findElement(By.id(“abc”)) etc.I know you are genius testers and You have done 🙂

Feature Runner File

Basically, you should use the following code, it will work. Give the path of your Feature files location and Step Definition Files path with the following syntax.

Reviewing test results with running tests by feature file:

Reviewing test results with running tests by runner suite:

If you like that writing test cases with Given-When-Then, Enjoy it!

You can find whole project in below link.
https://github.com/swtestacademy/BDDCucumberSeleniumWebTest 

If you want to import this project, you need to do below steps:

1) Open IntellJ and click “Git” as shown below.

2) Set Git repository as “https://github.com/swtestacademy/BDDCucumberSeleniumWebTest.git” and select your project folder and click “Clone”.

3) Right click “pom.xml” and click “Add as Maven Project.

4) Click File -> Project Structure and set top and src folder as “Sources”.

5) Right click pom.xml and click Maven -> “Download Sources and Documentation”

6) Open Module Settings and do the followings.

7) Go to File->Seetings and write “javac” to the search bar and set target bytecode version from 1.5 to 1.8

and build your project!

If you do not get any error, then run your tests!

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...
extentreports

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary https://java.com/tr/download/ 2- Download latest Jmeter version 3.0 or higher. http://jmeter.apache.org/download_jmeter.cgi 3- Download Jmeter PluginsManager JAR file and...
By | 2017-06-14T02:27:54+00:00 June 14th, 2017|Cucumber, Selenium, Test Automation|4 Comments

About the Author:

Ozgur Kaya
Özgür KAYA is a Senior Software testing and devops professional with 10+ years of experience. He has worked at Turkcell Global Bilgi, Veripark, Turkcell, N11, Trendyol as system & application administrator, test & devops & test automation engineer, test team lead. Now, he is working as Software Testing Team Lead at EFT Software. He is interested in Continuous Integration & Continuous Delivery configurations, Building Test automation framework in everywhere, Build, Release and Deployment Management, Load & Performance & Stress Testing, Application Administering & Monitoring, Security Testing with OWASP rules, Test & QA team management. You can find detailed information about him on his linked-in page.

4 Comments

  1. Marcus June 17, 2017 at 12:03 am - Reply

    Just another “login page” example. Looking forward to see a real-life complex example, where pages change according to user logged in and so on.

    • Onur Baskirt
      Onur Baskirt June 18, 2017 at 1:04 am - Reply

      If you have experience on it, we are so happy to publish your real-life complex article. Thanks.

  2. Djelloul September 7, 2017 at 8:28 am - Reply

    its better to use Serenity BDD Framework

    • Onur Baskirt
      Onur Baskirt September 7, 2017 at 11:57 pm - Reply

      It will be a choice. Also, Gauge is an another option.

Leave A Comment