Behavior-Driven Development (BDD) using
Selenium WebDriver and Cucumber-JVM: A
Step by Step Tutorial
Software Quality Engineering
Introduction
BDD Basics: Behavior-Driven Development also known as Acceptance Test-Driven Development is an agile development practice that has evolved from Test-Driven Development (TDD). BDD was introduced by Dan North (for more information visit http://dannorth.net). Behavior-Driven Development provides answers to two important questions:
·
What
to test
·
When
to test
Behavior-Driven Development has the
following benefits:
·
Fast
feedback to the development team
·
Stakeholder
involvement
·
Tests
are written early in the development process
·
Tests
are based on user (stakeholder) Acceptance criteria
·
Tests
are written in a language (English) that business/non-technical folks can
understand
BDD = TDD + automated acceptance testing.
Intended Audience: This tutorial is meant for software test engineers, test automation engineers, or programmers who may be interested in learning how to implement Behavior-Driven Development (aka Acceptance Test-Driven Development) using Cucumber-JVM in Java and Selenium WebDriver. The tutorial assumes the user has basic familiarity with Java, Selenium, Eclipse, Maven, and MS Windows 7.
The tools you will need
·
Java
(JDK 7 or later)
·
Cucumber-JVM
·
Eclipse
·
Maven
·
Selenium
WebDriver
Installation and Setup
1. Java (JDK): You may install JDK by
- downloading it from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
-
run
the JDK installer
-
Update
the PATH variable
-
From
the command line type: java –version
to verify that java was installed correctly, you should see something similar
to the screenshot below.
2. Eclipse: Download and install Eclipse from http://www.eclipse.org/downloads/ . For this tutorial I used Eclipse Kepler -- Eclipse IDE for Java EE Developers. Follow the instruction provided in download package to install Eclipse or visit https://ist.berkeley.edu/as-ag/tools/howto/install-eclipse-win.html for installation instruction.
3. Maven: You may install Maven plugin from Eclipse IDE as follows:-
a. Launch Eclipse IDE
b. From Eclipse IDE navigate to Help à Install New Software…
C. Click Add button at top right corner.
d. At the dialog box pop up: fill in the Name as "M2Eclipse" and Location as http://download.eclipse.org/technology/m2e/releases
e. Click the Ok button and follow instruction to
complete the installation.
After the
installation, verify that Maven has been installed:
a.
Go to Window à Preferences
b.
Observe, Maven is enlisted at left panel
c.
Launch the command line to verify that Maven has been installed and
configured correctly, type: mvn –v and
you will get something similar to the screenshot below:
4. Cucumber-JVM: You may install the Cucumber-Eclipse plugin as follows
- Launch Eclipse, if it is not running already
- Click Help à Install New Software…
- Click the Add button on the Install dialog box
- Enter Cucumber Eclipse Plugin in the Name textbox In the Location textbox enter http://cucumber.github.com/cucumber-eclipse/update-site
- Click Ok button
- Select the latest version of Cucumber Eclipse Plugin checkbox from the Available software window
- Click Next
- Follow instructions on the screen to complete the setup
5. Selenium WebDriver: Download the latest version of Selenium (version 2.45.0 was used for this tutorial) from http://docs.seleniumhq.org/download/; use the selenium installation documentation from that website to setup and configure Selenium. We will configure Eclipse to work with Selenium in a later section.
Let’s Get Started
In this tutorial I
will demonstrate how to use the Cucumber-JVM framework with Selenium WebDriver
to implement Behavior-Driven Development in Java. Developers, QA, and
non-technical/Business people can work together to write features and scenarios
in Gherkin language. The features and scenarios are based on the feature/user
story under test.
Create the Project:
1.
Launch
Eclipse and point it to a workspace (where you would like your project to
reside)
2.
Create
a new maven project
a.
Project
à New à
other…
b.
Select
Maven project from the Select
a wizard dialog box and click Next
from the dialog box that appears
c.
Place
a check mark on the Create a Simple
Project (skip archetype selection) checkbox on the New Maven Project dialog box and click the Next button
d.
In
the New Maven Project (Configure
project) dialog box, fill in the required fields. A sample is shown below;
you may choose a different Group ID, artifact ID, and Name for your
project. Click the Finish button.
3.
Your
project has been created; verify that the project structure is similar to the
screen shot below.
4.
Verify
that the JRE System Library is the same as the installed JRE version on your
computer, if not change it to be the same. To change the JRE System Library, do
the following:
a.
Select
and right-click the JRE System Library
folder from your project
b.
Select
Build Path à
Configure Build path… from the context menu to open the Java Build Path dialog box
c.
Select
the JRE System Library (J2SE-XX) and
press the Remove button from the Libraries tab
d.
Click
the Add Library button to open the Add Library dialog box
e.
Select
JRE System Library from the Add Library dialog box and click Next.
f.
Select
the Workspace default JRE (jren,
where n is the JRE version), if it is not already selected.
g.
Click
Finish.
h.
Click
Ok, to accept the change you just
made and close the Java Build Path window
5.
Add
dependencies to the POM file. To add a dependency, do the following:
a.
From
your project right-click the POM.xml
file
b.
Select
Maven à
Add Dependency from the context menu to display
the Add Dependency dialog box
c.
Complete
the fields in the Add Dependency dialog box and click Ok.
i. For this tutorial I
added the following dependencies; please make sure the version numbers you use
match the versions of the tools installed on your PC:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId> junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId> selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId> cucumber-core</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId> cucumber-html</artifactId>
<version>0.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId> cucumber-jvm-deps</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<scope>test</scope>
</dependency>
6.
Add
plugin(s) to the POM file. To add a plugin do the following:
a.
From
your project, right-click the POM.xml file
b.
Select
Maven à
Add Dependency from the context menu to display
the Add Plugin dialog box
c.
Complete
the fields in the Add Plugin dialog box and click the Ok button to accept your change and close the dialog box.
i. For this tutorial I
added the following plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>-Duser.language=en</argLine>
<argLine>-Xmx1024m</argLine>
<argLine>-XX:MaxPermSize=256m</argLine>
<argLine>-Dfile.encoding=UTF-8</argLine>
<useFile>false</useFile>
</configuration>
</plugin>
(Hint: after you add the plugin, open the POM file with Maven
POM Editor, select the POM.xml tab to include the configuration section to the
Plugin)
7.
Now,
we will create a new package to make a home for the feature file(s)
a.
Select
and expand the project in Package Explorer, if it is not already expanded
b.
Right-click
on src/test/resources
c.
Select
New à
Package from the context menu, to display the New Java Package dialog box
d.
Enter
a name for the package and click Finish
e.
We
will now create a feature file in the package we just created.
i. Right-click the
package we just added in the last step and click New à File from the context menu, to open the New File dialog box
ii. Enter the name of
your feature file in the New File dialog box (be sure to include a .feature
extension), and click the Finish
button
f.
Enter
the following text in the file we just created
Verify that
your project looks like the screenshot below:
g.
Now,
we will create a package where our step definition (and other java) class(es)
will live.
i. From the project
tree in Eclipse, right-click on src/test/java
ii. Select New à Package to display the New Java Package dialog box (see the screenshot in step 7 c.)
iii. Enter a name for the
new package in the New Java Package
dialog box and click Finish
h.
Create
a new class in the package we just created, to define the steps in the feature
file we created earlier
i. Right-click the
package created above and select New à Class from the context menu to display the New Java Class dialog box
ii. Enter a name for
your class in the New Java Class
dialog box, then click the Finish
button
iii. Include the
following code in the import section of the step definition file we just
created:
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.*;
import cucumber.api.DataTable;
import static
org.junit.Assert.*;
import java.util.Map;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.*;
import
org.openqa.selenium.remote.DesiredCapabilities;
import
org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
iv. We will create
another java class that will run our test
1.
Follow
the instruction about creating a new class, but this time name the class RunCukesTest
2.
Copy
the following code into the class:
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions ( plugin = {"pretty", "html:target/cucumber-htmlreport", "json:target/cucumber-report.json"})
The new class looks like below (ignore the errors for
now):
v. We need to tell the
project where to fine selenium libraries and other jar files the project needs
1.
Right-click
the project and select Build Path Configure Build Path from the context menu, to display the project properties window
2.
In
the project properties dialog box click the Libraries tab to make it the active tab
3.
Click
the Add External JARs… button to
open the Jar Selection dialog box
4.
Point
the JAR selection to your selenium location and add all the jar files in the
selenium folder and subfolder(s) to the project
5.
Click
Ok to accept your changes and close
the project properties dialog box. Notice that most of the errors are resolved
after the project was updated
6.
We
will further update the project with the dependency jar files that we added to
the POM file earlier. I have included a folder called jars in the
tutorial zip file. Copy that folder to your project folder and repeat step 4
above to include the dependency files in your project’s external jar path.
Alternatively, you may download these files from the internet or configure
Maven to manage these dependencies for you– see the files you need on the
screenshot below. After you add the files, you will notice that all the project
errors are resolved
vi. Write the step
definition code to make use of the feature file we created earlier
1.
Run
the project so that the Cucumber API will generate the binding code we will
need to create our step definition implementation
a.
Right-click
the project and select Run As à
Maven test
b.
Wait
for Maven test execution to complete
c.
Locate
the console tab (usually on displayed on the bottom pane of Eclipse window)
d.
Copy
the code snippet that has been generated for us, to the step definition class.
e.
Write
the code to implement the steps. The completed step definition code is shown in
the screenshot below. Be sure to download IEDriverServer and point the file
library to it as shown in setup method below. Open the zipped project file in
Eclipse for more details.
vii. Run the test
1.
Before
we can run the test we need to setup the BMI website on the local PC
a.
Unzip
BMICalculator.zip, included with this tutorial, to C:\inetpub\wwwroot (assuming Windows is installed on your C
drive)
b.
Launch
IIS on your local PC and create a new website to host the BMI web application. Visit
http://support.microsoft.com/en-us/kb/323972 for more
information on configuring and hosting a website on your local machine. You may
use the document, HostingBMICalculatorForDemo.pdf, to setup and run the BMI
calculator on your local PC.
c.
Verify
that the new website is functional.
2.
Run the test from Eclipse IDE; Right-click the
project and select Run As à
Maven test
viii. View test results
1.
From
Eclipse, expand the project, if it is not already expanded
2.
Expand
the target folder
3.
Expand the cucumber-htmlreport
folder
4.
Double-click
the index.html file to open the test
report (see screenshot below)
You can download the completed project
You are
done!!! I hope it worked for you.
No comments:
Post a Comment