Zoomba.APILibrary: Passing/Setting header values from another TestCase or Keyword

Hi

I have a usecase where I need to grab the auth token from webUI and then Pass it on to API test-cases as header.

I have implemented and get the auth-token from Keyword (Get AuthToken). But in Zoomba.APILibrary we set the headers in Variables section as

*** Variables ***
${endpoint}= “some url”
&{headers_dictionary}= Authorization=Bearer ${token} Content-Type=application/json charset=UTF-8
${query_string}= none

In the test case file I am using Set Variable to update the ${token} but it’s not getting updated.

Get Basic API Test
${token1}= Run Keyword Get AuthToken #keyword to get auth-token and assign it
${token}= Set Variable ${token1}
Log ${token}
${response}= Call Get Request ${headers_dictionary} ${endpoint} ${query_string}
Log ${response}

Hey so I think what you have here is a misunderstanding of how RobotFramework handles the Variables section at runtime. When the test cases is loaded it is going to attempt to verify ${token} which at that point is not set yet.

So a couple of options would look like:

  1. You could simply make your headers dictionary after you get your token:
Get Basic API Test
    ${token}=     Run Keyword Get AuthToken     #keyword to get auth-token and assign it
    ${headers_dictionary}=       Create Dictionary    Authorization=Bearer ${token}     Content-Type=application/json 
   charset=UTF-8
    ${response}=    Call Get Request    ${headers_dictionary}    ${endpoint}    ${query_string}
    Log ${response}
  1. Create a helper keyword to create the headers dictionary for you:
*** Keywords ***
Generate Headers For APIs
    ${token}=     Run Keyword Get AuthToken     #keyword to get auth-token and assign it
    ${headers_dictionary}=       Create Dictionary    Authorization=Bearer ${token}     Content-Type=application/json     
    charset=UTF-8
    [Return]              ${headers_dictionary}

*** Test Cases ***
Get Basic API Test
    ${headers_dictionary}=       Generate Headers For APIs
    ${response}=    Call Get Request    ${headers_dictionary}    ${endpoint}    ${query_string}
    Log ${response}

Hi Brandon,

Thanks for this. Was not knowing Create Dictionary can be used in Zoomba package.
Yeah, with this gives better flexibility to create headers.
Closing the issue :slight_smile:

Cheers,
Amit

1 Like