Husain Ashraf
Husain Ashraf

Reputation: 251

Set Variable If returns none robot framework

I am a newbie to Robot Framework and when running the below the varible ${month} reutns back 'none'.

What I am trying to do is convert the month as a number to its relevant month in text e.g. 1 to January, 4 to April etc.

I am sure this will be a simple fix but I have no idea what it is ... Any help would be very much appreciated.

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

Upvotes: 1

Views: 5951

Answers (3)

supraja reddy
supraja reddy

Reputation: 19

Below script might solve your issue.Please check once

enter image description here

Upvotes: 0

Psytho
Psytho

Reputation: 3384

Your code is doing exactly what you ask him to do. Look at it: in the last line you say

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

And thus ${month} is set to None because ${month int} is not 12 and None is the default value for the case that the given condition is not true. You need to use else if logic and this is done this way in 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

See Documentation to "Set Variable If".

Upvotes: 2

Uri Shtand
Uri Shtand

Reputation: 1737

I'm not sure why the comparison fails (maybe you're comparing integer to a string?) but you can circumvent the problem by using the dateTime object:

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

Upvotes: 0

Related Questions