taibc
taibc

Reputation: 923

How to using global variable robotframework?

I want to increase my variable through loop. How and where can I declare it (such as: {myvar} = 0)

*** Test Cases ***

Start to Login
    Log     ${myvar}

*** Keywords ***

Start to Login
    [Arguments]    ${LIST}
    : FOR    ${LINE}    IN    @{LIST}
    \    ${myvar}=    Evaluate    ${myvar} + 1
    \    Log    ${myvar}

Upvotes: 0

Views: 671

Answers (2)

A. Kootstra
A. Kootstra

Reputation: 6961

An alternative to the answer from @pankaj-mishra is the following. This removes the evaluate and uses Set Variable with simple arithmetics to increase the value. It is important to start with a numerical value. This is why the variable is created with ${0} to ensure that the 0 is indeed a number and not a string.

*** Test Cases ***
test counter
    ${counter}    Set Variable     ${0}

    :FOR    ${item}    IN RANGE    10
    \    ${counter}    Set Variable    ${counter+1}
    \    Log    ${counter}

Upvotes: 1

pankaj mishra
pankaj mishra

Reputation: 2615

have a look on this

*** Settings ***

*** Variables ***
@{LIST}    5    6    7
${myvar}

*** Test Cases ***
Check
    Start to Login    ${LIST}


*** Keywords ***
Start to Login
    [Arguments]    ${LIST}
    :FOR    ${LINE}    IN    @{LIST}
    \    ${myvar}=    Evaluate    ${myvar} + 1
    \    Log to console    ${myvar}

output

Check                                                                 
  1
  2
  3

Upvotes: 1

Related Questions