RobotFramework:使用FOR循环进行连接

时间:2019-03-21 19:58:22

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

我对使用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
...    }

1 个答案:

答案 0 :(得分:-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()函数或与之类似)。