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)

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

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

  • Also make sure the correct libraries are imported…

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

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…

    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…

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…

    In the next section, we will start to create our test 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.