In the following post I am going to explain how to setup cucumber on Java with selenium to use for Behavior Driven Development.
I am going to use IntelliJ for writing this test cases, however you can choose eclipse or any other IDE as well.
Go to File >> New >> Project and select Maven as shown in the screenshot
Provide GroupId and ArtifactID
Provide the Project Name, After this step, IntelliJ will create new project for you.
I am going to automate following test case for this BDD (Cucumber) demo
Feature:, is a keyword through which you can specify which feature you are testing
Scenario:, is a keyword though which you can specify of which scenario following test cases belong to
Your test cases start after the scenario line. When specifies the step we need to perform and Then specifies the part we need to verify.
Now the next thing we need to do is, implement this test cases and write selenium code for it. however before that you need to write a Java class which runs your test cases. Create a java class under test\java\com.gaurang package and paste the following code.
Please create one file with following code
Now everything is in place, let's write the selenium code for Gherkin statement we wrote above. There are actually two way to do it.
I am going to use IntelliJ for writing this test cases, however you can choose eclipse or any other IDE as well.
How to create project.
I have also hosted the demo project on GitHub you can access that using following link
Add Maven Dependencies.
We need to add Cucumber, Selenium and JUnit dependencies to pom.xml. Open pom.xml and add the following code to it.<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.40.0</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies>
Automating Your First Test case
I am going to automate following test case for this BDD (Cucumber) demo
- Open wikipedia.com website
- Search for BDD
- Result page should contain Behavior-driven development text.
Now before we begin writing test cases, let's create proper packages. I have created the packages in following, however you can create the way you like.
Just make sure that all your code (directly or indirectly) is under test package.
The first thing we need to do, to write cucumber tests, is to convert existing test case into Gherkin Syntax. In the resources folder create one package and inside this package create one feature file, mine is called wikipedia_search.feature however you can give the name you want.
Feature: Verify WikiPedia Search Scenario: Verify Wikipdia for indirect posts When I search "BDD" Then I should get "Behavior-driven development" on page
Feature:, is a keyword through which you can specify which feature you are testing
Scenario:, is a keyword though which you can specify of which scenario following test cases belong to
Your test cases start after the scenario line. When specifies the step we need to perform and Then specifies the part we need to verify.
Now the next thing we need to do is, implement this test cases and write selenium code for it. however before that you need to write a Java class which runs your test cases. Create a java class under test\java\com.gaurang package and paste the following code.
Please create one file with following code
@RunWith(Cucumber.class) @CucumberOptions(plugin={"pretty", "html:target/cucumber-html-report"}) public class RunAllTests { }
Now everything is in place, let's write the selenium code for Gherkin statement we wrote above. There are actually two way to do it.
- Run the file created in above step, and on the console you will be able to see something like below. copy this code and paste into the class file.
- You can open the feature file and press ALT+Enter key combination, this will ask you to choose the file name and then it will create the empty implementation of all the steps in that file.
Once this is done, the only thing remain is to write corresponding selenium code.
Report:
Cucmber generates it's own HTML report, which look like this.
Cucumber HTML Report |