Table of Contents
Framework Setup
Creating the Maven Project
- Launch IntelliJ and select ‘Create New Project’
- Select ‘Maven’ in the left pane
- Select the appropriate Java JDK in the ‘Project SDK’ dropdown at the top (should be the one installed previously)
- Click ‘Next’
- In the ‘GroupId’ field, enter an appropriate name (e.g. “com.producttestframework”)
- In the ‘ArtifactId’ field, input an appropriate name (e.g. “AutomationFramework”)
- Click ‘Next’
- Give an appropriate Project Name (e.g. “ProductAutomation”) and an appropriate project location on your local machine (e.g. ~/Users/username/DevProjects/ProductAutomation)
- Click ‘Finish’
- Advance to IntelliJ IDEA –> Preferences/Settings –> Build, Execution, Deployment –> Build Tools –> Maven –> Importing (will be slightly different depending if you’re on Mac or Windows)
- Ensure ‘Import Maven projects automatically’ is checked
Maven Dependencies
You can manage all your projects dependencies, plugins and packages by simply adding them into your Maven project’s POM.xml file 🙂
- Open up the POM.xml file and add the following source and target properties within the <project/> tags…
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
- Open up the POM.xml file and add the following dependencies within the <dependencies> tag section. If you do not see any <dependencies/> opening and closing tags, simply add them within the <project/> tags.
Please note that the latest versions may change since the date I wrote this guide. I recommend searching for each dependency below on https://mvnrepository.com and simply copying the dependencies from there and then pasting them into your project’s POM.xml file.
Do make sure however, that you include certain information like ‘exclusions’ for JUnit in the TestNG dependency as shown in my examples below.
Cucumber Dependencies
Cucumber-Java
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>4.8.0</version> <scope>test</scope> </dependency>
Cucumber-JVM-Deps
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.6</version> <scope>test</scope> </dependency>
Cucumber-TestNG
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>4.8.0</version> <scope>compile</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency>
Cucumber-Reporting
<dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>3.8.0</version> </dependency>
TestNG Dependencies
TestNG
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.8</version> <scope>test</scope> </dependency>
Selenium WebDriver Dependencies
Selenium Java
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.12.0</version> </dependency>
WebDriver Manager (used to automatically grab the necessary Selenium Drivers when creating an instance, rather than manually setting a path to them)
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>2.2.1</version> </dependency>
Extra Needed Dependencies for Drivers
Google Core Libraries for Java
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>25.0-jre</version> </dependency>
Gson
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.4</version> </dependency>
Log4J Logging
Log4J-Core
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.0</version> </dependency>
Log4J-API
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.0</version> </dependency>
Maven Plugins
Please again note that the latest versions may change since the date I wrote this guide. I once again recommend searching for each plugin listed below on https://mvnrepository.com and simply copying the plugins from there and then pasting them into the <plugins> tag section in your project’s POM.xml file.
Do again make sure however, that you include all the specific information in my examples.
- In the same POM.xml file, add <plugins/> tags within added <build/> tags, and then add the following plugins within the <plugins/> tags. Again add all this outside of the <dependencies/> tags but within the <project/> tags
Maven-Surefire-Plugin
This plugin can be used for reporting and is also used in our test framework to configure where our test suites are run from (e.g. testng.xml)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin>
IntelliJ Plugins
Cucumber for Java Plugin
- Ensure ‘Cucumber for Java’ plugin is installed and enabled in IntelliJ by going to Settings –> Plugins and ensuring ‘Cucumber for Java’ is checked
- MacOS: IntelliJ IDEA –> Preferences –> Plugins
- Windows: File –> Settings –> Plugins
Create a .gitignore File
- In IntelliJ, right-click on your project root and select New –> File
- Name the file .gitignore and click OK
- Paste the following into your .gitignore file (this is a default template used for Maven projects)
log/ target/* .idea/ *.iml *.iws .DS_Store pom.xml.tag pom.xml.releaseBackup pom.xml.versionsBackup pom.xml.next release.properties dependency-reduced-pom.xml buildNumber.properties .mvn/timing.properties .envrc # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) !/.mvn/wrapper/maven-wrapper.jar
Now would be a good time to save the Maven project if you haven’t yet done so! 🙂
Packages & Directory Structure
- 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)
- pages
- steps
- utils
- drivers
- helpers
- hooks
- selenium
- Right-click on the {project-root}/src/test directory, select New –> Directory
- Name the directory ‘resources’ and click OK
- Right-click on the {project-root}/src/test/resources directory and select ‘Mark Directory As’ –> ‘Test Resources Root’ (if not marked already)
- 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
- 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)
- BaseScenarios.feature
Pages package
- Right-click the ‘pages’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘pages’ package will contain all our class files for our web pages / page objects, following Page Object Model (POM) principles. Please see https://goo.gl/4iq2Et for more information)
- Page
- BasePage
Steps package
The steps package will contain all our step definition classes, which will contain our step definitions / glue which links our steps in our test scenarios to the java methods which perform different actions.
When it comes to grouping our step definitions, it is good practice to have a Step Definition class file for each major domain object we are trying to test.
For example, in this blog series, we will mainly be testing that a user can search for “Reddit” on DuckDuckGo and then navigate to Reddit by selecting the appropriate search result.
So in this case, we could have the following Step Definition files…
- Right-click the ‘steps’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘steps’ package will contain all our step definitions, which acts as the glue that connects our BDD/Gherkin language scenarios in our Feature files, to our test methods in our Java classes. Please see https://cucumber.io/docs/gherkin/step-organization/ for more information)
- BaseSteps
- this class contains all the step definitions for your base scenarios and steps that apply across multiple domain objects
- SearchSteps
- this class contains all the step definitions for steps that perform actions around searching on DuckDuckGo
- RedditSteps
- this class contains all the step definitions for steps that involve Reddit
- BaseSteps
Utils package
Drivers package
- Right-click the ‘drivers’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘drivers’ package will contain class files for all the different WebDrivers our test framework will use)
- ChromeWebDriver
- FirefoxWebDriver
Helpers package
- Right-click the ‘helpers’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘helpers’ package will contain class files for all our static methods which can act as helpers to our existing functionality)
- WebDriverHelpers
- WebElementHelpers
Hooks package
- 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
- StepHooks
- TestRunHooks
Selenium package
- Right-click the ‘selenium’ package, select ‘New’ –> ‘Java Class’ and add the following classes to the package (the ‘selenium’ package will contain our core WebDriver setup and DriverController instance, as well as a Settings class where we can list public static variables to be used in our project)
- Driver
- DriverController
- Settings
