Chirag Dhingra
Chirag Dhingra

Reputation: 138

How to store Value returned from 1st keyword and then using that value in another keywords without running the 1st keyword again in robot framework

I struck in one problem in which I want to store Value returned from 1st keyword in variable during initial run and then using that variable in another keywords without running the 1st keyword again .

I tried below code but not able to resolve the issue.

Create Account Post Request
    ${response}=    call post request    ${headers2}    ${base_url}   ${base_url_account}    ${account_data}
    log    ${response}
    Should Be True     ${response.status_code} == 201
    ${account_id}    get value from json     ${response.json()}    id
    log    ${account_id}
    RETURN    ${account_id}

Get Network ID    --> [While running this keyword Create Account Post Request will run again which updates the account ID which I don't want]
    ${acc}=   Create Account Post Request  --> [During this It will run Create Account Post Request again which I don't want]
    log    ${acc}

My questions are :

  1. How to use account_ID in any keywords without running the first keyword again and again ?
  2. How to use return value from 1st keyword in any other keywords? [Condition, I don't want to run 1st keyword again to get the return value]

Below scenario is expected :

  1. 1st Keyword ran
  2. 1st Keyword return some value
  3. Store value in some variable
  4. Use returned value anywhere in the suite file, in any keyword within the suite, in any test case .

Upvotes: 1

Views: 455

Answers (1)

Himanshu Gupta
Himanshu Gupta

Reputation: 246

Declare a blank variable

*** Variables ***
${account_id}

Add an IF condition in the keyword to run only if the account id is blank, else return the global variable. This way the function steps will be executed only once throughout the suite.

Create Account Post Request
    IF    "${account_id}" == ""  --> ${account_id} should be in inverted commas "${account_id}".
        ${response}=    call post request    ${headers2}    ${base_url}   ${base_url_account}    ${account_data}
        log    ${response}
        Should Be True     ${response.status_code} == 201
        ${account_id}    get value from json     ${response.json()}    id
        log    ${account_id}
        Set Suite Variable    ${account_id} 
    END
    [RETURN]    ${account_id}

Upvotes: 2

Related Questions