While searching a better
automation solution for web applications, I could find a great automation
framework which is called “Cucumber”. Simply, Cucumber is a BDD supported
testing tool that executes automated tests. It is easy & flexible solution
that supports java-selenium development. Unlike selenium robot framework, this
BBD solution has a huge technical support on internet.
What is BDD?
BDD (Behavior Driven Development) is a good extension to the conventional TDD (Test
Driven Development).
TDD is widely using by
developers to write their unit tests. Add a test, run entire test suit and
write some code on newly added test, rerun all tests and refactor is the main
concept of TDD.
BDD
has a different approach. Its mainly focus on the behavior (input and outcome)
of the functionality. In other words, BDD is a combination of user story (a
short introduction of tests) and acceptance criteria.
Example :
Cucumber
is used to execute tests written in this format. The language that
Cucumber understands is called 'Gherkin'. Gherkin is just a combination of four keywords (given, when, then, and). It's very easy
to write tests with Gherkin. also, even non-technical people can understand the
tests and scenarios. Check below example which is a test written in Gherkin.
Example :
As a [role] I want
[feature] so that [benefit]
Given [initial context]
When [input event]
Then [outcome]
Feature: Search on Google
As an user
I want to search on Google
So that I can see results
Scenario: results are
shown
Given the page is open
"http://www.google.com"
When I search for
"Cucumber"
Then a browser title
should contains "Cucumber"
Now, let’s have some fun with
cucumber!
Setup your first cucumber project
- Download sample cucumber-java-selenium project
from here - https://github.com/michalvich/cucumber-jvm-selenium-example
Note 1 : Check the directory structure. You can see ‘pom.xml’ in the root directory of the project. ‘pom.xml’ is the fundamental unit of work in Maven. It is contains information about the project and configuration details used by Maven to build the project.
Note 2 : You can see the .feature file at '..\src\test\resources\com\michalvich\cucumber\selenium\example'. That is the cucumber test case document which is written in Gherkin. - Install Java, create a user variable “JAVA_HOME” and set it to jdk installation directory (example - C:\Program Files\Java\jdk1.7.0_21)
- update/create the Path environment variable in the user variables and append the %JAVA_HOME%\bin
- Check java is installed successfully. ( type “java –version” in command prompt)
- Download eclipse IDE (https://www.eclipse.org/downloads/)
- Download maven (http://maven.apache.org/download.cgi)
- Read the installation instruction at http://maven.apache.org/download.cgi#Installation or follow these steps.
- Add the M2_HOME environment variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.2.1
- Add the M2 environment variable in the user variables with the value %M2_HOME%\bin
- update/create the Path environment variable in the user variables and append the value %M2%
- Check maven is installed successfully. (type “mvn –version” in command prompt)
- Go to the place which pom.xml located and type,
- “> mvn package” (to build the project)
- “> mvn install” (optional)
- “> mvn eclipse:eclipse” (to generate the eclipse project files from your POM)
- Open eclipse and set the maven repo directory (in eclipse - windows - preferences - java - built path -> add M2_REPO --- maven repo directory (C:/Users/Test/.m2/repository))
- Open eclipse and import the project ( file – import – general - existing project in to workspace)
- Run the project as a junit (run – run configurations – browse the project and the class)
Note 3 : You can see java code at '..\src\test\java\com\michalvich\cucumber\selenium\example', which is the code we should write to execute our cucumber test cases.
Note : Make this small change before proceed. Open the 'pom.xml' file. find this set of codes
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.28.0</version>
</dependency>
Change the version no "2.28.0" to "2.41.0"
How to write your own code?
- First, write a cucumber test case document, save as a .feature and put it in the same directory which locate ‘GoogleSearch.feature’.
- Then, Create a empty java class with the same name and just run it. You can see the code snippets print in your eclipse console window.
- Finally, Copy and paste the snippets to implement missing steps. Write few lines of codes for test. you can have any help from internet.
Look! How easy it is! Software automation is not a nightmare anymore!
No comments:
Post a Comment