【问题标题】:How to search for empty string/compare two strings in Robot Framework如何在 Robot Framework 中搜索空字符串/比较两个字符串
【发布时间】:2019-03-27 21:10:51
【问题描述】:

我已经阅读了这个问题

How to test for blank text field when using robotframework-selenium?

以及答案中机器人框架文档的two links,但我仍然不知道如何检查变量是否为空。

我想这样做

if var A equals var B then
   do something
else
   do something else

其中 A 是一个既可以包含某些内容也可以为空的字符串,其中 B 为空或 null。

【问题讨论】:

    标签: string if-statement compare robotframework is-empty


    【解决方案1】:

    可以使用多种不同的方式来实现,如下所示,选择适合你的方式

    1. 这样你可以检查两个变量是否等于或不

      Run Keyword If    '${A}'=='${B}'   do something    ELSE    do something
      
    2. 这样你可以一次性检查你的两个变量是否都为None

      Run Keyword If    '${A}'=='None' And '${B}'=='None'    do something
      
    3. 如果你的变量相等,你也可以使用下面的方法得到,如果两个值相等,它会返回 true

      Should Be Equal    ${A}    ${B}
      
    4. 如果两个值不相等,则返回 true。

      Should Not Be Equal   ${A}    ${B}
      

    更多信息请通过this docs

    机器人框架中还有${EMPTY}变量,您可以使用它来检查变量是否为空

    【讨论】:

    • So "Run keyword If '${A}'=='${EMPTY}' ?你为什么用撇号写这个?
    • 如何将示例 3 和 4 与 if 子句结合起来?
    • @hensti for first query..... 将其包装在撇号中的原因是因为如果您的变量不同,例如 A 是 int 而 B 是字符串,那么它会将两者都视为字符串,然后它将执行操作,没有撇号,只能比较相似的数据类型变量
    • @hensti 进行第二次查询 ....Run Keyword If '${A}'!='${Empty}' And '${B}'!='${Empty}' Should Be Equal ${A} ${B} 同样您也可以进行第四次查询
    • 嗯,我不确定我是否在第二个查询中遵循您的回答。 should be equal ${A} ${B} 然后呢?条件是否满足?
    【解决方案2】:

    就像这样工作:

    ${aaax}=     set variable  aaa aa ba baavaa
    ${aaaxx}=    set variable  aaa aba baavaa
    ${aba}=      set variable  aba
    
    ${res1}=     run keyword and return status  should contain  ${aaax}     ${aba}
    ${res2}=     run keyword and return status  should contain  ${aaaxx}    ${aba}
    
    log to console  ${EMPTY}
    log to console  res1: ${res1}
    log to console  res2: ${res2}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-29
      • 2019-01-14
      • 2018-05-28
      • 2022-09-30
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2010-11-13
      相关资源
      最近更新 更多