Cucumber Utils - CodeProject
Cucumber Utils - CodeProject
Slevoaca Florin
Summary
A Java library meant to help you write organized and clean Cucumber tests.
It takes over the usual dependencies and features needed inside a test framework, such as:
Most of these features can be used directly from code within your test project and are also available as Cucumber step definitions.
Tutorial
Follow the Cucumber Utils Tutorial for a better picture on how this library is/should be used.
Maven Central
<dependency>
<groupId>io.github.fslev</groupId>
<artifactId>cucumber-utils</artifactId>
<version>${latest.version}</version>
</dependency>
Gradle: compile("io.github.fslev:cucumber-utils:${latest.version}")
cucumber-java8
cucumber-guice
xml-unit
json-compare
apache-http-client
Configuration
In order to integrate cucumber-utils within your test project, you must configure the following glue package inside your IDE
Cucumber plugin or / and inside the code:
com.cucumber.utils
Features
1. Compare Mechanism
The following types of objects are supported for complex comparison, via the Cucumbers class:
dependency: json-compare
XMLs
dependency: xml-unit
If the objects compared are not of any type from above, then the comparison is done via the equals() method.
2. State-Sharing Mechanism
The state sharing mechanism is based on guice, cucumber-guice and Cucumber anonymous parameter types.
State is shared between different Cucumber steps inside the same scenario by using scenario properties.
2.1 How to Set and Use Scenario Properties
Within the Cucumber Scenario, by using the param <name>="<value>" Cucumber step:
As you can see, in order to use the value of a scenario property within your Cucumber scenario, you must call it by its name,
using the special symbols #[ ].
Under the hood: If your Cucumber step definition uses anonymous parameter types {}, then any #[property.name]
sequence is parsed and the corresponding value is mapped to the corresponding argument:
from resource file, via load scenario props from file "relative/path/to/file.properties" Cucumber step:
animal=Rabbit
.properties
.yaml
.property
.json
.xml
.txt
If a scenario property is read from a .property, .json, .xml or .txt file, then the name of the scenario property will actually be
the name of the file, without extension:
Rabbit
from resource directory, via load all scenario props from dir "relative/path/to/dir" Cucumber step:
where, inside the properties directory are defined several files containing the corresponding properties.
Note
The function for reading scenario properties from a directory walks through the whole directory tree structure. It filters only
the supported file types.
import com.google.inject.Inject;
@ScenarioScoped
public class ParamSteps {
@Inject
private ScenarioProps scenarioProps;
Example:
If comparison passes, then a new scenario property will be set, having the name userId and the value matched from the
HTTP response.
This new scenario property can be used further inside your test scenario:
3. Customized Clients
The following clients are available via Cucumber-Utils:
HTTP client
SQL clients (MySQL, PostgreSQL, Sybase, etc. -> depending on the SQL driver you configure)
Shell and Jsch clients
These clients are initialized and configured via the builder pattern, in order to allow construction of new settings between Cucumber
steps.
HTTP Client
Example:
Connect to SQL databases, execute queries, compare results and also execute updates:
host=vm-test1.sandbox.lan
port=22
user=tanja
password=*****
privateKey=/home/tanja/.ssh/id_rsa
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)