Using 3rd party libraries: API testing example

Below is a little walk-through how to utilize 3rd party libraries in Copado Robotic Testing. In this particular example we will be validating response from simple REST API using RequestsLibrary.

There are other libraries and ways of doing REST API testing in Robot Framework. RequestsLibrary is used just as an example. Other notable libraries for doing REST API testing are for example:

Use case

We want to use OpenBooksLib /books api endpoint to query and validate specific book using ISBN.

The request should be done in this kind of format:

https://openlibrary.org/api/books?bibkeys=ISBN:0451526538

…​and it will return book details in json format.

Test data

Let’s expect that our api should take ISBN 0201558025 as an input and we should verify the following info from the response:

  • Book title: Concrete mathematics

  • Published: 1994

  • Author: Ronald L. Graham

Creating test script

Installing RequestsLibrary

RequestsLibrary is already installed to Copado Robotic Testing. If you try this locally, please run

pip install robotframework-requests

Importing Library

Add the following at the top of your test script:

Library   RequestsLibrary

Getting data

The first thing we want to do is call the api and check that we get valid data as a response. Let’s worry about parsing data later.

The basic idea is:

  • We create a session using Create Session

  • We populate query and parameters

  • We call a specific endpoint with our query using Get Request

  • We get a response to ${resp} variable

  • We check the status using ${resp.status_code}

  • We log the whole response using ${resp.text}

I’m not going through openbooks api usage but just show the example code here. If you want to know all the details how to use this specific api, please refer here.

Open Library: Get a book based on ISBN 10 (GET)
    [Documentation]             Testing openlibrary public api - getting books via ISBN10
    ${var}=                     Create Session              openbookslib                http://openlibrary.org/api
    ${query}=                   Set Variable                    bibkeys=ISBN:0201558025
    &{params}=                  Create Dictionary           format=json                 jscmd=data
    ${resp} =                   Get Request                 openbookslib                /books?${query}                      params=&{params}
    Should Be Equal As Strings                              ${resp.status_code}         200
    Log                         ${resp.text}

If you run this and check the logs, you should see valid book information in json format attached to logs.

Parsing json response

For parsing JSON reponse we will create a custom helper keyword. Create a file keywords.robot under resources folder. Add the following content there:

*** Settings ***
Library                         Collections
*** Variables ***

*** Keywords ***
Get Field Value From Json
    [Documentation]        The value of json argument expected to be one dictionary
    [Arguments]            ${json}                ${field_name}
    ${object} =            Evaluate               json.loads("""${json}""")
    Log                    ${object}
    ${fieldValue} =        Get From Dictionary    ${object}                   ${field_name}
    [Return]               ${fieldValue}

Import your resource file in your test script file:

Resource                        ../resources/keywords.robot

Then let’s put the expected data to variables. Add the following to your test script file under *Variables* section:

*** Variables ***
${ISBN_10}                      0201558025
${EXPECTED_TITLE}               Concrete mathematics
${EXPECTED_YEAR}                1994
${EXPECTED_AUTHOR}              Ronald L. Graham

Then we use our helper keyword to turn json object to robot fw (python) dictionary:

${book_info}=               Get Field Value From Json                               ${resp.text}                ISBN:${ISBN_10}

After that we can get individual items from the response like this:

${title}=                   Get From Dictionary         ${book_info}                title

So, our full test case will be:

*** Test Cases ***
Open Library: Get a book based on ISBN 10 (GET)
    [Documentation]             Testing openlibrary public api - getting books via ISBN10
    ${var}=                     Create Session              openbookslib                http://openlibrary.org/api
    ${query}=                   Set Variable                bibkeys=ISBN:${ISBN_10}
    &{params}=                  Create Dictionary           format=json                 jscmd=data
    ${resp} =                   Get Request                 openbookslib                /books?${query}                      params=&{params}
    Should Be Equal As Strings                              ${resp.status_code}         200
    Log                         ${resp.text}

    # parse returned data to variables
    ${book_info}=               Get Field Value From Json                               ${resp.text}                ISBN:${ISBN_10}
    Log                         ${book_info}
    ${title}=                   Get From Dictionary         ${book_info}                title
    ${published}=               Get From Dictionary         ${book_info}                publish_date
    ${authors}=                 Get From Dictionary         ${book_info}                authors
    Log                         ${authors}
    ${main_author_name}=        Get From Dictionary         ${authors[0]}               name

    # Verify returned information against known values
    Should Be Equal As Strings                              ${title}                    ${EXPECTED_TITLE}
    Should Be Equal As Strings                              ${published}                ${EXPECTED_YEAR}
    Should Be Equal As Strings                              ${main_author_name}         ${EXPECTED_AUTHOR}