【问题标题】:Set Variable If returns none robot framework设置变量如果返回无机器人框架
【发布时间】:2016-04-06 12:47:18
【问题描述】:

我是 Robot Framework 的新手,在运行下面的变量 ${month} 时返回“无”。

我要做的是将月份作为数字转换为文本中的相关月份,例如1 月至 1 月,4 月至 4 月等。

我相信这将是一个简单的修复,但我不知道它是什么......任何帮助将不胜感激。

${month int}  Get Current Date  result_format=%m
Log To Console  month int=${month int} 

${month} =  Set Variable If  '${month int}' >= 13  Fail
${month} =  Set Variable If  '${month int}' == '01'  January
${month} =  Set Variable If  '${month int}' == '02'  Febuary
${month} =  Set Variable If  '${month int}' == '03'  March
${month} =  Set Variable If  '${month int}' == '04'  April
${month} =  Set Variable If  '${month int}' == '05'  May
${month} =  Set Variable If  '${month int}' == '06'  June
${month} =  Set Variable If  '${month int}' == '07'  July
${month} =  Set Variable If  '${month int}' == '08'  August
${month} =  Set Variable If  '${month int}' == '09'  September
${month} =  Set Variable If  '${month int}' == '10'  October
${month} =  Set Variable If  '${month int}' == '11'  November
${month} =  Set Variable If  '${month int}' == '12'  December

Log To Console  month string=${month}

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    您的代码正在执行您要求他执行的操作。看它:在最后一行你说

    ${month} =  Set Variable If  '${month int}' == '12'  December
    

    因此${month} 设置为None,因为${month int} 不是12,None 是给定条件不成立的情况下的默认值。您需要使用 else if 逻辑,这在 Robotframework 中以这种方式完成:

    ${month} =  Set Variable If  '${month int}' >= 13  Fail
    ...  '${month int}' == '01'  January
    ...  '${month int}' == '02'  Febuary
    ...  '${month int}' == '03'  March
    ...  '${month int}' == '04'  April
    ...  '${month int}' == '05'  May
    ...  '${month int}' == '06'  June
    ...  '${month int}' == '07'  July
    ...  '${month int}' == '08'  August
    ...  '${month int}' == '09'  September
    ...  '${month int}' == '10'  October
    ...  '${month int}' == '11'  November
    ...  '${month int}' == '12'  December
    

    Documentation to "Set Variable If"

    【讨论】:

    • 我尝试了上面的方法,下面的方法现在给了我“失败”,但月份是 11,正如在 Log To Console month int=${month int} 中可以看到的那样。 ${month int} 获取当前日期 result_format=%m 记录到控制台 month int=${month int} ${month} = 设置变量 If '${month int}' >= 13 Fail ... '${month int }' == '01' January ... '${month int}' == '02' February ... '${month int}' == '03' March 等 登录到控制台月份字符串=${month } 比较字符串 css=div[class='a-month'] ${month}
    【解决方案2】:

    我不确定比较失败的原因(也许您正在将整数与字符串进行比较?)但您可以通过使用 dateTime 对象来规避问题:

    ${month int}  Get Current Date  result_format=datetime
    Log To Console  month int=${datetime.month}
    ${month} =  Set Variable If  ${datetime.month} >= 13  Fail
    ${month} =  Set Variable If  ${datetime.month} == 1  January
    ${month} =  Set Variable If  ${datetime.month} == 2  Febuary
    ${month} =  Set Variable If  ${datetime.month} == 3  March
    ....
    

    【讨论】:

    • 谢谢我试过这个,但不幸的是仍然没有'。我也尝试过转换为字符串,但这也没有解决。
    【解决方案3】:

    以下脚本可能会解决您的问题。请检查一次

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 2019-11-29
      • 2018-04-25
      • 2015-06-18
      • 2020-12-07
      • 1970-01-01
      • 2020-10-05
      • 2020-05-10
      相关资源
      最近更新 更多