user10593323
user10593323

Reputation: 47

Get all value from For loop in robot framework

Hi I am new in robot framework, I need to click in some element with xpath. I get the number of element and stored in a variable ${element} when I run my code it found ${element}=4

Now I want to click on each element. So i tried to get all index in a variable like ${i1} untill ${i4} The result should give me this:

    ${i1} = 0
    ${i2} = 1
    ${i3} = 2
    ${i4} = 3

i tried this

:FOR  ${i}  IN  RANGE   0   ${element}
\   log ${i}

But it gives result like

    20181101 19:21:07.269 : INFO : ${i}: 0
    20181101 19:21:08.269 : INFO : ${i}: 1
    20181101 19:21:09.269 : INFO : ${i}: 2
    20181101 19:21:10.269 : INFO : ${i}: 3

Thank you

Upvotes: 0

Views: 1965

Answers (2)

Sidara KEO
Sidara KEO

Reputation: 1709

In your case you should be use Nest Variable of robotframework . you can find my sample below and its should be solve your current problem.

test
     ${i}    Set Variable    i
    :FOR    ${n}    IN RANGE    4
    \   ${x}    Evaluate    ${n}+1
    \   Set Test Variable    ${${i}${x}}    ${n}
    log to console      ${i3}

and the result is same as you expetcation

${i1} = 0 ${i2} = 1 ${i3} = 2 ${i4} = 3

Upvotes: 1

A. Kootstra
A. Kootstra

Reputation: 6961

What you want to do can be achieved by using the keyword Set (Test/Suite/Global) Variable keyword. This takes two parameters and allows you to use variables in variables for creating the name.

*** Test Cases ***
TC
    ${element}    Set Variable    ${4}
    :FOR     ${i}     IN RANGE     0     ${element}
    \    Set Test Variable    ${i${i}}    ${i}

This results in the following Message Log in RED.

Starting test: Folder.Forloop I.TC
20181102 07:41:39.010 : INFO : ${element} = 4
20181102 07:41:39.033 : INFO : ${i0} = 0
20181102 07:41:39.044 : INFO : ${i1} = 1
20181102 07:41:39.055 : INFO : ${i2} = 2
20181102 07:41:39.064 : INFO : ${i3} = 3
Ending test: Folder.Forloop I.TC

Upvotes: 0

Related Questions