user7784919
user7784919

Reputation:

Handle an Argument varible as a Return value in Robot Framework

I'm having an issue while returning an Argument variable in Return after some manipulation.

Kindly look at the code : Note this is a sample code

*** Settings ***
Library    Selenium2Library
Library    Collections

*** Keywords ***
Parent Routine
    ${index}    Set Variable    0
    ${index}    Set Variable      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Set Variable      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}

Grand Child Routine
    [Arguments]    ${index}
    : For    ${i}     IN RANGE    1    5
    \    ${index}    Set Variable      ${index} +  1
    #\    Some other manipulation
    [Return]    ${index}

*** Test Cases ***
Sample Test Case
    [Documentation]   Simple test for Return Value
    Parent Routine

Kindly look at the output window

enter image description here

Most probably the expected output is 5 but it showing [u'Child Routine', u'0']

Kindly assist me how to get the expected output.

Upvotes: 1

Views: 2263

Answers (3)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Setting variables

You are using set variable wrong. That keyword is for setting the value of a variable, it is not for calling other keywords.

Consider the following code:

${index}    Set Variable      Child Routine     ${index}

You are creating a list where the first value is the literal string "Child Routine" and the second value is whatever is in ${index}, and then you are setting the variable ${index} to that list.

If you want to call a keyword and save its return value, all you do is make that keyword the first thing after any variables. For example, in The following code we call Child Routine, passing it ${index} as its only argument. The result will be saved in ${index}.

${index}    Child Routine     ${index}

Doing math

You can't just add "+ 1" to a variable. Robot isn't exactly a programming language. If you want to add one to a variable you will need to use some keyword to do that. The built-in keyword Evaluate can be used for such purposes.

In other words, instead of this:

${index}    Set Variable      ${index} +  1

You need to do this:

${index}   evaluate   ${index} + 1

Upvotes: 1

ILostMySpoon
ILostMySpoon

Reputation: 2409

That is because you are attempting to call your user keywords by passing it to Set Variable which only takes in String values. When you pass in multiple values, you end up with a List.

If you want to call your user keywords and assign its return value to a variable, you want to use Run Keyword

Parent Routine
    ${index}    Set Variable    0
    ${index}    Run Keyword      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Run Keyword      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}

Upvotes: 0

B.Balamanigandan
B.Balamanigandan

Reputation: 4875

Yes, we can use...

In your code you missed the Evaluate keyword. Before proceeding any mathematical operation we should use the Evaluate keyword.

Only change is ${index}= Evaluate ${index} + 1

Finally your code is

*** Settings ***
Library    Selenium2Library
Library    Collections

*** Keywords ***
Parent Routine
    ${index}    Set Variable    0
    ${index}    Set Variable      Child Routine     ${index}
    log to console    ${index}

Child Routine
    [Arguments]    ${index}
    ${index}    Set Variable      Grand Child Routine    ${index}
    #\    Some other manipulation
    [Return]    ${index}

Grand Child Routine
    [Arguments]    ${index}
    : For    ${i}     IN RANGE    1    5
    \    ${index}=    Evaluate      ${index} + 1
    #\    Some other manipulation
    [Return]    ${index}

*** Test Cases ***
Sample Test Case
    [Documentation]   Simple test for Return Value
    Parent Routine

Upvotes: 0

Related Questions