This is a complete BDD automation framework template for web functional testing using Java, Cucumber and Selenium. It is based on the Page Object model, and it provides all the required pieces to start creating page classes and tests cases.
Even though I intended to make this template as complete as possible, it is susceptible to improvements and modifications based on your experience and requirements. I just wanted to share an approach that has worked for me on several projects. I hope you find it helpful.
- Java JDK (11+)
- Maven
- Cucumber-7 with Junit-5
- Selenium 4.8
The project contains 2 major modules:
-
main
contains all the elements required to interact with the web application, such as the Selenium WebDriver, page classes, and helper classes and methods. -
test
contains the test properties management, test steps and feature files for the test execution. Basically, code intest
calls web automation functionalities existing inmain
.
- Clone the repository.
In main:
- Create page classes under
pages
. Make new pages classes to extendPage
class, which contains commonly used methods to interact with a web page using Selenium. Page classes must receiveAppSession
as an argument in the constructor, which contains the properties for the current session and theDriverManager
.Then, add the element locators and methods for that page. Takepages.Home
as an example. - Add new page classes to the constructor of
app.PageManager
class. Take the existing examples as a reference. - Add any other property that needs to be passed to the page classes in the
app.AppSession
class. This template has already some predefined. - Customize the creation of the web drivers in
app.DriverManager
. Add arguments and preferences as required. Note: the DriverManager is using the Selenium Manager(Beta) approach to create the driver, which doesn't require the driver files. Modify theDriverManager
with any other suggested approach from Selenium if required.
In test:
- Define
test.properties
as required. For each property defined, make sure it is retrieved byproperties.TestProps
class either as mandatory or with a default value. By doing this, properties can be accessed easily from anywhere in the code. Note: test.properties is usually not shared in a repository since it might contain sensitive information like credentials - Configure
properties.TestContext
to initializeAppSession
,PageManager
and to set session properties.TestContext
is used to share properties across test steps using dependency injection, specifically cucumber-picocontainer for this framework. - Create a test steps file under
testSteps
.@Before
and@After
steps are included in thetestSteps.CommonSteps
class. For all the test steps file, make sureTestContext testContext
is defined as a parameter in the constructor, this way the properties and objects in TestContext are accessible from any test step in the file. TaketestSteps.LoginSteps
as an example. - Create feature files using existing test steps.
- Execute tests typing
mvn test
in the terminal.
Test properties, like the app URL, credentials, etc., can be retrieved from several places using the following order of precedence:
- Command argument
-Dkey=<value>
test.properties
file- Default value (if applicable)
test.properties
file is located at root directory by default. Note: test.properties is usually not shared in a
repository since it might contain sensitive information like credentials
Test execution is triggered by "mvn test" command in a terminal from the project directory.
mvn test
: takes test properties values from test.properties filemvn test -DtestPropertyKey=<testPropertyValue>
: Overwrites values from test.propertiesmvn test -DexcludedGroups="properties" -Dgroups="login | navigation"
: executes tests with -Dgroups tags and excludes the ones in -DexcludedGroups. Refer to Cucumber-Junit tags filtering
This framework supports parallel test execution using the Cucumber JUnit Platform Engine settings. To enable parallel execution, set the cucumber.execution.parallel.enabled configuration parameter to true in junit-platform.properties. Be cautious when executing tests in parallel since it consumes a lot of memory. Refer to the documentation to do the proper configuration
- Features are run in alphabetical order of the .feature file name