RESTinstance library makes it easy to test and interact with JSON REST API web services.
Let's start learning it by importing the library and configuring it to test our example service: a Plone CMS demo site hosted at http://plonedemo.kitconcept.com:
*** Settings ***
Library REST https://plonedemo.kitconcept.com
Because Plone requires explicit Accept: application/json
header to route reuqest to its REST API, we define user keyword Set default headers as Suite Setup to set that header for REST library calls:
*** Settings ***
Library BuiltIn
Suite Setup Set default headers
*** Keywords ***
Set default headers
${headers}= Create dictionary
... Accept=application/json
Set headers ${headers}
Now we are ready to start calling the REST library by:
This first test should PASS, because English language path should return en
as its language:
*** Test Cases ***
GET English portal root
GET /en
String response body language en
Output response body language
This second test should FAIL, because German language path should return de
as its language, not en
:
*** Test Cases ***
GET portal root
GET /de
String response body language en
============================================================================== Jupyter ============================================================================== GET portal root | FAIL | 'de' is not one of ['en'] Failed validating 'enum' in schema: {'default': 'de', 'enum': ['en'], 'type': 'string'} On instance: 'de' ------------------------------------------------------------------------------ Jupyter | FAIL | 1 critical test, 0 passed, 1 failed 1 test total, 0 passed, 1 failed ============================================================================== Output: /run/user/1000/tmpu6rrygt2/output.xml
Calling mutable API at plonedemo.kitconcept.com
requires authentication with the service.
To allow REST library to test authenticated methods, we need to login to the service.
Here we define a new user keyword to use SeleniumLibrary to login and retrieve a valid session cookie:
*** Settings ***
Library SeleniumLibrary
Suite teardown Close all browsers
*** Variables ***
${API} http://plonedemo.kitconcept.com
*** Keywords ***
Get auth token
Open browser ${API}/en/login browser=headlessfirefox
Page should contain Login as Editor
Click link Login as Editor
Page should contain You are now logged in
${cookie}= Get cookie __ac
[return] ${cookie.value}
Next we can redefine our Suite Setup to also set the session cookie for REST library request headers:
*** Settings ***
Library BuiltIn
Suite setup Set default headers
*** Keywords ***
Set default headers
${token}= Get auth token
${headers}= Create dictionary
... Accept=application/json
... Cookie=__ac=${token}
Set headers ${headers}
And finally, as we are now logged in, we can start testing the creation of new pages on Plone:
*** Settings ***
Library String
*** Keywords ***
Create new page
${payload}= Create dictionary
... @type=Document
... title=Hello World
... text=<p>Here be dragons!</p>
POST /en ${payload}
Output
Integer response status 201
${url}= Output response body @id
${url}= Strip string ${url} characters="
[return] ${url}
*** Test Cases **
Can view the new page
${url}= Create new page
Go to ${url}
Page should contain Here be dragons!
Set window size 800 600
Capture page screenshot