How to set a ${variable} from text file

1,381 views
Skip to first unread message

Thibaut Leger

unread,
Jul 10, 2014, 9:26:28 AM7/10/14
to robotframe...@googlegroups.com
Hi all,

First of all, I'm a real novice in using robotframeworks and try to use it to allow me to connect on different server via SSH to check first the connectivity, and then login.
I have found this good script which is perfect when you have to test max 15 servers but when you have to test around 100 system, there is no benefit on using a single test at 1 and having to update variable manually for each server.

Here is the code that I have found using the SSH Library:


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*** Settings ***
Documentation     This example demonstrates executing commands on a remote machine
...               and getting their output and the return code.
...               Notice how connections are handled as part of the suite setup and
...               teardown. This saves some time when executing several test cases.
Suite Setup       Open Connection And Log In    # Open Connection And Log In
Suite Teardown    Close All Connections    # Close All Connections
Library           SSHLibrary

*** Variables ***
${HOST}           10.56.103.181
${USERNAME}       <login>
${PASSWORD}       <password>

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to ran commands on the remote machine.
    ...    The keyword returns the standard output by default.
    ${output}=    Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}    Hello SSHLibrary!

Execute Command And Verify Return Code
    [Documentation]    Often getting the return code of the command is enough.
    ...    This behaviour can be adjusted as Execute Command arguments.
    ${rc}=    Execute Command    echo Success guaranteed.    return_stdout=False    return_rc=True
    Should Be Equal    ${rc}    ${0}

Executing Commands In An Interactive Session
    [Documentation]    Execute Command always executes the command in a new shell.
    ...    This means that changes to the environment are not persisted
    ...    between subsequent Execute Command keyword calls.
    ...    Write and Read Until variants can be used to operate in the same shell.
    Write    cd ..
    Write    echo Hello from the parent directory!
    ${output}=    Read Until    directory!
    Should End With    ${output}    Hello from the parent directory!

*** Keywords ***
Open Connection And Log In
    Open Connection    ${HOST}
    Login    ${USERNAME}    ${PASSWORD}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

On the above script what I try to found on how to do is how to take data from a txt or csv file containing a list of IP@ to be used by the variable ${host}
I have read different doc on this topic, but as I'm not a developper, there is a lot of point that I don't understand on how to do as their no concrete example shown.

The variable text file can have an open format but I was looking for a simple way as follow:

ip@1
ip@2
ip@3
...
...
...
IP@xxx

Thanks in advance for your help.

Thibaut




Tatu Aalto

unread,
Jul 11, 2014, 3:41:08 PM7/11/14
to leg...@gmail.com, robotframe...@googlegroups.com
Ugh

So basically you have a list of IPs and pick one of them when you start to execute a test suite, correct? I can think many ways to solve the problem, but it all depends how you would select the correct IP from list/source? In a round robin manner, take a random pick, based on some other settins or something else? If you could describe how yo want to select the correct IP, then perhaps we could point you the right direction.

-Tatu
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

legethi

unread,
Jul 11, 2014, 6:14:41 PM7/11/14
to robotframe...@googlegroups.com, leg...@gmail.com

Hi tatu,

Well the way to select ip@ is not the most important as all ip@ have to be test. So i was more thinking by going from the first to the last
Thanks for your help
KR
Thibaut


Tatu Aalto

unread,
Jul 12, 2014, 7:27:00 AM7/12/14
to leg...@gmail.com, robotframe...@googlegroups.com
Ugh

Example like this:
*** Settings ***
Suite Setup       suite setup
Test Setup        test setup

*** Variables ***
@{IP LIST}        1    2    3    4

*** Test Cases ***
IP1
    Log    ${IP FOR TEST}

IP2
    Log    ${IP FOR TEST}

*** Keywords ***
test setup
    ${IP FOR TEST}    Set Variable    @{IP LIST}[${INDEX}]
    ${INDEX}    Evaluate    ${INDEX}+1
    Set Test Variable    ${IP FOR TEST}
    Set Global Variable    ${INDEX}

suite setup
    ${INDEX}    Set Variable    0
    Set Global Variable    ${INDEX}

There are other ways too, reading the User Guide (example for loops), Collections library provide good ideas how to do what you need.

-Tatu

legethi

unread,
Jul 12, 2014, 5:41:14 PM7/12/14
to robotframe...@googlegroups.com, leg...@gmail.com
Hi Tatu,

Thanks for this exemple but I have few question. First how can I have to setup suite in same file? as per the script that I have copy, I have already the following:

Suite Setup       Open Connection And Log In    # Open Connection And Log In
Suite Teardown    Close All Connections    # Close All Connections

If I try to add the Setup Suite setup suite as you mention, I have a sanity check error.

My second question is based on the following part related to variable,


*** Variables ***
@{IP LIST}        1    2    3    4

If my understanding is correct this is a list of variable set in the test scenario. May I correct? in this case how to use instead an external file.
Yesterday I have found this kind of script to open an excel file as per below:

First it's set this library openpyxl.reader.excel.

Then below script is use to open the excel file and look at the value line per line:

Excel sheet
    ${wb} = load_workbook iphost.xlsx
    ${ws} = set variable ${wb.get_active_sheet()}
    ${cell} = set variable ${ws.cell('A1')}
    ${HOST} = set variable ${cell.value}
But now my issue with this test is that when my script start by opening the SSH connection using the value of ${HOST} and make error with saying the there is no variable called ${HOST}
Should I have to apply then this command Set Test Variable   ${HOST} to allow my script to have the IP@ set?
Thanks in advance
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Tatu Aalto

unread,
Jul 13, 2014, 10:37:56 AM7/13/14
to robotframe...@googlegroups.com
Ugh

1) Setups/Teardowns are just executing one keyword. You can put any logic in to the keyword that you want. My example was just generic example to show how you can choose value from a list one at the time. User guide [1] gives good documentation how to use Setups/Teardowns correctly.
2) Reading the user guide variable [2] chapter gives you answer on this question.
3) Set variable keyword does not make the variable available globally. User guide exaplains this in the variable scope and priorities chapter [3].

-Tatu
[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-setup-and-teardown
[2] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#variables
[3] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#variable-priorities-and-scopes
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages