【问题标题】:How to extract values from nested dictionary variable in robot framework?如何从机器人框架中的嵌套字典变量中提取值?
【发布时间】:2021-08-02 19:53:51
【问题描述】:

我有嵌套的 dict 变量,想从中提取值来比较它是否等于实际值。使用键它可以正常工作,但是使用嵌套的 dict 我没有找到如何提取值的方法(例如 operatorid)。你对如何做有什么建议吗? 变量

${HIRING_DATA_TYPES}=    {'Job Profile': {"AND":"", "operatorId": 'in the selection list', "targetInstances": 'Finance'},
...    'Recruiter': {"AND":"", "operatorId": 'not in the selection list', "targetInstances": '3314 Tax'},'Job Requisition': {"AND":"", "operatorId": 'is not empty'},
...    'Description': {"AND":"", "operatorId": 'equal to', "dataType":"Text", "targetType": 'targetText', "targetValue": "SDET_text"},
...    'Description 2': {"AND":"", "operatorId": 'greater than', "dataType":"Text", "targetType": "targetText", "targetValue": "SDET_text"},
...    'Description 3': {"AND":"", "operatorId": 'greater than or equal to', "dataType":"Text", "targetType": "targetText", "targetValue": "SDET_text"}}

提取密钥

Check Added Source Fields
    [Arguments]    ${RESPONSE}   ${FILTERS_TO_ADD}
    ${ACTUAL_SOURCE_FIELDS}=    Get Json Path    ${RESPONSE}    $.pipelines..filter..children..field..descriptor
    Sort list    ${ACTUAL_SOURCE_FIELDS}
    ${FILTERS_TO_ADD}=    Evaluate    ${HIRING_DATA_TYPES}
    ${EXPECTED_SOURCE_FIELDS}=    Get Dictionary Keys    ${FILTERS_TO_ADD}
    Lists Should Be Equal    ${ACTUAL_SOURCE_FIELDS}   ${EXPECTED_SOURCE_FIELDS}

【问题讨论】:

  • 您展示了创建一个名为 ${HIRING_DATA_TYPES} 的变量,但您从未在代码中使用该变量。请edit您的问题包括minimal reproducible example。另外,我不认为${HIRING_DATA_TYPES} 是一本字典。根据您的定义,它看起来像一个字符串。

标签: dictionary nested robotframework


【解决方案1】:

它通过以下方式为我工作:

Check Added Comp Operators and Values
    [Arguments]    ${RESPONSE}   ${FILTERS_TO_ADD}
    @{EXPECTED_COMP_OPERATOR}=    Create List
    FOR    ${key}    IN    @{HIRING_DATA_TYPES.keys()}
        ${COMP_OPER_VALUES}=    Get From Dictionary    ${HIRING_DATA_TYPES}[${key}]   operatorId
        Append To List    ${EXPECTED_COMP_OPERATOR}    ${COMP_OPER_VALUES}
    END
    Lists Should Be Equal    ${ACTUAL_COMP_OPER}    ${EXPECTED_COMP_OPERATOR}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2021-11-01
    • 2020-12-07
    相关资源
    最近更新 更多