Live interactive debugging with Robot framework
Recently while refreshing python and Robot Framework skills, I came across one very old problem which almost every engineer faces in the testing and development team that is live debugging with robot framework. So liked to write about this.
For doing live debugging with the robot framework, one requires to install 'debug library' in their shell. If this is already installed, then one doesn't need to do the same again. To install the library in your shell, use the following command :
pip install robotframework-debuglibrary
How to Debug: To debug your code one needs to call 'Library / DebugLibrary' in your robot file. After that, wherever in the testcase you want to start live debugging you can give the 'Debug / Debug If' keyword. For example:
*** Settings ***
Library DebugLibrary
** test case **
SOME TEST
# some keywords...
Debug
# some else...
Start Test
*** Keywords ***
$first_variable = evaluate t['resources']['${dut}']['${model}']
When execution will reach to Debug command it will provide you a prompt where you can start live debugging. For example:
==============================
IGMP Groups are removed
now the testcase will start
Verifying the various precheck conditions ..
>>>>> Enter interactive shell
Only accepted plain text format keyword separated with two or more spaces.
Type "help" for more information.
>
=======================================
One can supply different keywords or Robot framework command and can check for the output to debug live. For example:
> Log to Console This is debugging shell ######## Log command is used to print ouput on console
This is debugging shell
> Start Traffic And Wait For Traffic To Start. #### keyword for starting traffic was used to start traffic on ixia
Starting the traffic
Traffic Applied, lets start the traffic
0 iteration: Complete Traffic status is {'status': '1', 'stopped': '1'}
Actual Traffic status is 1
1 iteration: Complete Traffic status is {'status': '1', 'stopped': '1'}
Recommended by LinkedIn
Actual Traffic status is 1
2 iteration: Complete Traffic status is {'status': '1', 'stopped': '1'}
Actual Traffic status is 1
3 iteration: Complete Traffic status is {'status': '1', 'stopped': '1'}
Actual Traffic status is 1
4 iteration: Complete Traffic status is {'status': '1', 'stopped': '0'}
The actual Traffic status is 0
>. ##### after completion of command it will come back to > prompt in debugging mode.
Similarly, a related tester or debugger can call different libraries which may be missing on the original library and can use further commands.
How to come out of debugging mode:
When the related executor is done with debugging one can press ctrl + D and can come out of the interactive debugging shell. It will be useful to run the rest of the robot file for debugging or execution.
Standalone debugging in Robot framework:
you can run it also as a standalone robot framework depending on requirement, for that use rfdebug command to start and then the rest of commands can be used directly in interactive shell:
$ rfdebug
[...snap...]
>>>>> Enter interactive shell
> help
Input Robotframework keywords, or commands listed below.
Use "libs" or "l" to see available libraries,
use "keywords" or "k" to see the list of library keywords,
use the TAB keyboard key to autocomplete keywords.
Documented commands (type help <topic>):
========================================
EOF continue docs help keywords libs ll n pdb selenium
c d exit k l list longlist next s step
> log hello
> get time
< '2011-10-13 18:50:31'
> # use TAB to auto complete commands
> BuiltIn.Get Time
< '2011-10-13 18:50:39'
> import library String
> get substring helloworld 5 8
< 'wor'
The interactive shell support auto-completion for robot framework keywords and commands. Try input BuiltIn. then type <TAB> key to feeling it. The history will save at ~/.rfdebug_history default or any file defined in the environment variable RFDEBUG_HISTORY.
In case you don't remember the name of the keyword during using rfdebug, there are commands libs or ls to list the imported libraries and built-in libraries, and keywords <lib name> or k to list keywords of a library.
rfdebug accept any pybot arguments, but by default, rfdebug disabled all logs with -l None -x None -o None -L None -r None.
Step debugging: Debug library supports also step debugging. you can use step /s , next /n, coninue /c, list /l and long list /ll to trace and view the code step by step like in pdb:
$ robot some.robot
[...snap...]
>>>>> Enter interactive shell
> l
Please run `step` or `next` command first.
> s
.> /Users/xyb/some.robot(7)
-> log to console hello
=> BuiltIn.Log To Console hello
> l
2 Library DebugLibrary
3
4 ** test case **
5 test
6 debug
7 -> log to console hello
8 log to console world
> n
hello
.> /Users/xyb/some.robot(8)
-> log to console world
=> BuiltIn.Log To Console world
> c
>>>>> Exit shell.
world
Note: step by step debugging is not supported for For loop currently.
PS: please drop your comment if you like it or dislike it. Your comments are helpful to improve this blog.