How to save multiple output values received from For Loop of one script to variables.robot

Hi, I am using Robot Framework, Selenium in Pycharm. I want;

  1. To save multiple return values in different variables.
  2. These variables I need in variabls.robot.
  3. After that I want to fetch these values one by one from variables.robot in the for loop of different scripts.

Below is the requirement example.

I have below web table which I access in my first script.

  1. I want to navigate through all the rows and get the values of Product ID one by one (In first script). Here I am using loop to get the values and processing it.
  2. Then want to save all the Product IDs in different variables in variable.robot file
  3. Finally want to call these product IDs in multiple other scripts. In few scripts I need one or two id’s and in couple of scripts I need them to bring one by one in loop.

I was thinking about list but not able to acheive it.

  • Please suggest if any solution is with list too.
  • Want to know how to save multiple outputs received from For Loop to different variables.
  • also want to know how to save these values from first script to variables.robot so it will be easy call these values any other scripts.

I am getting list in one script but how to send it to variables.robot or call these values one by one in other scripts?

@{list}   Create List
    Append To List   ${list}   ${ProductID}

You can save to a CSV file and then read them.
Or you can save as variable file (.yml, .json, .py) and they import. In this case you need to have more complex data structures.

1 Like

I am using below code to create the list

${list}=   Create List   ${ProductID}
    ${data}=    create list    ${list}
    Append To Csv File     ProductID.csv     ${data}

I am gettng below CSV

Then calling saved values in another script like

${rows} =  get element count   xpath=//*[@id="YearlyReport"]/tbody/tr
    sleep  2
    log to console   Total Rows ${rows}

  ${ProductID}=     Read Csv File To List      ProductID.csv
  FOR    ${element}    IN    @{ProductID}
        Log To Console   ${element}[0]

   FOR    ${rowno}    IN RANGE    1    ${rows}+1

    Input Text    xpath=//input[@placeholder='Search ...']    ${element}[0]
    sleep   5


    ${Details} =   Get Text    xpath=//*[@id="YearlyReport"]/tbody/tr[${rowno}]/td[10]
    log to console  In Search details of ${element}[0] is  ${Details}

I am getting below error

List ‘${element}’ has no item in index 0. If I change value to 1 then it gives same error List ‘${element}’ has no item in index 1.

Any suggestions ?

1 Like

Here is an example of dealing with CSV lists:

Language: English

*** Settings ***
Library           CSVLibrary
Library           OperatingSystem    AS    OS

*** Test Cases ***
Save data
    [Setup]    Create File    ${CURDIR}${/}ProductID.csv
    ${ProductID}=    Create List    A1    B1    C1    D1    E1
    Append To Csv File    ${CURDIR}${/}ProductID.csv    ${ProductID}

Read data
    ${ProductID}=    Read Csv File To List    ${CURDIR}${/}ProductID.csv
    Log Many    ${ProductID}
    # We get [['A1', 'B1', 'C1', 'D1', 'E1']]
    # So, we must select element 0 of this list
    ${products}=    Set Variable    ${ProductID[0]}
    FOR    ${element}    IN    @{products}
        Log    ${element}
    END
    [Teardown]    OS.Remove File    ${CURDIR}${/}ProductID.csv
2 Likes

Thanks ! It is working.

${products}=    Set Variable    ${ProductID[0]}
FOR    ${element}    IN    @{products}
Log    ${element}