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

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

testCompile group: 'io.rest-assured', name: 'rest-assured', version:'3.1.0'
compile group: 'com.github.scribejava', name: 'scribejava-apis', version: '5.5.0'

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 Gradle

Gradle is an open-source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.  Gradle uses a directed acyclic graph (“DAG”) to determine the order in which tasks can be run.

Install Gradle

MacOS:

Install HomeBrew (Package Manager)

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system.

  1. Go to https://brew.sh/
  2. Copy the command and paste it into Terminal…
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Gradle
  1. Open up Terminal and enter…
    $ brew install gradle

    (without the $)

Windows

Install Chocolatey (Package Manager)
  1. Open up a command prompt (cmd.exe) with administrative privileges
  2. Enter in the following command…
    @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Install Gradle
  1. Open up a command prompt (cmd.exe) with administrative privileges again
  2. Enter in the following command…
    choco install gradle

Setting Up Gradle 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 PATH=$PATH:/opt/gradle/gradle-4.9/bin
  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

Windows:

  1. Click ‘Start’ and search for “System”
  2. Go to ‘Advanced System Settings’ –> ‘Advanced’ –> ‘Environment Variables’
  3. Double-click on ‘Path’ in the System Variables pane
  4. Set and append the following PATH environment variable…
    C:\Gradle\gradle-4.9\bin;

    ({gradle-directory-path}\bin)

  5. Click ‘OK’ where applicable to apply your changes
  6. Confirm you have correctly set the Gradle environment variable by opening CMD Prompt and entering…
    $ gradle -v

    (exclude typing 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 Gradle Project

  1. Launch IntelliJ and select ‘Create New Project’
  2. Select ‘Gradle’ in the left pane
    • Select the appropriate Java JDK in the ‘Project SDK’ dropdown at the top (should be the one installed previously.  For purposes of this guide and for simplicity reasons, we are using Java 8 JDK)
      • Make sure ‘Java’ is check in the right pane
    • 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. Check the ‘Use auto-import’ checkbox
  7. Ensure the ‘Use default gradle wrapper (recommended)’ radio button is selected
  8. Ensure ‘Use Project JDK’ is selected for the Gradle JVM
  9. Click ‘Next’
  10. Give an appropriate Project Name (e.g. “ProductAutomation”) and an appropriate project location on your local machine (e.g. ~/Users/username/DevProjects/ProductAutomation)
  11. Click ‘Finish’
  12. Advance to IntelliJ IDEA –> Preferences/Settings –> Build, Execution, Deployment –> Build Tools –> Gradle
    • Check the ‘Create directories for empty content roots automatically’ checkbox
      • This will create your src/main and src/test sub-directories and resources for you
  13. Click ‘Apply’
  14. Click ‘OK’

Gradle Dependencies

You can manage all your projects dependencies, plugins and packages by simply adding them into your Gradle project’s build.gradle file ?

  1. Open up the build.gradle file and add the following dependencies dependencies task 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
testCompile group: 'io.cucumber', name: 'cucumber-java', version: '4.8.0'
Cucumber-JVM-Deps
testCompile group: 'io.cucumber', name: 'cucumber-jvm-deps', version:'1.0.6'
Cucumber-TestNG
compile group: 'io.cucumber', name: 'cucumber-testng', version:'4.8.0'
Cucumber-Reporting
compile group: 'net.masterthought', name: 'cucumber-reporting', version:'3.19.0'

TestNG Dependencies

TestNG
testCompile group: 'org.testng', name: 'testng', version:'6.14.3'

RestAssured Dependencies

RestAssured
testCompile group: 'io.rest-assured', name: 'rest-assured', version:'3.1.0'
Scribe

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

compile group: 'com.github.scribejava', name: 'scribejava-apis', version: '5.5.0'

Tell Gradle to use TestNG.xml File

Later in this blog series, we will create a testng.xml file which we use to run our different test suites, defined for example in our TestRunner class that we will also make.  We need to specifically tell Gradle to use our testng.xml file for test classes and ordering, so lets configure a test suite (xml) to be executed in a Gradle task ?

  1. Add the following TestNG task below the dependencies section in your build.gradle file…
    test {
        useTestNG() {
            suites 'testng.xml'
        }
    }

When finished, the build.gradle file should look similar to below…

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.testify.qa'
version = '1.0-SNAPSHOT'

description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8



repositories {
        
     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    compile group: 'io.cucumber', name: 'cucumber-testng', version:'4.8.0'
    compile group: 'net.masterthought', name: 'cucumber-reporting', version:'3.8.0'
    compile group: 'com.github.scribejava', name: 'scribejava-apis', version:'2.5.3'
    testCompile group: 'io.cucumber', name: 'cucumber-java', version:'4.8.0'
    testCompile group: 'io.cucumber', name: 'cucumber-jvm-deps', version:'1.0.6'
    testCompile group: 'org.testng', name: 'testng', version:'6.9.8'
    testCompile group: 'io.rest-assured', name: 'rest-assured', version:'3.1.0'
}

test {
    useTestNG() {
        suites 'testng.xml'
    }
}

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 Gradle projects)
    log/
    target/*
    .idea/
    .DS_Store
    release.properties
    buildNumber.properties
    .gradle/
    build/
    
    # Ignore Gradle GUI config
    gradle-app.setting
    
    # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
    !gradle-wrapper.jar
    
    # Cache of project
    .gradletasknamecache
    
    # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
    # gradle/wrapper/gradle-wrapper.properties

    Now would be a good time to save the Gradle 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!

Previous Article

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.