我正在使用 Robot Framework 关键字
Subtract Date From Date
来测试日期是否在 2 个日期之间(例如 11/06/2020 是在 07/06/2020 和 20/06/2020 之间),但我收到错误在我的日志文件中:
值错误:时间戳无效`。
日期是从
String
中提取的 Span Text
,我将其转换为日期。
这是我的代码:
${date}= Get Current Date result_format=datetime
${postdate}= Get Text xpath://app-post[1]//div[1]//div[1]//div[1]//div[2]//div[1]//div[2]//div[1]//div[2]//span[2]
${postdate}= Convert Date ${postdate} date_format=%d/%m/%y
${date}= Convert Date ${date} result_format=%d/%m/%y
${temp} = Subtract Date From Date ${postdate} ${date}
Should Be True ${temp}>=0
问题是您没有遵循此处提到的关键字
Subtract Date From Date
允许的格式:https://robotframework.org/robotframework/latest/libraries/DateTime.html#Date%20formats
%y
会给你20
,但这显然还不够。如果我将其更改为 %Y
,它将为我提供当前年份的 2020
,并且关键字 Subtract Date From Date
不再抱怨无效的时间戳。您还需要将其更改为date_format=%Y/%m/%d
之类的内容,即更改%Y
、%m
和%d
的顺序。
编辑:
我现在可以看到您还在使用 date_format 以及 result_format 和
Convert Date
。这是代码中的另一个错误,您需要将 result_format
与两者一起使用。
编辑2:
完整的工作示例:
(这里没有使用内置库,这些日期时间关键字来自 DateTime 库)
以下脚本有效(使用静态 PAST_DATE 检查示例运行):
${date}= Set Variable 21
${month}= Set Variable 12
${year}= Set Variable 2019
${PAST_DATE}= BuiltIn.Catenate SEPARATOR=/ ${year} ${month} ${date}
Log ${PAST_DATE}
${CURRENT_DATE}= Get Current Date result_format=%Y/%m/%d
Log ${CURRENT_DATE}
${DIFFERNCE_IN_DAYS}= Subtract Date from Date ${CURRENT_DATE} ${PAST_DATE} verbose
Log ${DIFFERNCE_IN_DAYS}
@{total_days}= Split String ${DIFFERNCE_IN_DAYS}
${final_days}= Set Variable ${total_days[0]}
Log ${final_days}
Should be true ${final_days} >= 0