【问题标题】:Robotframework: Getting date at runtime using Get Current Date, in specific formatRobotframework:使用特定格式获取当前日期在运行时获取日期
【发布时间】:2014-09-14 08:05:12
【问题描述】:

我使用Get Current Date 关键字以年-月-日格式返回日期。我使用这些特定信息来确保帐户的 created 时间戳在自动化测试中是正确的。

问题是关键字无法识别,我的代码应该是正确的(它应该可以工作并且它应该以我希望的格式生成日期。

*** Keywords ***

Initialize Test Data
    ${DATE}=    Get Current Date    result_format=timestamp
    ${MYNUM}=    faker.Random Int

    Set Suite Variable  ${MYNUM}
    Set Suite Variable  ${DATE}

为什么我会收到错误No keyword with name 'Get Current Date' found.

提前致谢。

【问题讨论】:

    标签: date testing selenium-webdriver automation robotframework


    【解决方案1】:

    标准 RF 库中是否存在关键字获取当前日期?有一个名为 Get Time 的内置关键字。文档解释了如何格式化输出。要使用 Get Current Date,您需要先导入 DateTime 库。

    更新: 适合我的 RF 脚本示例:

    *** Settings ***
    Library           DateTime
    
    *** Test Cases ***
    datatimetest
       ${d}=    get time
       log    {d}
       ${d}=    Get Current Date    result_format=%Y-%m-%d
       log    {d}
       ${d} =    Add Time To Date    2014-05-28 12:05:03.111    7 days
       log    {d}
    

    请记住,DateTime 是一个新库,所以如果您有旧版本的 Robot Framework,您需要安装库或升级 RF。

    【讨论】:

    • 如果您查看获取当前日期的链接,它是否存在,并且我正在使用该特定版本的 RF.. 无论如何,如果我想使用 Get Time 我怎么能截断输出,以便我有 date-month-day ?
    • 是的,我在发布答案后意识到这一点。但是它不是内置关键字。您需要导入 DateTime 库。我已经更新了我的答案。
    • 根据文档,${DATE}= DateTime.Get Current Date result_format=timestamp 的输出应该是(至少今天)2014-07-23,对吗?
    • 我认为您不需要“日期时间”。在您的关键字之前。我正在将 RF 脚本示例添加到我的答案中。
    • 谢谢。我有日期时间。作为前缀,因为当我的同事查看此文件时,我将名为“DateTime”的库用于文档/学习目的。
    【解决方案2】:

    我在 Python 中使用 RF,默认情况下我的 IDE 会检测 Python DateTime 库。 尝试使用完整路径:

    Library           robot.libraries.DateTime
    

    robot.libraries.DateTime.

    【讨论】:

      【解决方案3】:

      您还可以根据需要格式化时间。见this answer。它提供格式建议。您可以使用 Python DateTime 函数,如

         ${now}    Evaluate    '{dt.day}/{dt.month}/{dt.year}'.format(dt=datetime.datetime.now())    modules=datetime
         Log   ${now}
         Log to Console   now time 1: ${now}
         ${now}    Evaluate  '{dt:%A}, {dt:%B} {dt.day}, {dt.year}'.format(dt=datetime.datetime.now())    modules=datetime
         Log   ${now}
         Log to Console   now time 2: ${now}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-17
        • 2013-08-17
        • 1970-01-01
        • 2021-04-03
        • 2016-09-27
        • 2019-10-01
        • 2020-09-19
        • 1970-01-01
        相关资源
        最近更新 更多