QuickStart demo project for Robot Framework

Introduction

The official quick start guide for RobotFramework can be cloned from github

Documentation

In this project you are invited to study tests for the script login.py , which simulates logging into an application. The script is written in Python and uses text files to simulate a database

The tests are written, of course, in Robot Framework, but in the reStructuredText format, which is plain text with inserts of robot code. Tests can be run from such files only if the docutils library is installed in addition to robot.

The .rst looks approximately like this:

Text text text .. code:: robotframework code code Text text text

Convenient for writing documentation. Extremely inconvenient when you need to read code. Perhaps those who use this format know some tricks unknown to me. If you are one of them, write in telegram .

Below is a code fragment QuickStart.rst

Data-driven tests ----------------- Quite often several test cases are otherwise similar but they have slightly different input or output data. In these situations *data-driven tests* allows varying the test data without duplicating the workflow. With Robot Framework the `[Template]` setting turns a test case into a data-driven test where the template keyword is executed using the data defined in the test case body: .. code:: robotframework *** Test Cases *** Invalid password [Template] Creating user with invalid password should fail abCD5 ${PWD INVALID LENGTH} abCD567890123 ${PWD INVALID LENGTH} 123DEFG ${PWD INVALID CONTENT} abcd56789 ${PWD INVALID CONTENT} AbCdEfGh ${PWD INVALID CONTENT} abCD56+ ${PWD INVALID CONTENT} In addition to using the `[Template]` setting with individual tests, it would be possible to use the `Test Template` setting once in the settings table like `setups and teardowns`_ defined later in this guide. In our case that

For writing documentation this is quite convenient, but looking for pieces of tests in a document full of ordinary text is unpleasant.

Clone the repository

More about the git clone command you can find out here

git clone https://github.com/robotframework/QuickStartGuide.git

These tests are written with reStructuredText , so docutils has to be installed.

python -m pip install docutils

Study the contents of the downloaded directory:

cd QuickStartGuide
ll

drwxr-xr-x 5 andrei urnsu 4096 May 5 11:25 ./ drwxr-xr-x 5 andrei urnsu 4096 May 5 11:25 ../ -rw-r--r-- 1 andrei urnsu 796 May 5 11:25 BUILD.rst drwxr-xr-x 8 andrei urnsu 4096 May 5 11:25 .git/ drwxr-xr-x 2 andrei urnsu 4096 May 5 11:25 lib/ -rw-r--r-- 1 andrei urnsu 21237 May 5 11:25 QuickStart.rst -rw-r--r-- 1 andrei urnsu 359 May 5 11:25 README.rst drwxr-xr-x 2 andrei urnsu 4096 May 5 11:25 sut/

I recommend creating and activating in advance a Python virtual environment in which you then install Robot Framework according to the instructions

python -m venv venv
source venv/Scripts/activate
python -m pip install --upgrade pip
python -m pip install robotframework
python -m pip install docutils

Project structure

QuickStartGuide/ |-- BUILD.rst |-- QuickStart.rst |-- README.rst |-- lib | `-- LoginLibrary.py `-- sut `-- login.py 2 directories, 5 files

Demonstration

The project for testing the login is in the directory sut

The script is called login.py

python sut/login.py create andrei HeiHeiru1

SUCCESS

python sut/login.py login andrei HeiHeiru1

Logged In

robot QuickStart.rst

============================================================================== QuickStart ============================================================================== User can create an account and log in | PASS | ------------------------------------------------------------------------------ User cannot log in with bad password | PASS | ------------------------------------------------------------------------------ User can change password | PASS | ------------------------------------------------------------------------------ Invalid password | PASS | ------------------------------------------------------------------------------ User status is stored in database | PASS | ------------------------------------------------------------------------------ QuickStart | PASS | 5 tests, 5 passed, 0 failed ============================================================================== Output: /home/andrei/robotframework/QuickStartGuide/output.xml Log: /home/andrei/robotframework/QuickStartGuide/log.html Report: /home/andrei/robotframework/QuickStartGuide/report.html

Banner image

Without .rst

Since I really dislike reading code from a mishmash in a QuickStart.rst file, I rewrote everything into a normal .robot file.

Now the project structure looks like this:

QuickStartGuide/ |-- BUILD.rst |-- QuickStart.robot |-- QuickStart.rst |-- README.rst |-- lib | `-- LoginLibrary.py `-- sut `-- login.py 2 directories, 6 files

The result is the following original file

*** Settings *** Library OperatingSystem Library lib/LoginLibrary.py Suite Setup Clear Login Database Test Teardown Clear Login Database Force Tags quickstart Default Tags example smoke *** Variables *** ${USERNAME} janedoe ${PASSWORD} J4n3D0e ${NEW PASSWORD} e0D3n4J ${DATABASE FILE} ${TEMPDIR}${/}robotframework-quickstart-db.txt ${PWD INVALID LENGTH} Password must be 7-12 characters long ${PWD INVALID CONTENT} Password must be a combination of lowercase and ... uppercase letters and numbers *** Test Cases *** User can create an account and log in Create Valid User fred P4ssw0rd Attempt to Login with Credentials fred P4ssw0rd Status Should Be Logged In User cannot log in with bad password Create Valid User betty P4ssw0rd Attempt to Login with Credentials betty wrong Status Should Be Access Denied User can change password Given a user has a valid account When she changes her password Then she can log in with the new password And she cannot use the old password anymore Invalid password [Template] Creating user with invalid password should fail abCD5 ${PWD INVALID LENGTH} abCD567890123 ${PWD INVALID LENGTH} 123DEFG ${PWD INVALID CONTENT} abcd56789 ${PWD INVALID CONTENT} AbCdEfGh ${PWD INVALID CONTENT} abCD56+ ${PWD INVALID CONTENT} User status is stored in database [Tags] variables database Create Valid User ${USERNAME} ${PASSWORD} Database Should Contain ${USERNAME} ${PASSWORD} Inactive Login ${USERNAME} ${PASSWORD} Database Should Contain ${USERNAME} ${PASSWORD} Active *** Keywords *** Clear login database Remove file ${DATABASE FILE} Create valid user [Arguments] ${username} ${password} Create user ${username} ${password} Status should be SUCCESS Creating user with invalid password should fail [Arguments] ${password} ${error} Create user example ${password} Status should be Creating user failed: ${error} Login [Arguments] ${username} ${password} Attempt to login with credentials ${username} ${password} Status should be Logged In # Keywords below used by higher level tests. Notice how given/when/then/and # prefixes can be dropped. And this is a comment. A user has a valid account Create valid user ${USERNAME} ${PASSWORD} She changes her password Change password ${USERNAME} ${PASSWORD} ${NEW PASSWORD} Status should be SUCCESS She can log in with the new password Login ${USERNAME} ${NEW PASSWORD} She cannot use the old password anymore Attempt to login with credentials ${USERNAME} ${PASSWORD} Status should be Access Denied Database Should Contain [Arguments] ${username} ${password} ${status} ${database} = Get File ${DATABASE FILE} Should Contain ${database} ${username}\t${password}\t${status}\n

Note that obsolete settings are used here Force Tags and Default Tags , which should really be replaced with Test Tags

*** Settings *** … Test Tags quickstart example smoke … *** Test Cases *** User status is stored in database [Tags] variables database -example -smoke

Under the hood

How these tests work. Let us go through them in order.

The test User can create an account and log in uses the keyword Create valid user which calls the method create_user() from LoginLibrary.py

*** Test Cases *** User can create an account and log in Create Valid User fred P4ssw0rd Attempt to Login with Credentials fred P4ssw0rd Status Should Be Logged In &hellip *** Keywords *** … Create valid user [Arguments] ${username} ${password} Create user ${username} ${password} Status should be SUCCESS

The method create_user() calls the method _run_command()

# lib/LoginLibrary.py import os.path import subprocess import sys class LoginLibrary(object): def __init__(self): self._sut_path = os.path.join(os.path.dirname(__file__), '..', 'sut', 'login.py') self._status = '' def create_user(self, username, password): self._run_command('create', username, password) # … def _run_command(self, command, *args): command = [sys.executable, self._sut_path, command] + list(args) process = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) self._status = process.communicate()[0].strip()

The method _run_command() uses the libraries sys and subprocess

I recommend studying my article about subprocess here

Let us examine what happens in the _run_command method while the keyword works Create Valid User from the test User can create an account and log in

The first line:

def _run_command(self, command, *args):

self - this is an instance of the LoginLibary class.

In it, during initialisation, the attribute self._sut_path (type str) holds the path to the script login.py

And the status self._status, currently equal to an empty string

command - this is the string 'create', which we got from the method create_user()

*args - these are the strings fred and P4ssw0rd which as username and password we got from the method create_user(), which got them from the keyword Create valid user

Details about how *args and **kwargs work you can read in the article *args **kwargs Python on my site devhops.ru

The second line:

command = [sys.executable, self._sut_path, command] + list(args)

Now the variable command contains a list of the following elements:

The path to python

The path to login.py

"create"

"fred"

"P4ssw0rd"

['C:\venv\Scripts\python.exe', 'C:\QuickStartGuide\lib\..\sut\login.py', 'login', 'fred', 'P4ssw0rd']

Article author: Andrei Olegovich

Banner image