robot test case: for loop does not work - robotframework

I have following codes, some of them work but...(These sections are in 1 file and consecutive, split for clarification)
*** Settings ***
Documentation documentation for this test suite
... requires command line variables: IP
resource resource/applicationcontrol.robot
resource resource/clientmgr.robot
Suite Setup applicationcontrol.Open connections
Suite Teardown Close connections
Test Setup Exit Current Running Apps
*** Variables ***
#{apps} 1stapp 2ndapp 3rdapp 4thapp
=====================================================
*** Keywords ***
App For Port [Arguments] ${app}
:for ${port_num} IN 1 2
\ applicationcontrol.Launch application ${app} ${port_num}
\ ${status} Run Keyword And Return Status Check launching status ${app} ${port_num}
\ Continue For Loop If '${status}'=='False'
\ Run Keyword If '${status}'=='True' Add Success
\ Exit For Loop <--Line in question
Q: Exit For Loop exits the most enclosing For loop or all nesting For Loops?
======================================================
Add Success
${success_num} Evaluate ${success_num} + 1
Ratio Statistics
${fail_num} Evaluate ${total_num} - ${success_num}
Log Total: ${total_num}, Failed: <p style="color:red">${fail_num}</p> HTML
Exit Current Running Apps
:for ${port_num} IN 1 2
\ Exit application ${port_num}
\ Check exit status ${port_num} <--Line in question
Q: Check exit status ${port_num} gets executed only for 1st loop...don't understand why
======================================================
*** Test Cases ***
Launch and exit multiple apps [Arguments] #{apps}
${temp} Get Length ${apps}
Set Test Variable ${total_num} ${temp}
Set Test Variable ${success_num} 0
:for ${app} IN #{apps} <--Line in question
\ App For Port ${app}
\ Exit Current Running Apps
Ratio Statistics <--Line in question
Q: :for ${app} IN #{apps} This For Loop should have executed 4 times, but only executed for 1 time.
Q: Ratio Statistics never gets executed. How can I get it executed?
I cannot find answers on internet or User's Guide. Thanks in advance.
Additional:
resource/applicationcontrol.robot:
Exit application [Arguments] ${PORT}
${RUNNING_RESPONSE} send cmd appsvc get_apps
${RUNNING_APPS} response parameter ${RUNNING_RESPONSE}
Pass Execution If '${RUNNING_APPS}' == '${None}' No app running on ${PORT}
${TARGET_APP} get target application ${RUNNING_APPS} ${PORT}
send cmd appsvc exit_app ${TARGET_APP}

Exit For Loop exits the most enclosing For loop or all nesting For Loops?
Real nested for loops don't exist in Robot.
You can just have a loop that calls a keyword that has a loop.
Anyway, "Exit For Loop" exist the current loop (most enclosing if you want)
Check exit status ${port_num} gets executed only for 1st
for ${app} IN #{apps} This For Loop should have executed 4 times
Ratio Statistics never gets executed. How can I get it executed?We don't have enough information to help you on those questions. Yes, it should work the way you describe... See Brian's comment.

Thanks everyone, I found the answer.
In resource/applicationcontrol.robot:
Exit application [Arguments] ${PORT}
${RUNNING_RESPONSE} send cmd appsvc get_apps
${RUNNING_APPS} response parameter ${RUNNING_RESPONSE}
Pass Execution If '${RUNNING_APPS}' == '${None}' No app running on ${PORT}
${TARGET_APP} get target application ${RUNNING_APPS} ${PORT}
send cmd appsvc exit_app ${TARGET_APP}
The Pass Execution If statement exits current test case in question instead of the rest of that keyword, which is what I want it to be.
So my solution is putting the rest of this keyword into a new 'helper' keyword and using Run Keyword Unless instead to invoke the new keyword.

Related

Anyone have idea how to run linux command in robot framework at backend

I have to run this command
./emsInventory.sh -s 10 -i EMS1004 -p EMS -v 10.2.0.15.1 -d "EMS Patch " -c ems10/pass_ems10#rac_ems10.agnity.com
In robot framework to make sure that data is created or not
Use the Run Process in the Process library - this is its precise purpose.
You can use SSH Library in robot framework more details can be found here http://robotframework.org/SSHLibrary/SSHLibrary.html.
Below examples could be useful
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!

Execute command usage fails

*** Settings ***
Documentation Transfer a file from OCU to PC
Library SSHLibrary
Suite Setup Open Connection and Login With Public Key
Suite Teardown Close All Connections
*** Variables ***
${REMOTE HOST} 192.168.x.xxx
${USER} xxx
*** Keywords ***
Open Connection and Login With Public Key
Open Connection ${REMOTE HOST}
Login With Public Key ${USER} /home/bhushan/.ssh/id_rsa
*** Test Cases ***
Run the given command on the console
${pwd}= Execute Command pwd
Should Be Equal ${pwd} /home/root
${output}= Execute Command ls
${output}= Execute Command scp /home/root/myfile.txt bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/
Log ${output}
I have Dropbear of the latest version installed on my target. I am trying to run the above SSH Library Robot framework keyword based test case which defines to transfer a file from remote to the local host. The execute command keyword works in the beginning i.e. ${output}= Execute Command ls, but i am getting an error while using the same keyword in copying the files, i.e i can't see anything happening on the console. It is hanging and i waited for long time to see the output but haven't seen anything. But this works when i copy the files to my local machine manually. Can anyone here try to help me in fixing this error.
KEYWORD ${output} = SSHLibrary . Execute Command scp
/home/root/myfile.txt bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/
Documentation: Executes command on the remote machine and returns
its outputs. Start / End / Elapsed: 20170829 10:26:23.897 / 20170829
10:26:50.148 / 00:00:26.251 10:26:23.898 INFO Executing command 'scp
/home/root/myfile.txt
bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/'...................>
This is what i see in the log

robot framework: telnet execute command "Prompt is not set"

Trying to run this piece of code, but a "Prompt is not set" error keeps occurring at the Execute Command line.
*** Settings ***
Library Telnet
Library Telnet ${out}
Library Collections
Library Collections ${y}
Library Collections ${x}
*** Variables ***
${ip} 0.0.0.0
${port} 0
*** Test Cases ***
telnet to server
Open Connection ${ip} ${port}
verify something
${out}= Execute Command ls
${y}= Get From List ${out} 0
Should Match Regexp ${y} /^ID$/
Exit Test
Close All Connections
I have also tried deleting "Library Telnet ${out}" and replacing the " ${out}=
Execute Command ls" line with the following, but receive the same error.
Write ls
Set Prompt ${out}
${out}= Read Until Prompt
Is there a problem with the syntax? Or, is the usage of the "prompt" completely wrong? (if so, how can i fix this?)
(note: this is a first attempt at robot framework, so please feel free to comment on any other problems!)
Everything is in the Telnet docs. I use RED Robot Editor which can show me docs for Telnet and Telnet KW by hover over Telnet entry in editor, this can also be generated via command line:
python -m robot.libdoc Telnet show
There is a part about Prompt:
== Prompt ==
Often the easiest way to read the output of a command is reading all
the output until the next prompt with `Read Until Prompt`. It also makes
it easier, and faster, to verify did `Login` succeed.
Prompt can be specified either as a normal string or a regular expression.
The latter is especially useful if the prompt changes as a result of
the executed commands. Prompt can be set to be a regular expression
by giving ``prompt_is_regexp`` argument a true value (see `Boolean
arguments`).
Examples:
| `Open Connection` | lolcathost | prompt=$ |
| `Set Prompt` | (> |# ) | prompt_is_regexp=true |
Check Telnet docs for more help and examples.
ps. I don't see reason to import Telnet with parameter:
Library Telnet ${out}
Although I am too late to answer, I did not see the exact expected answer hence answering this now.
You need to set 2 things in Open Connection.
prompt_is_regexp=yes
prompt=#{Your expected prompt}

How to check if unix process is running using robot framework

I am trying to check if the process is actively running from the unix system.It can be one process or multiple process .Following is the example that i am trying. Can someone guide me how this can be achieved ?
*** Settings ***
Library Process
Library OperatingSystem
*** Test cases ***
Example
${output} = Run process /etc/init.d/bluetooth
${op}= Is Process Running ${output}
Should be equal ${op} True
[root#test ssh-scripts]# pybot test-process.robot
==============================================================================
Test-Process
==============================================================================
Example | FAIL |
Non-existing index or alias '<result object with rc 3>'.
------------------------------------------------------------------------------
Test-Process | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
/etc/init.d/bluetooth is startup script, you won't to check the startup script but rather the process(es) started by it
Keyword Run Process waits till the process terminates. You probably won't wait for termination, you rather want to run the process on background.
This works as expected for me:
Test Processes
${handle} Start Process echo foo stdout=/tmp/bar
${output} Is Process Running handle=${handle}
Should Not Be True ${output}
${handle} Start Process yes stdout=/dev/null
${output} Is Process Running handle=${handle}
Should Be True ${output}
[Teardown] Terminate All Processes
I.e. echo terminates earlier than the first Is Process Running check is performed. But yes is started and keeps running in the moment of second Is Process Running.
Following script worked for me . I have placed a regex pattern to match the string after command is executed. This can achieved in alternate way but this served my purpose.
*** Settings ***
Library Process
Library SSHLibrary
Suite Setup Open Connection And Log In
Suite Teardown Close All Connections
Library OperatingSystem
*** Variables ***
${HOST} 16.10.90.24
${USERNAME} root
${PASSWORD} admin123
${postgres_cmd} /etc/init.d/DB_Platform_TaskManager status
${PATTERN} (?m).*is\ running
*** Test Cases ***
Check whether postgres is running
Open Connection And Log In
Start Command ${postgres_cmd}
${rc}= Read Command Output return_stdout=True return_rc=False
Log ${rc}
Should Match Regexp ${rc} ${PATTERN}
*** Keywords ***
Open Connection And Log In
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}

Setting up SSH in robot framework with multiple hosts

Within robot framework, I can use a for loop in each test case to Start an SSH connection to each host within a list, and then run commands on that host.
But it seems that the ssh setup happens in every test case which takes a considerable amount of time.
Is there some way in which I can make the connection persistent for every test case in a test suite?
Example Code:
*** Settings ***
Variables sys_variables.py
Resource ${RESOURCES}/common.robot
*** Test Cases ***
Ping :FOR ${HOST} IN #{REACHABLE}
\ SSH to ${HOST} ${USER} ${PASS}
\ ${result} = Run and Log ${PING_GOOGLE_DNS}
\ Should Be Equal As Integers ${result} 0
\ log ${result}
This works, but I'd like to not have to run it in every testcase.
Just put the tests in a single suite and make the connections in the suite setup. The suite setup is executed one time, before the actual test executions.
Consider the following example code:
*** Settings ***
Suite Setup SuiteSetup
*** Test Cases ***
Test1
Log This is the test 1
Test2
Log This is the Test 2
*** Keywords ***
SuiteSetup
Log This is the single setup
Put your connection cycle inside the SuiteSetup keyword and you're done.

Resources