Recently I started looking at ways of writing Selenium functional tests for a set up using BDD. There are different tools available to write the tests such as easyb, scalatest & cucumber. This blog post will focus on setting up a test project using Selenium 2, maven and Cucumber.
Once we have set up the Maven project then we need to set up the POM file so that all the required jar files for Selenium and Cucumber are available to the project.
Following are the contents of the pom.xml file for my project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
The project structure that I have created is as follows.
Now create a “GoogleSearch.feature” file under the features directory as.
Feature: Google Search Feature
In order to ensure that Google Search works
I want to run a quick Hello World search.
Scenario: Hello World Scenario
Given The search is Hello World
When The Search is performed
Then The browser title should have Hello World
Now create a class file under the following package.
src/test/java/cucumberdemo/GoogleSearchFeature.java
If you have not installed Cucumber before then for the first time you run this project you need to specify the following command.
mvn clean integration-test
Following is the out put of doing this command.
[INFO] Feature: Google Search Feature
[INFO] In order to ensure that Google Search works
[INFO] I want to run a quick Hello World search.
[INFO]
[INFO] Scenario: Hello World Scenario # features/GoogleSearch.feature:5
[INFO] Given The search is Hello World # features/GoogleSearch.feature:6
[INFO] When The Search is performed # features/GoogleSearch.feature:7
[INFO] Then The browser title should have Hello World # features/GoogleSearch.feature:8
[INFO]
[INFO] 1 scenario (1 undefined)
[INFO] 3 steps (3 undefined)
[INFO] 0m0.334s
INFO] You can implement step definitions for undefined steps with these snippets:
[INFO]
[INFO] @Given ("^The search is Hello World$")
[INFO] @Pending
[INFO] public void theSearchIsHelloWorld() {
[INFO] }
[INFO]
[INFO] @When ("^The Search is performed$")
[INFO] @Pending
[INFO] public void theSearchIsPerformed() {
[INFO] }
[INFO]
[INFO] @Then ("^The browser title should have Hello World$")
[INFO] @Pending
[INFO] public void theBrowserTitleShouldHaveHelloWorld() {
[INFO] }
Now copy the step definitions from the command line interface and add these steps to the GoogleSearchFeature class file as follows. If you notice that there is an annotation @Pending, this would make sure that if the feature has not been implemented then Cucumber will ignore these tests. In the following class, I have removed the @Pending annotation and add Selenium 2 code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
If you go the terminal again and run the following maven command again.
mvn clean integration-test
You will see the tests running and once the tests are finished you can see the following test results in the project’s target directory.