Using the PYTHON request library in a robotframework test script

1,558 views
Skip to first unread message

ziffification

unread,
Mar 27, 2015, 10:11:19 AM3/27/15
to robotframe...@googlegroups.com
Hi

I am trying to understand how to use a library like REQUEST in a robotframework test script:
something like this below I have done in a regular PYTHON script

response = requests.get('https://172.168.100.12/views', auth=HTTPDigestAuth('user', 'password'), verify=False)

how would I use it in a RF script is the part I am having trouble figuring out.

Message has been deleted

Ed Manlove

unread,
Mar 28, 2015, 4:06:11 PM3/28/15
to robotframe...@googlegroups.com
On 03/27/2015 01:24 PM, ziffification wrote:

> [SNIP]
>
> so i added
>
> def create_digest_session(self, alias, url, headers={}, cookies=None,
> auth=None, timeout=None, proxies=None,
> verify=False):
>
> auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
> return self._create_session(alias, url, headers, cookies, auth,
> timeout, proxies, verify)
>
>
> amd then ran
>
> python
> >>>import py_compile
> >>>py_compile.compile('RequestKeywords.py')
>
> to get the RequestKeywords.pyc updated as well
>
> root@PC2303VM:/home/robm/Downloads/requests-robotframework/tests# ls
> -la /usr/local/lib/python2.7/dist-packages/RequestsLibrary/
> total 64
> drwxr-sr-x 2 root staff 4096 Mar 27 11:10 .
> drwxrwsr-x 66 root staff 4096 Mar 27 08:18 ..
> -rw-r--r-- 1 root staff 867 Mar 27 08:18 __init__.py
> -rw-r--r-- 1 root staff 1247 Mar 27 08:18 __init__.pyc
> -rw-r--r-- 1 root staff 19651 Mar 27 11:09 RequestsKeywords.py
> -rw-r--r-- 1 root staff 18303 Mar 27 11:10 RequestsKeywords.pyc
> -rw-r--r-- 1 root staff 18 Mar 27 08:18 version.py
> -rw-r--r-- 1 root staff 180 Mar 27 08:18 version.pyc
>
>
> so At this point I thought this would work:
>
> [SNIP]

Are you sure you are running the modified requests library in
/usr/local/lib/python2.7/dist-packages/RequestsLibrary/ and you don't
have another version installed somewhere on your machine? I don't recall
how to check from either Python or from Robot Framework to see wether or
not it is picking out the one you expect. Maybe $/which pybot might
help? You shouldn't need to compile it and it should get compiled when
python pulls in all the packages although this I suspect is not your
issue. Also you might try using virtualenv which you can create a clean
python to test isolated development with (not much of an issue with
Robot Framework as you might just have half dozen packages but if you
have a lot or maybe even a little development it can save you).

Ed

Ed Manlove

unread,
Mar 28, 2015, 4:13:03 PM3/28/15
to robotframe...@googlegroups.com
I should have also added that you can use the following line to regain control of stdout from Robot Framework for debugging purposes. It could come in handy in your situation

import pdb, sys; pdb.Pdb(stdout=sys.__stdout__).set_trace()

Just put it in the python code you wish to debug and it starts up pdb.

Ed

ziffification

unread,
Mar 28, 2015, 8:57:47 PM3/28/15
to robotframe...@googlegroups.com


hey Thanks Ed!

It never occurred that i could have another install ...I will try the virtual env 

incidentally this is how its included in my testfile as a library

Library ../src/RequestsLibrary/RequestsKeywords.py so I am pretty sure its looking in the right place

 

Ed Manlove

unread,
Mar 28, 2015, 9:17:57 PM3/28/15
to robotframe...@googlegroups.com
Have you tried just

Library    RequestsLibrary

although you do say that it only produces and error for your keyword... mmmm... not sure. But you might try the debug line,

import pdb, sys; pdb.Pdb(stdout=sys.__stdout__).set_trace()

That always is helpful to me when trying to figure out what is going on.

Ed

Ben Fariello

unread,
Mar 30, 2015, 12:17:40 AM3/30/15
to robotframe...@googlegroups.com
I'm not sure what problem you're having. You can define your keyword implementation Python script, and import requests in that class file. Then invoke the http get/post etc, and parse the response however you need to to determine pass/fail scenarios. 

Something I noticed was that the Robot Framework plugin for Eclipse uses Jython 2.5, which is incompatible with the requests library, so you have to user the jybot or pybot to run the tests With Jython or Python 2.7 or higher. You get failure messages during imports. If you can't use Requests you can probably use urllib2, but it's definitely not at simple as Requests. 

ziffification

unread,
Mar 30, 2015, 5:18:31 PM3/30/15
to robotframe...@googlegroups.com
OK

a little more progress this time I give the full path 

Library  Collections
Library  String
Library  /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py
Library  OperatingSystem
Suite Teardown  Delete All Sessions

*** Test Cases ***
Get With DigestAuth
    [Tags]  get
    ${auth}=  Create List  username  password
    Create Digest Session   TerraceQ    https://172.168.101.139     auth=${auth}

    ${resp}=  Get  TerraceQ  /views
    Should Be Equal As Strings  ${resp.status_code}  200
    Should Be Equal As Strings  ${resp.json()['authenticated']}  True


but I get 

robm@PC2303VM:~/Downloads/requests-robotframework/tests$ pybot digestonly.txt==============================================================================
Digestonly                                                                    
==============================================================================
Get With DigestAuth                                                   | FAIL |
Keyword 'RequestsKeywords.Create Digest Session' got multiple values for argument 'auth'.
------------------------------------------------------------------------------
Digestonly                                                            | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  /home/robm/Downloads/requests-robotframework/tests/output.xml
Log:     /home/robm/Downloads/requests-robotframework/tests/log.html
Report:  /home/robm/Downloads/requests-robotframework/tests/report.html


from the test


ps I cant get the debugging to work either  I added the python code to the RequestsKeywords.py

Ed Manlove

unread,
Mar 30, 2015, 8:58:45 PM3/30/15
to robotframe...@googlegroups.com
Concerning debugging try putting it somewhere you know it will be hit
and start a breakpoint. Its surprising it is not working. As for the
above issue do you need to make it

@{auth}= Create List username password
Create Digest Session TerraceQ https://172.168.101.139
auth=${auth}

Just a thought...

Ed

Ed Manlove

unread,
Mar 31, 2015, 7:18:42 AM3/31/15
to robotframe...@googlegroups.com
On 03/30/2015 08:58 PM, Ed Manlove wrote:
> On 03/30/2015 05:18 PM, ziffification wrote:
>> OK
>>
>> a little more progress this time I give the full path
>>
>> Library Collections
>> Library String
>> Library
>> /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py
>> Library OperatingSystem
>> Suite Teardown Delete All Sessions
>>
>> [SNIP]
>
> Concerning debugging try putting it somewhere you know it will be hit
> and start a breakpoint. Its surprising it is not working. As for the
> above issue do you need to make it
>
> @{auth}= Create List username password
> Create Digest Session TerraceQ https://172.168.101.139
> auth=${auth}
>
> Just a thought...
>
> Ed
>

I am also still cautious on how you are importing the library. By giving
a direct path to the RequestLibrary.py file. Note here I am saying the
python file and not the folder it is in which also includes the pythin
initialization file, __init__.py. The initialization file in turn is not
an empty one but sets the library variable ROBOT_LIBRARY_SCOPE. So by
importing the RequestLibrary.py file and not the RequestLibrary library
I think you might be missing something. Have you tried

Library RequestsLibrary

as it should be on the path.

Ed

ziffification

unread,
Mar 31, 2015, 9:50:49 AM3/31/15
to robotframe...@googlegroups.com


    ${auth}=  Create List  username  password
    Create Digest Session   TerraceQ    https://172.168.101.139     auth=${auth}

change to this?


     @{auth}=  Create List  username  password
     Create Digest Session   TerraceQ    https://172.168.101.139    
auth=${auth}

just move the auth down?

ziffification

unread,
Mar 31, 2015, 9:54:28 AM3/31/15
to robotframe...@googlegroups.com
yup that looks better adding

Library RequestsLibrary  works still gives me that error however but its definitely a better choice for the reasons you pointed out.

robm@PC2303VM:~/Downloads/requests-robotframework/tests$ pybot digestonly.txt
==============================================================================

Digestonly                                                                   
==============================================================================
Get With DigestAuth                                                   | FAIL |
Keyword 'RequestsLibrary.Create Digest Session' got multiple values for argument 'auth'.

------------------------------------------------------------------------------
Digestonly                                                            | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  /home/robm/Downloads/requests-robotframework/tests/output.xml
Log:     /home/robm/Downloads/requests-robotframework/tests/log.html
Report:  /home/robm/Downloads/requests-robotframework/tests/report.html




Reply all
Reply to author
Forward
0 new messages