Useful practices of creation automatic tests by using cucumber jvm
1
Useful practices of creation
automatic tests by using
Cucumber-JVM
Shapin Anton
November 19, 2016
2
• Lead Software Test Automation Engineer
• 9+ years in IT
• Areas of my competency: manual,
automation, performance and etc.
• Successfully completed 9 BDD projects.
• 1 BDD projects in progress.
Email: anton_shapin@epam.com
Skype: anton_shapin
GIT: http://github.com/kirlionik
Shapin Anton
5
BDD approach
BDD(behavior-driven development) - is a set of software engineering practices
designed to help teams build and deliver more valuable, higher quality software faster. It
draws on Agile and lean practices including, in particular, Test-Driven Development (TDD) and
Domain-Driven Design (DDD).
BDD isn’t a software development methodology in its own right. It’s not a replacement
for Scrum, XP, Kanban, RUP, or whatever methodology you’re currently using.
MAIN GOAL: EXECUTABLE SPECIFICATION
6
How it works
@Given("^I perform Quick Search by "([^"]*)" $")
public void i_perform_quick_search_by(String query) {
driver.findElement(By.id(“searchQuery”)).sendKeys(query);
driver.findElement(By.id(“submit”)).click();
}
Scenario: Running a Full Text Quick Search.
Given I perform Quick Search by "IPhone 4S"
...
Each step maps to Java Method
File *.feature
File *.class
Example of GUI Scenario
Scenario: Running a Full Text Quick Search.
Given I perform Quick Search by "IPhone 4S"
When I click on link 'Search History' on panel 'Quick Search'
Then the term query "IPhone 4S" should be the first in the Search
History grid
9
Why I like BDD:
Test logic is in total independent
layer of implementation.
1
All test cases and automated
tests are up to date.
2
BA could accept user-story base
on test execution report.
3
Manual or Junior qa
automation engineers help
me develop automated tests.
4
Example of Web Service
IP Address Geolocation XML API
The API returns the location of an IP address (country, region, city, zip code, latitude, longitude) and the associated
time zone in XML format.
Usage:
For country precision (faster), do a query with the following API :
IPv4 Address
http://localhost.com/v5/ip-country/?key=<your_api_key>&ipv4=74.125.45.100
IPv6 Address
http://localhost.com/v5/ip-country/?key=<your_api_key>&ipv6=2001:0200:0102::
http://localhost.com/v4/ip-city/?key=<your_api_key>&ip=74.125.45.100
For city precision, do a query with the following API (if you omit the IP parameter it will check with your own IP) :
Best Practices. Test Step Creation.
Create template of steps:
WHEN:
• I set the …
• I send request to …
• I add …
Then:
• I get … .
• the report should contain ...
• the values of the ...
WHEN:
• I set the key as «12Gth6Ntds»
• I send request to Geo City
• I add the ip «74.125.45.100»
Then:
• I get http status as "200"
• the report should contain country «USA»
This will help find existing steps and creating a new steps
Best Practices. Test Step Creation.
This will help to find the existing steps and
do not make a mistake in the name of the parameter
Name of parameter must not be a variable
@When("^I set the key as "([^"]*)" $")
public void i_set_key(int number) {
// TODO: code goes here
}
@When("^I set the "([^"]*)" as "([^"]*)" $")
public void i_set_param(String name, int number) {
// TODO: code goes here
}
Given I set the “key” as “12Gth6Ntds”
Given I set the “ip” as “74.125.45.100” Given I set the key as “12Gth6Ntds”
BAD GOOD
Best Practices. Test Step Creation.
This will help understand function of steps
Create Javadoc before each Step definition methods
Best Practices. Test Step Creation.
User Helpers. For example IntelliJ IDEA plugin «Cucumber for Java»
15
Best Practices. Test Step Creation.
1. Separating the Support Code
2. Favorite way to organize step definition files is to organize
them with one file per domain entity: GeoCityStepDef.class,
GeoCountryStepDef.class.
3. By default Cucumber find StepDef classes in the same
package as *.feature files.
Organizing the Code
Best Practices. Test Step Creation.
This will help you get more understandable tests reports
Use cucumber plugins for reporting.
@CucumberOptions(
strict = true,
plugin = {
"com.github.kirlionik.cucumberallure.AllureReporter",
"pretty", "json:target/Cucumber.json",
"html:target/cucumber-html-report"
}
)
You can develop your own Cucumber plugins.
Best Practices. Test Step Creation.
Use cucumber plugins for reporting.
com.github.kirlionik.cucumberallure.AllureReporter
@SeverityLevel.CRITICAL @TestCaseId("geo-0001") @Issue(“geo-1006")
Scenario: Check city by ipv4
Given I set the key as "asd-asd-asd"
And I set the ip as "123.123.123.123"
When I send request to Geo City
Then I get http status as "200"
And the report should contain country "USA"
And the report should contain city "New York"
You can:
• Define Severity of each scenario.
• Create link to issue.
• Link to user-story.
• Create attachments.
• Other Allure Core features …
This will help you get more understandable tests reports
Best Practices. Test Step Creation.
Use cucumber plugins for reporting.
com.github.kirlionik.cucumberallure.AllureReporter
Best Practices. Test Step Creation.
public class Container {
public GeoServiceOutput output;
public String key;
public String ip;
}
All steps in Cucumber are independent.
This will help you develop automated tests and
use complex architecture of tests system.
Use class “Container” for transfer data between stepDef
methods and classes.
Best Practices.
Feel free to use parallel mode for test execution.
This will reduce tests execution time.
For example. How to:
• Create several “runner” classes with name “*ParallelIT.class”
• Define tags of features in each “runner” class.
You shouldn`t have the same tags in different “runner”
classes.
• Add profile into pom.xml file:
• Add parameters in Configuration section of “maven-failsafe-
plugin”:
• For execute tests run command:
mvn clean install -Pparallel
<profile>
<id>parallel</id>
<properties>
<junit.threadCount>4</junit.threadCount>
<junit.parallel>classes</junit.parallel>
<run.classes>**/*ParallelIT.class</run.classes>
</properties>
</profile>
<reuseForks>false</reuseForks>
<forkCount>20</forkCount>
<threadCount>${junit.threadCount}</threadCount>
<parallel>${junit.parallel}</parallel>
In my current project I reduced execution time from 30 min to 10 min
Best Practices.
You can transfer to Java StepDef method complex objects.
….
Then the report should have the next formatting:
| text | color | font | size | bold | type |
| Country | Black | Arial | 28 | true | NORMAL |
| Ip address | RED | Calibri | 20 | false | NORMAL |
@Then("^the report should have the next formatting$")
public void check_the_report_style(List<StyledText> styledTextsList) {
// TODO: code goes here
}
public class StyledText {
private String text;
private String color;
private String font;
private Integer size;
private boolean bold;
private String type;
}
Cucumber create objects of StyledText automatically.
22
Summary
1. Create Javadoc before each Step definition methods.
2. Create template of steps.
3. Name of parameter must not be a variable.
4. Use cucumber plugins for reporting.
5. Very carefully think through the architecture of your test
system (What? Where? Why? How?).
23
Conclusion
1. BDD is a very good approach. But this is not a magic bullet.
2. Most difficult things in BDD are create good test system
architecture and define «Rules of the game»
3. To use or not to use BDD depends on situation and project.
24
Thank you for attention!
Email: anton_shapin@epam.com
Skype: anton_shapin
GIT: http://github.com/kirlionik