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.
- Feature File: Will be written with Gherkin syntax Given-When-Then things.. And will be human readable.
- 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.
- Page File: Selenium Webdriver codes go here. Write your functions and call them from step definition file.
- 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)
Feature: Login section @QuickTest Scenario: Successfully Login to Website Given I am on the www.garanti.com.tr Home Page When I click Login Link on Home Page And Fill UserID field with "111234567854" and Fill Password field with "P@ssw0rd" and click Login on LoginPage And I click SMS Code and write "458298" on Login Page Then I should see url contains "wellcome" #And I should see my account Dashboard page Scenario Outline: UnSuccessfully Login with Invalid SmsCode to Website Given I am on the www.garanti.com.tr Home Page When I click Login Link on Home Page And Fill UserID field with "111234567854" and Fill Password field with "P@ssw0rd" and click Login on LoginPage And I click SMS Code and write "<smscode>" on Login Page Then I should not see url contains "wellcome" Examples: Valid user Invalid SMS Code | smscode | | 123456 | | 11111 | Scenario Outline: UnSuccessfully Login with Credentials to Website Given I am on the www.garanti.com.tr Home Page When I click Login Link on Home Page And Fill UserID field with "<userid>" and Fill Password field with "<password>" from list and click Login on LoginPage Then I should not see url contains "dashboard" Examples:Invalid User | userid | password | | 68131538 | P@ssw0rd | | 123454 | P@ssw0rd |
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.
import cucumber.api.CucumberOptions; import cucumber.api.testng.AbstractTestNGCucumberTests; @CucumberOptions(features = "src/test/testScenarios/", glue = "test.tests.cucumber" , monochrome = true, format = {"pretty", "html:target/cucumber"}) public class LoginFeatureRunner extends AbstractTestNGCucumberTests{ }
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!

Ö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, Emirates Airlines, and Arute Solutions, and Mobven.
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.
If you have experience on it, we are so happy to publish your real-life complex article. Thanks.
its better to use Serenity BDD Framework
It will be a choice. Also, Gauge is an another option.
Hi Our,
Please post selenium c# nuint framework from scratch. Thanks