Sai Vikhyath
Sai Vikhyath

Reputation: 31

Error while incrementing the value in Robot Framework

*** Settings ***
Library  DateTime

*** Test Cases ***
Test title
    ${TIME}=  get current date  result_format=%H
    RUN KEYWORD IF
    ...  int(${TIME})%2==0
    ...  ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
    ...  ELSE
    ...  ${TIME}=  Evaluate  int(${TIME})+1
    ...  ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
    log to console  ${TIME}

I'm getting the following error:

No keyword with name '11=' found.

Upvotes: 1

Views: 207

Answers (1)

Todor Minakov
Todor Minakov

Reputation: 20067

You cannot have a variable assignment inside Run Keyword If - as its name goes it's only for running keyword, and doesn't support assignments (it treats the variable as another keyword it has to run). Thus the error - the framework substituted ${TIME} with its value (11), and tried executing that. In version 4.0 it supports proper IF/ELSE blocks, where this restriction doesn't apply:

Test title
    ${TIME}=  get current date  result_format=%H
    IF    int(${TIME})%2==0
         ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
    ELSE
         ${TIME}=  Evaluate  int(${TIME})+1
         ${TIME}=  catenate  SEPARATOR=  ${TIME}  :00
     END
    log to console  ${TIME}

In the mean time, you can solve it with another approach - using Set Variable If. It's sumilar to Run Keyword If that it's conditional, but takes only values.
There's a functionality in the framework that you can do calculations in place (strictly speaking - calling methods), that we'll employ - increment the value with 1:

Test title
    ${TIME}=  get current date  result_format=%H
    ${TIME}=  Convert To Integer    ${TIME}    # to be sure it's type is this, I haven't checked what the keyword returns
    ${TIME}=    Set Variable If     ${TIME}%2==0    ${TIME}:00
                         ...           ${TIME + 1}:00    # this is the ELSE block
    log to console  ${TIME}

Upvotes: 1

Related Questions