1. Introduction
The objective of this tutorial is to demonstrate how to setup Robot Framework for testing an API and a screen, specifically using:
-
RESTinstance
-
Playwright
It is not a tutorial for Robot Framework or for these libraries, there are already many such tutorials available. Instead this tutorial aims to get you a running test from which you can experiment and expand your knowledge.
2. Installation
2.1. Install Node.js
Node.js is required to run Playwright. Install Node.js from https://nodejs.org/en/download/
2.2. Install Python
We’ll use the venv tool to create a Python Virtual Environment on Windows. That will isolate this project from other Python projects you may have, that require different (versions of) packages. Note that we will not use the Anaconda Python distribution, as it is missing several libraries.
2.2.1. Which Python version?
The RIDE editor recommends a particular version of Python. At the time of writing (2021-11-14) this is version 3.8.
We’ll be wanting to install a binary of library wxPython as a dependency of the RIDE integrated development editor. Check the latest version available on https://pypi.org/project/wxPython/#files. that matches the version for RIDE. E.g. Python version cp38 means Python 3.8.
2.2.2. Download and install
Head over to python.org:
-
Click on Downloads > Windows.
-
Choose the Python 3 release as chosen above.
-
Choose the Windows Installer (64-bit).
-
Run the downloaded installer
-
Select option "Add Python to Path"
-
Click on Install Now
-
If your Windows installation is limited to 260 characters in the path, you’ll see an option to "Disable path length limit". Choose this option.
-
To verify the installation, open a command window and run:
python --version
2.3. Virtual Environment
A virtual environment is stored in a directory, with the name of the virtual environment.
We’ll use c:\Users\USERNAME\python_venv
to store the environments. Our environment for Robot is named robot_env
.
C:\> cd Users\USERNAME C:\Users\USERNAME> md python_venv C:\Users\USERNAME> cd python_venv C:\Users\USERNAME\python_venv> python -m venv robot_env
You will now have directory named robot_env
.
2.4. Activate Environment
You will need to activate the environment each time you start work. A batch file is recommended for this. We’ll get to this later. For now:
C:\Users\USERNAME\python_venv> robot_env\Scripts\activate
Once the environment is activated, the command prompt will show as:
(robot_env) C:\Users\USERNAME\python_venv>
Some of the subsequent libraries install more easily with wheel, so enter this command:
pip install wheel
2.5. Install Robot Framework
At a command prompt as above, run:
pip install robotframework
To check that the installation was successful, run
robot --version
Now we’ll also install the libraries required for our tutorial:
pip install restinstance pip install robotframework-browser
To install the node dependencies, run:
rfbrowser init
2.6. Install RIDE
RIDE is the IDE for Robot Framework. At the time of writing, we are using Python 3.8 and will be installing the 2.x pre-release.
We’re still at a command prompt looking like this:
(robot_env) C:\Users\USERNAME\python_venv>
Run these commands:
pip install wxPython pip install psutil pip install --pre robotframework-ride
3. Create folder structure
Create a project folder, let’s call it acmeTests
. Within this folder, create the following 3 folders:
resources results tests
In the project folder, create a script:
@echo off C:\%HOMEPATH%\python_venv\robot_env\Scripts\activate
4. API Testing using RESTinstance
You will find many references on the web for API testing that use the requests
library. A more modern approach is however to use RestInstance. This page also has a great tutorial to follow.
We’ll be using https://jsonplaceholder.typicode.com/ as the site against we will run our tests.
Copy the test script from https://pypi.org/project/RESTinstance/ into tests/api-test.robot
.
*** Settings ***
Library REST https://jsonplaceholder.typicode.com
Documentation Test data can be read from variables and files.
... Both JSON and Python type systems are supported for inputs.
... Every request creates a so-called instance. Can be `Output`.
... Most keywords are effective only for the last instance.
... Initial schemas are autogenerated for request and response.
... You can make them more detailed by using assertion keywords.
... The assertion keywords correspond to the JSON types.
... They take in either path to the property or a JSONPath query.
... Using (enum) values in tests optional. Only type is required.
... All the JSON Schema validation keywords are also supported.
... Thus, there is no need to write any own validation logic.
... Not a long path from schemas to full Swagger/OpenAPI specs.
... The persistence of the created instances is the test suite.
... Use keyword `Rest instances` to output the created instances.
*** Variables ***
${json} { "id": 11, "name": "Gil Alexander" }
&{dict} name=Julie Langford
*** Test Cases ***
GET an existing user, notice how the schema gets more accurate
GET /users/1 # this creates a new instance
Output schema response body
Object response body # values are fully optional
Integer response body id 1
String response body name Leanne Graham
[Teardown] Output schema # note the updated response schema
GET existing users, use JSONPath for very short but powerful queries
GET /users?_limit=5 # further assertions are to this
Array response body
Integer $[0].id 1 # first id is 1
String $[0]..lat -37.3159 # any matching child
Integer $..id maximum=5 # multiple matches
[Teardown] Output $[*].email # outputs all emails as an array
POST with valid params to create a new user, can be output to a file
POST /users ${json}
Integer response status 201
[Teardown] Output response body ${OUTPUTDIR}/new_user.demo.json
PUT with valid params to update the existing user, values matter here
PUT /users/2 { "isCoding": true }
Boolean response body isCoding true
PUT /users/2 { "sleep": null }
Null response body sleep
PUT /users/2 { "pockets": "", "money": 0.02 }
String response body pockets ${EMPTY}
Number response body money 0.02
Missing response body moving # fails if property moving exists
PATCH with valid params, reusing response properties as a new payload
&{res}= GET /users/3
String $.name Clementine Bauch
PATCH /users/4 { "name": "${res.body['name']}" }
String $.name Clementine Bauch
PATCH /users/5 ${dict}
String $.name ${dict.name}
DELETE the existing successfully, save the history of all requests
DELETE /users/6 # status can be any of the below
Integer response status 200 202 204
Rest instances ${OUTPUTDIR}/all.demo.json # all the instances so far
Run on the command line:
C:\acmeTests> activate (robot_env) C:\acmeTests> robot --outputdir results tests/api-test.robot
The test results can be found in the results
folder.
5. Using RIDE
Start the RIDE editor as follows:
(robot_env) C:\acmeTests> ride.py
6. UI testing using Browser Library
The Browser Library is powered by Playwright and more modern than Selenium.
We’ll start with a really simple test. Create this file:
*** Settings ***
Library Browser
*** Test Cases ***
Example Test
New Page https://playwright.dev
Get Text h1 contains Playwright
Run on the command line:
C:\acmeTests> activate (robot_env) C:\acmeTests> robot --outputdir results tests/ui-test.robot
7. Conclusion
You now have your environment up and running. View the extensive reference materials and the many tutorials to master Robot Framework.