How to validate status code in robot framework - robotframework

i about to make a validation status code in robot framework. i create value in json with wrong password and i want to make validation code 400 bad request with this code.
login_negative_user
${json} Load JSON From File ${CURDIR}/../json/negativeuser.json
# Set Test Variable ${JSON_SCHEMA} ${json}
${resp}= POST ${base_url}/horde/v1/auth/login json=${json}
${data} Convert To String ${resp.json()}
Status Should Be 400 ${resp}
Should Be Equal As Strings ${resp.status_code} 400
${jsondata} evaluate ${data}
${poselang}= Set Variable ${jsondata['data']['token']}
Set Global Variable ${poselang}
log to console Get poselang token
But my log is this
login negativeuser | FAIL |
HTTPError: 400 Client Error: Bad Request for url: {secret api}
am i wrong to make validation with that code? sorry i cant tell the api because it is secret

Related

How to get variable with robot framework to rest API

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

How to use bearer authorization for robot framework requestlibrary

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

ROBOT Framework- API POST - Response.content appears empty even with response successful

Issue:-
ROBOT Framework- API POST - Response.content appears empty even with response successful
Create Session test_session ${baseurllosexternal} verify=true
&{headers}= Create Dictionary Authentication-Token=123-123-123 Content-Type=application/json
${checkpostdata}= Parse Json ${Ratesearchpost_v1}
Log To Console ${checkpostdata}
${Response}= Post Request test_session ${RateSearch} data=${checkpostdata} headers=${headers}
${responseString} Set Variable ${response.content}
Library used RequestsLibrary.

Using headers with the Robot Framework requests library's 'Get Request' Keyword

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}

Sending a POST request using Balkan's requests lib with data and files (Robot Framework)

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.

Resources