IF ELSE Robotframework 4 – stuck in if condition

Question:

I am building a practice test with Robotframework/Selenium. I am trying to incorporate and IF/Else statement (that got enabled in RF4), but I am running into a problem. I created the following keyword to set the variable value I want to use in my else if statement:

Get Type of train
${trainTypeCode}=  Get Text    xpath= <xpath>
Set Test Variable    ${trainTypeCode}

This should either return IC or SPR, which I want to use in the following if else statement:

Verify trip duration
    IF    ${trainTypeCode} == SPR
        Should Be Equal    0:23    ${tripDuration}
    ELSE IF     ${trainTypeCode} == 'IC'
        Should Be Equal    0:15    ${tripDuration}
    ELSE
        Fail
    END

So based on type of train I want to test whether the trip duration is correct. However, I get the following error:

Evaluating expression 'SPR == SPR' failed: NameError: name 'SPR' is not defined nor importable 
as module

I tried using quotes and converting my variable to string, but I got the same error.

Later on I want to set something up so that it can check multiple results that I get from the NS (Dutch railways) travelplanner and check their trip duration based on train type. So I need something that can handle both train codes without having to predefine them.

I hope someone knows what I am doing wrong, it is hard to find much information as the IF/ELSE thing I try to use is from the newest version of robot framework.

Asked By: Jealgu

||

Answers:

So it is failing on the IF conditions. Try like this:

Verify trip duration
    IF    "${trainTypeCode}" == "SPR"
        Should Be Equal    0:23    ${tripDuration}
    ELSE IF     "${trainTypeCode}" == "IC"
        Should Be Equal    0:15    ${tripDuration}
    ELSE
        Fail
    END
Answered By: Helio