2015-11-11 79 views
1

我是Robot Framework的新手,在運行下面的變量$ {month}時會返回'none'。設置變量如果返回無機器人框架

我想要做的是將月份數字轉換爲文本中的相關月份,例如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} 

回答

0

我不知道爲什麼比較失敗(也許你比較整數爲字符串?),但你可以通過使用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 
.... 
+0

感謝我試過,但不幸的是仍然有「無」。我也嘗試轉換爲字符串,但這並沒有解決。 –

0

你的代碼正是你要求他做的事情。看它:在最後一行你說

${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"

+0

我嘗試了上面的內容,下面現在給我'失敗',但是月份是11,可以在日誌到控制檯month int = $ {month int}中看到。 $ {month int}獲取當前日期result_format =%m 記錄到控制檯month int = $ {month int} $ {month} =設置變量如果'$ {month int}'> = 13失敗 ...'$ {month int}'=='01'January ...'$ {month int}'=='02'Febuary ...'$ {month int}'=='03'March etc Log到控制檯月份字符串= $ {month} 比較字符串css = div [class ='a-month'] $ {month} –

0

下面的腳本可能會解決您的issue.Please檢查一次

enter image description here