Replacing ${variable} in json file containing 'ID'

Viewed 85

I have some issues with how to replace a variable appearing within test data. Let me explain. I am running tests data driven and lets say I have 2 test cases. One does something and returns an 'id'. This 'id' is then used by the 2nd test case to add another entry. Such dependency is currently unavoidable since the 'id' generated is unique each time and TestCase2 depends on it. Currently when running testcase1 I get back an 'id' which I set as a suite variable. And then testcase2 uses this 'id' variable thus set. This works if the data is hardcoded into the test case. But when the data is abstracted into a test data file I dont know how to replace the '${id} in the test data.

As an example. TestCase2 reads data from the json file like this.

"{\"query\":\"mutation updatedata($id: Int!, $details: String!) {\\r\\n  updatedetaildata(input: { id: $id, details: $details })\\r\\n}\\r\\n\",\"variables\":{\"details\":\"{\\\"total_amount\\\": 523000}\",\"id\":${ID}}}"

What I would like to find out is 1. How to replace the ${id} with suite variable that I set after running TestCase1? 2. In another scenario, if I were to pass ${id} to TestCase2 as an argument how do I get it to replace the ${id} field in the test case data?

Here's my test case :

*** Settings ***
Suite Setup                     Run Keywords
...                             Generate Access Token               AND
...                             Generate Random Number              AND
...                             Generate Random Name                AND
...                             Set Testrails Attribute             1           29
Test Teardown                   Add Test Result
Suite Teardown                  Send Report to Workchat
Resource                        ../../../../../Main/resources/importer.robot


*** Test Case ***
Create New Mission - Belanja (Whitelist)
    [Documentation]             This is new test case
    [Tags]                      api_test
    Set Test ID                 9449

    ${payload}                  Get File                    api-test/Main/collections/engagement/testing/apitest/createnewtest.json
    ${payload}                  Convert to Json             ${payload}

    ### Req body
    ${response}=                GraphQl Request     method=POST
    ...                         referrer_url=graphql/query
    ...                         payload_path=${payload}
    ...                         token=${token}
    Set Global Variable         ${response}
    Log To Console              ${response}
    ${payload}=                 set variable                ${response}
    ${mission_id}=              get value from json         ${payload}       $.data.misPinCreateMission.id
    Log To Console              ${mission_id}
    ${id}=                      set variable                ${mission_id}
    Set Suite Variable          ${ID}                       ${id}


    ${template}=                Get File                    api-test/Main/collections/engagement/testing/apitest/editmission.json
    ${template}=                replace variables           ${template}
    ${payload}                  Convert to Json             ${payload}

    ### Req body
    ${response}=                GraphQl Request     method=POST
    ...                         referrer_url=graphql/query
    ...                         payload_path=${payload}
    ...                         token=${token}
    Set Global Variable         ${response}
    Log To Console              ${response}

    ## Assertion
    ${expected_json}            Get File                    api-test/Main/assertions/expected-json/engagement/testing/apitest/editmission.json
    ${expected_json}=           Convert To Json             ${expected_json}

I got this error message :

Create New Mission: This test for Create N... ........{'errors': [{'message': "json body could not be decoded: invalid character 'd' looking for beginning of value"}], 

Both answers both be great appreciated. Thank you

1 Answers

Here is one way:

Replace ${ID} in template.json to be a valid number:

"{\"query\":\"mutation updatedata($id: Int!, $details: String!) {\\r\\n  updatedetaildata(input: { id: $id, details: $details })\\r\\n}\\r\\n\",\"variables\":{\"details\":\"{\\\"total_amount\\\": 523000}\",\"id\":0}}"

Then you can load and modify it:

*** Settings ***
Library    Collections
Library    OperatingSystem

*** Test Cases ***
Jsontest
    ${template}=        Get File    template.json
    ${template_d}=      Evaluate    json.loads(${template})    modules=json
    Set To Dictionary   ${template_d['variables']}   id    123
    Log To Console      ${template_d}
Related