Carbuary
Carbuary

Reputation: 11

Adding items to list variable in Robot Framework

I have this code where I am trying to loop through Similar Xpaths to get their text and add the text value to a list using robot farmwork

: FOR    ${i}    IN RANGE    2    ${count}+1
\  sleep  10s
\  ${j}   Get Text    //*[@id="stats"]/div/div/div[2]/div/div/div/div[${i}]/div[1]
\  Append To List     @{dbws_datapoints}    ${j}
\  log  @{dbws_datapoints}[${j}]

The code executes without errors but it is not printing the list and it is logging the result as follows in the logfile

FOR ${i} IN RANGE [ 2 | ${count}+1 ]

I am attaching the screenshots as well. Result file Image

Test Script Image

Please help

Upvotes: 0

Views: 16119

Answers (2)

A. Kootstra
A. Kootstra

Reputation: 6961

Although the images you linked to provided the context, it's always good to put the text version in your question. In addition it is expected to provide a minimal example, that does not rely on items not provided. The xpath and Get Text keyword are the examples here.

As already mentioned by @Bryan Oakley, the core of your issue seems to be the @{} that should be ${}. In addition you need to update your variable reference, as this is should be inside the curly braces example: ${var[2]}. In your example you started your loop from 2. When working with lists this poses an issue, as items added to a new list, always start with 0. For this reason an additional line was added to count the items in the list.

The below example code work provides the desired entries in the log file.

*** Settings ***
Library    String
Library    Collections

*** Variables ***
${count}              5
@{dbws_datapoints}    

*** Test Cases ***
FOR LOOP
    : FOR    ${i}    IN RANGE    0    ${count}
    \  ${j}   Get Text    ${i}
    \  ${listCount}    Get Length    ${dbws_datapoints}
    \    
    \  Run Keyword If    (${i}>2)     Append To List     ${dbws_datapoints}    ${j}
    \  Run Keyword If    (${i}>2)     Log To Console  ${dbws_datapoints[${listCount}]}

*** Keywords ***
Get Text
    [Arguments]    ${i}
    ${list}    Create List    aaa    bbb    ccc    ddd    eee
    [Return]    ${list[${i}]}

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386020

Use $ when referring to a list as an object

Append To List    ${dbws_datapoints}  ${j}
...
log  ${dbws_datapoints}[${j}]

For more information see List Variables in the robot user guide.

Upvotes: 5

Related Questions