Framework Structure

NOTE: If you have followed the Java Selenium Blog Series, then you can also skip this blog post and simply do the following only…

  • Add an ‘apis’ package to the {project-root}/src/test/java directory
    • Add the following class files in the ‘apispackage…
      • BaseApiTests
  • Add an ‘apisteps’ sub-package in the ‘steps‘ package
    • Add the following class files in the ‘apistepspackage
      • BaseApiScenariosSteps
  • Add a sub-directory in the ‘features‘ directory called ‘apifeatures
    • Add the following file in the ‘apifeatures‘ directory…
      • BaseApiScenarios.feature

Directories & Packages

  1. Right-click on the {project-root}/src/test/java directory, select New –> Package, and add the following packages (as well as sub packages within other packages.  You may need to put a temporary dummy file in ‘utils’ package first before adding the sub-packages):
    • apis
    • steps
    • utils
      • hooks
      • settings
  2. Right-click on the {project-root}/src/test directory, select New –> Directory
    • Name the directory ‘resources’ and click OK
  3. Right-click on the {project-root}/src/test/resources directory and select ‘Mark Directory As’ –> ‘Test Resources Root’ (if not marked already)
  4. Right-click the /src/test/resources directory again and select New –> Directory
    • Name the directory ‘features’ and click OK

Initial Files in each Package / Directory

Features folder

  1. Right-click the ‘features’ directory, select ‘New’ –> ‘File’ and add the following feature files (Feature files is what Cucumber uses to write BDD scenarios using Gherkin language. Please see https://cucumber.io/ for more information)
    • BaseApiScenarios.feature

APIs package

  1. Right-click the ‘apis’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘apis’ package will contain all our class files for our REST API tests)…
    • BaseApiTests

Steps package

  1. Right-click the ‘steps’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘steps’ package will contain all our class files for our step definitions)…
    • BaseApiScenariosSteps

Utils package

Hooks sub-package

  1. Right-click the ‘hooks’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘hooks’ package will contain classes for all the different hooks we can run before and/or after test runs/suites/scenarios etc.)
    • CucumberHooks

Settings sub-package

  1. Right-click the ‘settings’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘settings’ package will contain a class file for all our settings, e.g. login details and things we want to use in our tests, that won’t change (hence everything in Settings class will probably be ‘public static final’)
    • Settings

TestNG

With TestNG, a TestRunner class can be made which defines everything that you want to happen to a group/groups of tests. To work with Cucumber, you must define the directory where your Feature files are located in the project, as well as define where the glue is that connects your Feature files to your java test methods (both hooks and step definitions). The TestRunner class can also define what happens before, after and during the tests are run.

Creating the ‘TestRunner’ Class

  1. Right-click the src/test/java directory, select ‘New’ –> ‘Java Class’ and name the class “TestRunner”
  2. Add the following lines of code in to the ‘TestRunner’ class (also note the @CucumberOptions, where the location of the feature files, glue files and any formatter plugins are defined)…

    Interestingly, you can actually make as many TestRunner classes as you see fit for your project and then define which TestRunner classes, if any, get run within a test suite.  You define all of this within a file called ‘testng.xml’.

Creating the ‘testng.xml’ File

  1. Right-click the project root in the Project Explorer, select ‘New’ –> ‘File’ and create a file called ‘testng.xml’ and press the ‘OK’ button
  2. Open up the ‘testng.xml’ file and add the following lines of XML into the file (using similar/appropriate names for your own test suite and test name)…

Log4J2

Apache Log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is part of the Apache Logging Services project of the Apache Software Foundation. Log4j is one of several Java logging frameworks.

Configuring Log4J2

In our framework, we will create a Log4J2 config file and use it to configure our logging to the console.  You can also use Appender class in Log4J2 with a config file to configure logging to files, however we will stick to the Console in this blog series.

You can have one of many different types of Log4J2 config files, with certain filetypes being read before falling back to others.

In our framework, we will go with a simple XML config file.

For more info, please visit https://logging.apache.org/log4j/2.x/manual/configuration.html

Log4J2 XML File

  1. Right-click on the ‘/src/test/resources‘ directory and select ‘New’ -> ‘File’, name the file ‘log4j2-test.xml’ and press OK
  2. Paste the following into the xml file, it should look like below…

Building the Maven Project

  1. In the app bar for IntelliJ at the top, select ‘Build’ –> ‘Build Project’

In the next part, we will get started creating an application to use with an API and getting setup.

Building the Gradle project

If you have instead setup the project with Gradle instead of Maven, you can follow the steps below to build…

  1. Open CMD-prompt / Terminal and cd (change directory) into the root of your Maven project. For example…
    $ cd /Users/username/DevProjects/ProductAutomationFramework  (without typing the $)
  2. Enter the following command to build your Maven project…
    $ ./gradlew build  (without typing the $)

The core framework is now setup! In the next part, we will see how to create our Base Scenarios.

Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.