Robotframework Robot框架:如果条件不满足,如何使用“如果设置变量”,而不指定默认值(无)
我是机器人框架的新手 这里我使用robotframeworkforloop和Set Variable If关键字解析响应。但当我在循环中呼叫时。其中一个变量设置为“无”。我想避免这种情况 响应体Robotframework Robot框架:如果条件不满足,如何使用“如果设置变量”,而不指定默认值(无),robotframework,Robotframework,我是机器人框架的新手 这里我使用robotframeworkforloop和Set Variable If关键字解析响应。但当我在循环中呼叫时。其中一个变量设置为“无”。我想避免这种情况 响应体 { 'services': [ { 'service': 'postgres',, 'replicationStatus': 'completed' }, { 'service': 'mongodb', 'replication
{
'services': [
{
'service': 'postgres',,
'replicationStatus': 'completed'
},
{
'service': 'mongodb',
'replicationStatus': 'completed'
}
]
}
解析响应体
:FOR ${item} in @{response_body["services"]}
\ Log ${item}
\ ${mongodb_replication_status}= Set Variable If "${item['service']}"=="mongodb" ${item['replicationStatus']}
\ ${postgres_replication_status}= Set Variable If "${item['service']}"=="postgres" ${item['replicationStatus']}
Log ${mongodb_replication_status}
Log ${postgres_replication_status}
我得到如下输出:
${mongodb_replication_status} = completed
${postgres_replication_status} = None
在每个条件和值之后,都会给出默认值,在您的情况下:
:FOR ${item} in @{response_body["services"]}
\ Log ${item}
\ ${mongodb_replication_status}= Set Variable If "${item['service']}"=="mongodb" ${item['replicationStatus']} default value
\ ${postgres_replication_status}= Set Variable If "${item['service']}"=="postgres" ${item['replicationStatus']} default value
Log ${mongodb_replication_status}
Log ${postgres_replication_status}
另一个多条件示例:
${variable_value}= Set Variable If ${var} == 1 one
... ${var} == 2 two
... other than one or two
如果默认值不可用,则将变量指定为无
这在中有很好的解释。在每个条件和值之后,在您的情况下,会给出默认值:
:FOR ${item} in @{response_body["services"]}
\ Log ${item}
\ ${mongodb_replication_status}= Set Variable If "${item['service']}"=="mongodb" ${item['replicationStatus']} default value
\ ${postgres_replication_status}= Set Variable If "${item['service']}"=="postgres" ${item['replicationStatus']} default value
Log ${mongodb_replication_status}
Log ${postgres_replication_status}
另一个多条件示例:
${variable_value}= Set Variable If ${var} == 1 one
... ${var} == 2 two
... other than one or two
如果默认值不可用,则将变量指定为无
这在本书中得到了很好的解释