Getting Started

Introduction

In this series, we will setup an automation framework scratch to test against REST APIs.

The completed repository can be found at https://gitlab.com/testifyqa/Java-Web-RestAssured-Maven

We will use the following in our framework:

  • Cucumber
    • Cucumber is a software tool used by computer programmers for testing other software. It runs automated acceptance tests written in a behaviour-driven development style. Central to the Cucumber BDD approach is its plain language parser called Gherkin.
  • TestNG
    • TestNG is a testing framework for the Java programming language, inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities. It is similar to JUnit but has extra capabilities.
  • RestAssured
    • REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, maintainable tests for RESTful APIs.

NOTE: If you have followed and completed the blog series on Java Web Automation, then all you need to do is add the Maven dependencies for the latest version of RestAssured and Scribe v2.5.3 specifically, (due to using OAuth 1 in this guide) and the rest of this blog post can be skipped…

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.github.scribejava</groupId>
    <artifactId>scribejava-apis</artifactId>
    <version>2.5.3</version>
</dependency>

If you are starting from scratch, then continue reading below…

Setting Up Java

Java JDK

The Java Development Kit is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle.

  1. Download the Java JDK appropriate for your Operating System / Architecture from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  2. Follow the wizard and complete the installation of the Java JDK

Setting Up Java Environment Variables

An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer.  Because every developer on a team will have a slightly different Development environment on there local machine, using environment variables is good practice to help easily find and use needed processes and/or paths in an environment.

MacOS:

  1. Open up Terminal and type the following (exclude typing the $’s)…
    $ nano ~/.bash_profile
  2. Add the following lines to your .bash_profile file within Terminal to set the $JAVA_HOME environment variable…
    export JAVA_HOME=$(/usr/libexec/java_home)
    export PATH=$JAVA_HOME/bin:$PATH
  3. Save .profile and exit nano editor by pressing (in order, one at a time)…
    Ctrl + O
    [Return]
    Ctrl + X
  4. Completely quit out of Terminal and restart it to launch a fresh Terminal window with your changes applied
  5. Confirm you have correctly set the $JAVA_HOME environment variable by typing in Terminal…
    $ java version

Windows:

  1. Click ‘Start’ and search for “System”
  2. Go to ‘Advanced System Settings’ –> ‘Advanced’ –> ‘Environment Variables’
  3. Under the ‘System Variables’ section, click ‘New’ and then add and set the following new System Variable…
    1. Variable Name:  JAVA_HOME
    2. Variable Value:  C:\Program Files\jdk1.#.#_### (replace with directory path of where JDK was installed)
  4. Double-click on ‘Path’ in the System Variables pane
  5. Click ‘New’ and add and set the following PATH environment variable…
    C:\Program Files\jdk1.#.#_###\bin ({jdk-root-directory-path}\bin)
  6. Click ‘OK’ where applicable to apply your changes
  7. Confirm you have correctly set the $JAVA_HOME environment variable by opening CMD Prompt and entering…
    $ java version (exclude typing the $)

Setting Up Maven

Maven is a build automation tool used primarily for Java projects, to manage its dependencies and any external libraries and packages the Java project may need.  In Yiddish, the word maven means “accumulator of knowledge”.

Download Maven Binaries

  1. Download the Maven binaries from https://maven.apache.org/download.cgi
    1. If on Windows, download the zip file
    2. If on MacOS, download the tar.gz file
  2. Create a root directory for all your Framework related downloads (e.g. ‘C:\FrameworkSetup’ or ‘~/Users/username/FrameworkSetup’)
  3. Extract the downloaded Maven binaries and place the extracted Binaries directory inside the root directory you just created

Setting Up Maven Environment Variables

MacOS:

  1. Open up Terminal and type the following (exclude typing the $’s)…
    $ nano ~/.bash_profile
  2. Add the following lines to your .bash_profile file within Terminal to set the $M2_HOME (Maven) environment variable…
    export M2_HOME=/Users/username/FrameworkSetup/apachemaven#.#.# (replace directory with where you extracted Maven binaries to on your machine)
    export PATH=$JAVA_HOME/bin:$PATH
  3. Save .profile and exit nano editor by pressing (in order, one at a time)…
    Ctrl + O
    [Return]
    Ctrl + X
  4. Completely quit out of Terminal and restart it to launch a fresh Terminal window with your changes applied
  5. Confirm you have successfully added and set the environment variable for Maven by entering…
    $ source ~/.bash_profile (exclude the $)
    $ echo $M2_HOME (exclude the $)

Setting Up IntelliJ IDEA

IntelliJ IDEA is a Java IDE for developing computer software.  It is developed by JetBrains, and is available as a free community edition, or as a paid commercial edition. For the purposes of this blog, the free, community edition is fine ?

  1. Download IntelliJ IDEA from https://www.jetbrains.com/idea/download
  2. Complete installation of IntelliJ IDEA
  3. Launch IntelliJ IDEA and choose to not import any settings (unless you have appropriate ones already)
  4. Select your UI theme and click ‘Next’
  5. Select your keymap scheme and click ‘Next’
  6. Keep clicking ‘Next’ without changing anything on the following screens in the wizard
  7. Click ‘Start using IntelliJ IDEA’

Framework Setup

Creating the Maven Project

  1. Launch IntelliJ and select ‘Create New Project’
  2. Select ‘Maven’ in the left pane
    1. Select the appropriate Java JDK in the ‘Project SDK’ dropdown at the top (should be the one installed previously)
    2. Click ‘Next’
  3. In the ‘GroupId’ field, enter an appropriate name (e.g. “com.producttestframework”)
  4. In the ‘ArtifactId’ field, input an appropriate name (e.g. “AutomationFramework”)
  5. Click ‘Next’
  6. Give an appropriate Project Name (e.g. “ProductAutomation”) and an appropriate project location on your local machine (e.g. ~/Users/username/DevProjects/ProductAutomation)
  7. Click ‘Finish’
  8. 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)
    1. 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 ?

  1. Open up the POM.xml file and add the following dependencies within the <dependencies> tag section.

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.

Also make sure that Scribe is v2.5.3 specifically, as this specific version is needed by RestAssured when working with OAuth 1.

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>
</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>

RestAssured Dependencies

RestAssured
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>
Scribe

This is required by RestAssured when using OAuth 1 specifically. Please use v2.5.3 only, to avoid running into errors…

<dependency>
    <groupId>com.github.scribejava</groupId>
    <artifactId>scribejava-apis</artifactId>
    <version>2.5.3</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.

  1. In the same POM.xml file, add <plugins/> tags within <build/> tags, and then add the following plugins within the <plugins/> tags…

Maven-Resources-Plugin

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-resources-plugin</artifactId>
   <version>2.4</version>
</plugin>

Maven-Surefire-Plugin

<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>

Maven-Cucumber-Reporting

<plugin>
   <groupId>net.masterthought</groupId>
   <artifactId>maven-cucumber-reporting</artifactId>
   <version>3.8.0</version>
   <executions>
       <execution>
           <id>execution</id>
           <phase>verify</phase>
           <goals>
               <goal>generate</goal>
           </goals>
           <configuration>
               <projectName>ProductApiAutomationFramework</projectName>  <!-- Replace with project name -->
               <outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory>
               <cucumberOutput>target/cucumber-reports/CucumberTestReport.json</cucumberOutput>
               <buildNumber>1</buildNumber>
               <parallelTesting>false</parallelTesting>
           </configuration>
       </execution>
   </executions>
</plugin>

Maven-Compiler-Plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

IntelliJ Plugins

Cucumber for Java Plugin

  1. Ensure ‘Cucumber for Java’ plugin is installed and enabled in IntelliJ by going to Settings –> Plugins and ensuring ‘Cucumber for Java’ is checked
    1. MacOS: IntelliJ IDEA –> Preferences –> Plugins
    2. Windows: File –> Settings –> Plugins

Create a .gitignore File

  1. In IntelliJ, right-click on your project root and select New –> File
    1. Name the file .gitignore and click OK
  2. 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! In the next part, we will create the structure for our packages and directories ?

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

Next Article

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.