Framework Structure

It is in this part of the series that  you will start actually writing code.  If you are somewhat new to coding in Java and Selenium, I recommend writing out all the code snippets from scratch rather than copying and pasting them, for some of the following reasons;

  • As you start writing code, you will start picking up on patterns in the code, which will help you pick up things quicker
  • As you write out the code and try to reference certain libraries, IntelliJ will auto-suggest references to import for you, which will also help you learn the code that you are writing 🙂

utils.drivers package

Editing the ‘ChromeWebDriver’ Class

  1. Open up the ‘ChromeWebDriver’ class in the ‘utils.drivers’ package and edit the class file to match below, to add functionality to setup and load the ChromeWebDriver specifically. Also note here how we use WebDriverManager to download and setup the ChromeDriver before we execute any other methods in the ChromeWebDriver class…

Editing the ‘FirefoxWebDriver’ Class

  1. Open up the ‘FirefoxWebDriver’ class in the ‘utils.driver’ package and edit the class file to match below, to add functionality to setup and load the FirefoxWebDriver specifically. Also note here how we use WebDriverManager to download and setup the ChromeDriver before we execute any other methods in the FirefoxWebDriver class…

utils.selenium package

Editing the ‘DriverController’ Class

  1. Open up the ‘DriverController’ class in the ‘utils.selenium’ package and edit the class file to match below, to create an instance of DriverController which can be used to start either the Chrome or Firefox driver (we have also used Log4J here to report any errors when stopping the WebDriver)…

Editing the ‘Driver’ Class

  1. Open up the ‘Driver’ class in the ‘utils.selenium’ package and edit the class file to match below, to create a public WebDriver instance which returns DriverController when initiated, which can then be used in test methods (e.g. maximising the browser() window)…

Editing the ‘Settings’ Class

  1. Open up the ‘Settings’ class in the utils.selenium’ package and edit the class file to match below.  We will be using the Settings file to store public static variables that we will want to be using in our project.  For the purposes of this course, I will be explaining how to automate a simple DuckDuckGo search, therefore I will set my baseUrl to start.duckduckgo.com…

utils.hooks package

For now, we will only be adding Before and After hooks for the top-level Cucumber scenarios/feature files.

Editing the ‘CucumberHooks’ Class

  1. Open up the ‘CucumberHooks’ class in the ‘utils.hooks’ package and edit the class file to match below.  This class file will contain all our Before and After hooks for our Cucumber scenarios, which respectively get executed before or after a Cucumber feature and/or scenario. Please see https://github.com/cucumber/cucumber/wiki/Hooks for more information…

    As you can see above, I have used Cucumber tags in my hooks (see https://github.com/cucumber/cucumber/wiki/Tags for more information) to launch different browsers with different arguments passed (e.g. headless browser or normal browser with extensions disabled), depending on the Cucumber tag the Feature or Scenario being tested has.

pages package

Editing the ‘BasePage’ Class

  1. Open up the ‘BasePage’ class in the ‘pages’ package and edit the class file to match below.  The ‘BasePage’ class is where we will later put all our test methods for our BaseScenarios, and any methods that exist / can be shared across more than one specific page object (e.g. main navigation, validate a passed pageUrl etc)…

Editing the ‘Page’ Class

  1. Open up the ‘Page’ class in the ‘pages’ package and edit the class file to match below.  The ‘Page’ class is where we create a generic method called instanceOf that takes the same generic class and initialises a new object from PageFactory with the correct elements for the page. All page classes inherit in some way from ‘BasePage’, which in turn inherits from this ‘Page’ class

Step Definitions

Editing the ‘BaseSteps’ Class

  1. Open up the ‘BaseSteps’ class in the ‘steps’ package and edit it to match as below. This is just a placeholder step definition that inherits from ‘Page’ class, to help follow POM concepts…

Editing all the other Step Definitions

  1. Open up every other Step Definitions class file and change the classes so that they all inherit/extend from the ‘BaseSteps’ class (and thus in turn, inherit from the ‘Page’ class). Please find one example below…

TestNG

With TestNG, a TestRunner class can be made which defines everything that you want to happen that group 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 Project

Building the Maven project

  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…
    $ mvn clean install -DskipTests (without typing the $. We also currently have no tests. If we don’t put in the skipTests command, we will get a build error around one of the cucumber report plugins we are using).

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.