【问题标题】:Replacing ${variable} in json file containing 'ID'替换包含 \'ID\' 的 json 文件中的 ${variable}
【发布时间】:2022-08-15 14:45:12
【问题描述】:

我对如何替换测试数据中出现的变量有一些问题。让我解释。我正在运行测试数据驱动,假设我有 2 个测试用例。一个人做某事并返回一个\'id\'。然后第二个测试用例使用这个 \'id\' 添加另一个条目。这种依赖目前是不可避免的,因为每次生成的 \'id\' 都是唯一的,并且 TestCase2 依赖于它。 目前,在运行 testcase1 时,我得到了一个我设置为套件变量的 \'id\'。然后 testcase2 使用这样设置的这个 \'id\' 变量。如果数据被硬编码到测试用例中,则此方法有效。但是当数据被抽象成测试数据文件时,我不知道如何替换测试数据中的\'${id}。

举个例子。 TestCase2 像这样从 json 文件中读取数据。

\"{\\\"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}}}\"

我想知道的是 1. 如何用运行TestCase1 后设置的套件变量替换${id}? 2. 在另一种情况下,如果我将 ${id} 作为参数传递给 TestCase2,我如何让它替换测试用例数据中的 ${id} 字段?

这是我的测试用例:

*** 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}

我收到此错误消息:

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

两个答案都非常感谢。谢谢

  • 我曾处理过类似的问题。我会将 JSON 文件中的 \"${ID}\" 更改为像 0 这样的虚拟数字。这允许您加载 JSON。加载后,您可以更换它。 JSON 是字典,RF 有很好的关键字。
  • 还是一样,得到\"Create New Mission: This test for Create N... ........{\'errors\': [{\'message\': \"json body could not be decoded:无效字符 \'d\' 寻找值的开头\"}], \"

标签: robotframework


【解决方案1】:

这是一种方法:

将 template.json 中的 ${ID} 替换为有效数字:

"{\"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}}"

然后你可以加载和修改它:

*** 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}

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多