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!