Part 4. Base API Methods, Settings & Hooks

Base API Methods, Settings & Hooks

Introduction

In this section, we will create our base methods to authenticate with the Twitter APIs via OAuth, as well as set the Base URL.  We will then create a @Before(“Api”) cucumber hook to perform certain functions before a test scenario or feature is run, when that feature or scenario has the “Api” tag.

Settings

Now that we have all the parameters and information we need to successfully OAuth 1 with the Twitter API, let’s add that information to our ‘Settings’ class.

Editing the ‘Settings’ Class

  1. Open up the ‘Settings’ class in the ‘utils.settings’ package and edit the class file to include public static final String variables for the following parameters…
    • BaseUrlthe base URL for our API tests (e.g. ‘https://api.twitter.com/1.1/statuses’ )
    • ConsumerKey – the Consumer Key value given to us by Twitter
    • ConsumerSecret – the Consumer Secret value given to us by Twitter
    • Token – the Access Token value given to us by Twitter
    • TokenSecret – the Access Token Secret value given to us by Twitter
      (Note that your values will be different to mine, they are unique to every different Twitter account)

      package utils.settings;
      
      public class Settings {
      
          public static final String baseUrl = "https://api.twitter.com/1.1/statuses";
          public static final String consumerKey = "<your-consumer-key>"; //replace with your consumer key
          public static final String consumerSecret = "<your-consumer-secret>"; // replace with your consumer secret
          public static final String accessToken = "<your-access-token>"; // replace with your access token
          public static final String accessTokenSecret = "<your-access-token-secret>"; // replace your access token secret
      }

Creating Base Methods

Instance Variables

  1. Open up the ‘BaseApiTests’ class file and add the following static instance variables (inside the class, outside the methods)…
    private static Response response;
    private static RequestSpecification httpRequest;
    private static String strResponse;

We will use these variables later in our API test methods.

  • Also make sure the correct libraries are imported…
    import io.restassured.response.Response;
    import io.restassured.specification.RequestSpecification;

setBaseUri() Method

In this method, we will get RestAssured to set its BaseUri to the baseUrl we set in our ‘Settings’ class.

  1. Open up the ‘BaseApiTests’ class file and add the following method inside the class. (The whole thing should look like below with the correct libraries imported)…
    package apis;
    
    import io.restassured.RestAssured;
    import io.restassured.response.Response;
    import io.restassured.specification.RequestSpecification;
    
    import static utils.settings.Settings.baseUrl;
    
    public class BaseApiTests {
    
        private static Response response;
        private static RequestSpecification httpRequest;
        private static String strResponse;
    
        public static void setBaseUri() {
            RestAssured.baseURI = baseUrl;
        }
    }
    

authTwitter() Method

In this method, we will use the 4 parameters in our ‘Settings’ class to authenticate with Twitter via OAuth.

  1. Add the following method into the ‘BaseApiTests’ class file…
    public static RequestSpecification authTwitter() {
            httpRequest = given().auth().oauth(consumerKey, consumerSecret, accessToken, accessTokenSecret);
            return httpRequest;
        }

    You will have to edit your imports to use all the listed variables in the ‘Settings’ class, as well as the static methods of the RestAssured library. Your ‘BaseApiTests’ class file should look like below…

    package apis;
    
    import io.restassured.RestAssured;
    import io.restassured.response.Response; 
    import io.restassured.specification.RequestSpecification; 
    
    import static utils.settings.Settings.baseUrl;
    import static io.restassured.RestAssured.*;
    import static utils.settings.Settings.*;
    
    public class BaseApiTests {
    
        public static void setBaseUri() {
            RestAssured.baseURI = baseUrl;
        }
    
        public static RequestSpecification authTwitter() {
            httpRequest = given().auth().oauth(consumerKey, consumerSecret, accessToken, accessTokenSecret);
            return httpRequest;
        }
    }

In our authTwitter() method, we set the return type to RequestSpecification, and then set the RequestSpecification instance variable of httpRequest to the OAuth1 method.  Finally we return it so we can use it in other methods and be authenticated.

Hooks

We will now add a ‘@Before’ hook in our ‘CucumberHooks’ class, to make our test scenarios execute the setBaseUri() method before a scenario and/or feature is run, if it has a certain tag (e.g. “@Api”). Please see https://github.com/cucumber/cucumber/wiki/Hooks for more information…

  1. Open up the ‘CucumberHooks’ class in the ‘utils.hooks’ package and edit the class file to match below…
    package utils.hooks;
    
    import apis.BaseApiTests;
    import io.cucumber.java.Before;
    
    public class CucumberHooks {
    
        @Before("@Api")
        public void setBaseUri() {
            BaseApiTests.setBaseUri();
        }
    }

    In the next section, we will start to create our test scenarios 🙂