【问题标题】:RobotFramework : catenate with FOR loopRobotFramework:与 FOR 循环连接
【发布时间】:2019-08-12 18:39:42
【问题描述】:

我对使用 RobotFramework 并尝试使用 Catenate 格式化字符串相当陌生,想知道在格式化字符串时是否可以使用 FOR 循环,以下是正确的格式?任何建议将不胜感激,谢谢!!!

${data}=    Catenate
...    {
...    "email_address": "${NewUserEmailID}",
...    "user_name": "${UserName}",
...    "roles": [
...    :FOR    ${roleId}, ${catID}, ${subcatID}    IN    @{role_id_list}, @{category_id_list}, @{subCat_id_list}
...    /    {
...    /    "role_id": ${roleId},
...    /    "categories": [{ "category_id": "${catID}" }],
...    /    "subcats":[{ "sub_category_id": "${subcatID}" }]
...    /    },
...    ],
...    "line_manager": "${LineManageID}",
...    "guest_user": ${GuestUser},
...    "guest_invitation_text":    "Invitation Text",
...    "guest_redirect_url":    "http://www.example.com/guest",
...    "organization_id":    1
...    }

【问题讨论】:

  • 你试过运行它吗? ;) 不,至少不是你希望它运行的方式——循环不会执行,你只会得到包含“:FOR”、“@{role_id_list}”等的结束字符串。
  • 你应该在纯python中为此实现一个特殊的catenate关键字。

标签: python-3.x python-2.7 automated-tests robotframework


【解决方案1】:

想知道在格式化字符串时是否可以使用 FOR 循环

不,在执行循环并将每次迭代的值附加到Catenate 目标的意义上。将会发生的是你最终会得到包含“:FOR”、“@{role_id_list}”等的结束字符串。

但是实现你想要的并不难 - 只需要一个循环来构造中间字符串,并将 that 附加到 Catenate 中。像这样:

${roles array}=    Set Variable    ${EMPTY}    # initialize an empty string variable

:FOR    ${roleId}    ${catID}    ${subcatID}    IN ZIP    ${role_id_list}    ${category_id_list}    ${subCat_id_list}
/    ${roles array}=    Set Variable    ${roles array}{  # you append the target string to the end of the current value of the variable
/    ${roles array}=    Set Variable    ${roles array}"role_id": ${roleId},
/    ${roles array}=    Set Variable    ${roles array}"categories": [{ "category_id": "${catID}" }],
/    ${roles array}=    Set Variable    ${roles array}"subcats":[{ "sub_category_id": "${subcatID}" }]
/    ${roles array}=    Set Variable    ${roles array}},

# and now use it inside the Catenate
${data}=    Catenate
...    {
...    "email_address": "${NewUserEmailID}",
...    "user_name": "${UserName}",
...    "roles": [ ${roles array}
...    ],
...    "line_manager": "${LineManageID}",
...    "guest_user": ${GuestUser},
...    "guest_invitation_text":    "Invitation Text",
...    "guest_redirect_url":    "http://www.example.com/guest",
...    "organization_id":    1
...    }

如您所见,迭代多个列表的语法有点不同 - 需要使用 IN ZIP(来自/类似于 python 的 zip() 函数)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2017-09-13
    • 2019-04-14
    • 2012-09-13
    • 2020-07-10
    • 2019-01-05
    相关资源
    最近更新 更多