I have a test suite
Library RequestsLibrary
Library JSONLibrary
Library OperatingSystem
*** Variable ***
${base_url} https://api.sportpartnerxxx.vn/v1
${identity_URL} https://identity.sportpartnerxxx.vn
*** Test Cases ***
Login
${body}= Create Dictionary client_id=sportpartner-mobile-app client_secret=ifd-sportpartner-secret-2021-mobile-app grant_type=password username=abc password=123456
${header}= create dictionary content_type=application/x-www-form-urlencoded
${response}= Post ${identity_URL}/connect/token headers=${header} data=${body}
${token}= Set Variable Bearer ${response.json()["access_token"]}
Status Should Be 200
Refresh token
${body}= Create Dictionary client_id=sportpartner-mobile-app client_secret=ifd-sportpartner-secret-2021-mobile-app grant_type=password refresh_token=${refresh_token}
${header}= create dictionary content_type=application/x-www-form-urlencoded Authorization=&{token}
${response}= Post ${identity_URL}/connect/token headers=${header} data=${body}
Status Should Be 200
I want to take ${token} variable of Login test case add to Authorization value of Refresh token test case. But it failed.
Does anyone help me?
Variable crated in a case is local (visible) only in it, and in the keywords it calls; once the case ends, the var is deleted.
If you want it to be accessible in a follow up case, you need to extend its scope - call Set Suite Variable with it, or Set Global Variable - which will make it available for any other suites ran after this one.
This has one big drawback, though - you're adding dependency on the cases order; "Login" has always to be ran fist - and to succeed, so that "Refresh token" works.
That's why this is usually done through keywords (called in case setup, etc).
I'd kindly suggest to read about variables scopes in the user guide -
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-set-test-suite-global-variable-keywords
You could try saving the variable with
Set Suite Variable
Then accessing it in your other test case
https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable
Related
How to define multiple endpoints in the settings of robot
My expectation will be like this. Defining two urls in the settings
***Settings***
Library REST ${API_URL_1} URL_1
Library REST ${API_URL_2} URL_2
and it will called in the testcases as
*** Test Cases ***
Scenario-1
GET ${URL_1}/getsomething
get the response to a variable (say: data)
POST ${URL_2}/dosomething ${data}
will that possible using restinstace library?
According with the Library documentation:
Sends a GET request to the endpoint.
The endpoint is joined with the URL given on library init (if any). If
endpoint starts with http:// or https://, it is assumed an URL outside
the tested API
That means, if you build your test like:
*** Test Cases ***
Scenario 1
GET http://URL_1/getsomething
GET http://URL_2/getsomething
Will connect you to different endpoints.
Or you can store your URLs in variables and call the ones you want:
*** Variables ***
${URL_1} http://myfirsturl
${URL_2} http://myotherturl
*** Test Cases ***
Scenario 1
GET ${URL_1}/getsomething
GET ${URL_2}/getsomething
I m trying to use bearer authorization using requestlibary of robot framework.
Note:
${tokenval} getting value from another keyword.
${Body} value declared as a variable.
*** Test Cases ***
Createnew
${tokenval}= Get Token Client Credentials
${token1}= Catenate bearer ${SPACE} ${tokenval}
${headers} content-type=application/json Authorization=${token1}
Create Session apitest https://testxxapi.com ${headers}
${response}= POST On Session apitest /deals/new ${Body}
${resDict}= convert to dictionary ${response.json()}
log ${resDict}
But I m getting HTTPError: 403 Client Error.
Same request with bearer authorization working in postman.
There's one issue with your code, and one potential you'd better change for the piece of mind.
The Catenate keyword combines its arguments in a single string using (by default) "space" as delimiter.
Thus after this call:
${token1}= Catenate bearer ${SPACE} ${tokenval}
, the value you get is bearer the_token - 3 spaces b/n the two words. Drop the second argument, or just use
${token1}= Set Variable Bearer ${tokenval}
The other, potential issue is the casing of "Bearer" - according to the specs it should be treated case insensitive, but some Oauth 2.0 providers had bugs years ago where they expected it to be with a capital letter.
*** Settings ***
Documentation API Testing in Robot Framework
Library RequestsLibrary
Library Collections
Library OperatingSystem
*** Variables ***
${dev_b_token} Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI3R
${dev_b_server} https://test-api-dev-b.apps.robot-b.osd.corp
${params} kod=BI1K
*** Test Cases ***
Do a GET Request with Bearer token
&{headers} Create Dictionary Authorization=${dev_b_token}
Create Session dev_b_session ${dev_b_server}
Log ${headers}
${response}= GET On session dev_b_session /api/v1/test/info headers=${headers} params=${params}
Log ${response}
Status Should Be 200 ${response} #Check Status as 200
I want to execute the same keyword for an array of input in robot framework.
For ex:
*** Test Case ***
Login to gmail ${UserIDs} ${passwords}
Here, UserIDs and Passwords are an array and I wish to execute the keyword 'Login to gmail' for all the input in those arrays. I know that the keyword mentioned here wont work. But, I am looking help to achieve this.
Using a :FOR loop
If your constraint is that the usernames and passwords must be stored in arrays, and you all this in a single test case, your only solution is to use a loop. In this case, since you want to iterate over two equal-length arrays you can use the IN ZIP variant of robot's looping mechanism. In order for all iterations to run even if one failed, you can use run keyword and continue on failure within the loop.
Example:
*** Variables ***
#{userIDs} one#example.com two#example.com badguy#example.com
#{passwords} secretpassword secretPassword SecretPassword
*** Keywords ***
Login to gmail
[Arguments] ${userID} ${password}
should not be true $userID == "badguy#example.com"
... cannot login as bad guy
*** Test case***
Example
:FOR ${userID} ${password} IN ZIP ${userIDs} ${passwords}
\ run keyword and continue on failure
\ ... login to gmail ${userID} ${password}
Using a template
A more common solution is to use a test template. This allows you to specify keyword to be run for each test, and then the test itself contains the data (ie: the data is not stored in arrays).
Example:
*** Keywords ***
Login to gmail
[Arguments] ${userID} ${password}
should not be true $userID == "badguy#example.com"
... cannot login as bad guy
*** Test case***
Gmail logins
[Template] login to gmail
# username # password
one#example.com secretpassword
two#example.com bogus
badguy#example.com letmein
Using a test template for the suite
A third solution is to use one template for the whole suite. One advantage to this is that each success or failure is recorded as a separate test, and can have a unique name.
Example:
*** Settings ***
Test Template Login to gmail
*** Keywords ***
Login to gmail
[Arguments] ${userID} ${password}
should not be true $userID == "badguy#example.com"
... cannot login as bad guy
*** Test case***
# test case name # username # password
valid username/password one#example.com secretpassword
valid username, invalid password two#example.com bogus
invalid username badguy#example.com letmein
I want to test API using Requests Library.
My Code is as follows:
*** Settings ***
Documentation Read API Testcase
Library RequestsLibrary
*** Variables ***
${headers} {'content-type': 'application/json', 'authorizationFlag':'N'}
*** Test Cases ***
Read API
Create Session CLM http://172.20.33.224:8080/clm-reg/rest/dataservice/1/CLM/1
${resp} Get Request CLM /RegistrationRequestDetails/json/583d8b14498e021b2f93a773 headers = ${headers}
Log to console ${resp}
I am getting the error :
AttributeError: 'unicode' object has no attribute 'items'
I found the problem with the Headers i am passing.
when i searched over the internet, i got that the way i am passing the header values is correct.
Please any one help me on this.
Thanks
Sarada
I've changed your headers line to what should work. Let us know if you've any success or what other problems you get tripped up on.
*** Variables ***
${headers} Create Dictionary Content-Type application/json authorisationFlag N
The problem is that your ${headers} var is just a string, not a dictionary.
JSON is tricky that way. You have several options to create a dictionary in RF.
RF's Create Dictionary keyword
Python's json.loads(str) as a lib call
RF's Evaluate keyword...
You could use the built-in variable dictionary type like this:
Set Test Variable &{HEADERS} Content-Type=application/json authorisationFlag=N Accept=*/* Cache-Control=no-cache
Then call it as a variable which spread as a dict on your headers variable:
${resp} Post Request api-encoder /api-token-auth/ data=${DATA} headers=${HEADERS}
I'm trying to send a request (post) using Balkan's requests library from test case written in Robot Framework http://bulkan.github.io/robotframework-requests/#Post with two parameters as a data and a file. Unfortunatelly, all the time I have the same error like described below.
My Test Case:
X_T_Should Upload File Correctly And Get HTTP 200
Send Default File To SUT And Return Response
*** Keywords ***
Send Default File To SUT And Return Response
[Arguments] ${user_login}=${USER_LOGIN} ${user_password}=${USER_PASSWORD}
${url}= Get URL
${auth}= Create List ${user_login} ${user_password}
Create Session rm ${url} auth=${auth}
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
&{data}= Create Dictionary name=file filename=${DEFAULT_FILE_NAME}
${file_data}= Get Binary File ${CURDIR}${/}Resources${/}${DEFAULT_FILE_NAME}
&{files}= Create Dictionary file=${file_data}
${resp}= Post Request rm ${UPLOAD_URI} files=${files} data=${data} headers=${headers}
Delete All Sessions
Error (from Robot Framework):
20160525 09:47:10.645 : FAIL : ValueError: Data must not be a string.
The problem is with the keyword Post Request. When I do not set an argument files or data that everything is well but if I set both args. that I see these strange error.
It is a bug in library?
According to the documentation, the files parameter is a list of file names. You are passing the actual file contents into the keyword. This might explain why you are getting "Data must not be a string".
We've encountered this exception also. The exception seems to be raised in Requests Python library.
At line 119 of requests/models.py,
elif isinstance(data, basestring)
checks whether data is a string. And robotframework-requests seems to be casting data into a string almost always. There is an issue for robotframework-requests regarding the exception.