IF ELSE in robot framework with variables assignment

246 views
Skip to first unread message

Srinivasan Shanmugam

unread,
Nov 22, 2018, 10:40:22 AM11/22/18
to robotframework-users
Dear Robot framework experts,

As I am newbie to robot framework, this is regarding IF else statements in robot framework

I am automating the Wi-fi testing, ie., I need to check for wifi connectivity, only if it is true then perform the ping and get the signal strength in the below code snippet.

And also, could you please let me know how can I run these tests in isolation (for example to get the Wifi Ip and gateway Ip only if it is connected, or to perform the ping only if it is connected or to get the signal strength only if it is connected or so.. because with the below code snippet it has to always execute the file from top to bottom. 



*** Variables ***
${SSID}                           NasdfghFunkloch
${PW}                             0160717268088099898
${SIGNAL_LEVEL_THRESHOLD}         -90

*** Test Cases ***
Connect Wi-Fi to SSID
    ${RET} =     Wifi Disconnect Then Connect      ${SSID}      ${PW}
    Log    WIFI SSID: ${SSID}
    Log    WIFI PW: ${PW}
    Should Be True    ${RET}

Check for Wi-Fi Connectivity
    ${RET} =    Wifi Connect Verify
    Should Be True    ${RET}    

Get Gateway IP and Check Whether Wi-Fi is Pingable
    ${WIFI_MODULE_IP} =     Get Wifi Ip
    ${GATEWAY_IP} =     Get Gateway Ip
    ${RET} =    Wait Until Device Is Pingable     ${GATEWAY_IP}
    Should Be True    ${RET}

Check for Wi-Fi Signal Strength with Threshold
     Log    Wi-Fi Signal Strength Threshold: ${SIGNAL_LEVEL_THRESHOLD}
     ${link}     ${level}     ${noise} =     Get Wifi Signal Quality
     Should Be True      ${level} >= ${SIGNAL_LEVEL_THRESHOLD}


Thanks in advance

srinivasan

unread,
Nov 22, 2018, 4:09:21 PM11/22/18
to robotframe...@googlegroups.com
Could anybody please provide inputs how am I going to implement this? in robot framework?

Many Thanks in advance

--
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 https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Viswanath Nagarajan

unread,
Nov 22, 2018, 10:24:37 PM11/22/18
to sriniva...@gmail.com, robotframe...@googlegroups.com
Hi Srinivasan,

If you put all your testcase under a keyword section and call all these keywords in a single testcase you can achieve what you want. Please do like below

*** Keywords ***
Connect Wi-Fi to SSID
    ${RET} =     Wifi Disconnect Then Connect      ${SSID}      ${PW}
    Log    WIFI SSID: ${SSID}
    Log    WIFI PW: ${PW}
    Should Be True    ${RET}

Check for Wi-Fi Connectivity
    ${RET} =    Wifi Connect Verify
    Should Be True    ${RET}    

Get Gateway IP and Check Whether Wi-Fi is Pingable
    ${WIFI_MODULE_IP} =     Get Wifi Ip
    ${GATEWAY_IP} =     Get Gateway Ip
    ${RET} =    Wait Until Device Is Pingable     ${GATEWAY_IP}
    Should Be True    ${RET}

Check for Wi-Fi Signal Strength with Threshold
     Log    Wi-Fi Signal Strength Threshold: ${SIGNAL_LEVEL_THRESHOLD}
     ${link}     ${level}     ${noise} =     Get Wifi Signal Quality
     Should Be True      ${level} >= ${SIGNAL_LEVEL_THRESHOLD}

*** Test Cases ***
Testcase-1
    Connect Wi-Fi to SSID
    Check for Wi-Fi Connectivity
    Get Gateway IP and Check Whether Wi-Fi is Pingable
    Check for Wi-Fi Signal Strength with Threshold

Thanks,
Viswanath
--
Regards,
N.Viswanath

srinivasan

unread,
Nov 23, 2018, 2:41:30 PM11/23/18
to visu...@gmail.com, robotframe...@googlegroups.com
Dear Viswanath and Robot framework users,

Thanks a lot for your quick responses and appreciate the same..

Sorry for any misunderstanding, I hope whatever you have described is not the use case that I have to be implemented.

My requirement is: 

1. If I am connected to wifi only ie., only when testcase "Connect Wi-Fi to SSID" is "True" I should go and check for the keyword "Check for Wi-Fi Connectivity" otherwise I should fail the testcase " Connect Wi-Fi to SSID " and don't execute "Check for Wi-Fi Connectivity"

2. But in the above Scenario, where I have posted my code, when the test case "Connect Wi-Fi to SSID"  fails it still continues to execute "Check for Wi-Fi Connectivity" where this should not get executed until or unless " "Connect Wi-Fi to SSID" is true

3. Similarly only when  test case " Check for Wi-Fi Connectivity " is "True", I should go and execute "Get Gateway IP and Check Whether Wi-Fi is Pingable" and "Check for Wi-Fi Signal Strength with Threshold" otherwise fail the testcase and don't execute ""Get Gateway IP and Check Whether Wi-Fi is Pingable" and "Check for Wi-Fi Signal Strength with Threshold""

Could anybody please provide me some robot framework code syntax for code snippet for my above requirement.

As am stuck with this robot framework syntax issues form past 2 days, and wondering for no clues yet..

Kindly do the needful

Many Thanks in advance,



srinivasan

unread,
Nov 23, 2018, 3:27:48 PM11/23/18
to Viswanath Nagarajan, robotframe...@googlegroups.com
Sorry I forgot to mention some thing else.

Connect Wi-Fi to SSID, Check for Wi-Fi Connectivity, Get Gateway IP and Check Whether Wi-Fi is Pingable and Check for Wi-Fi Signal Strength with Threshold" seems to be comments in Robotframework I think so.

I need to accomplish as below:

1.     Only if "${RET}" of  "${RET} =     Wifi Connect      ${SSID}      ${PW}" is "True" go and execute "${RET} =    Wifi Connect Verify" otherwise fail this test case and don't execute the below statements.

"${RET} =    Wifi Connect Verify"

2. Only if t "${RET}" of "${RET} =    Wifi Connect Verify" is "True" go and execute the below statements ( ie. get the gateway ip, get wifi IP and ping) otherwise fail the testcase "${RET} =    Wifi Connect Verify" and don't execute the below statements.

   ${WIFI_MODULE_IP} =     Get Wifi Ip
    ${GATEWAY_IP} =     Get Gateway Ip
    ${RET} =    Wait Until Device Is Pingable     ${GATEWAY_IP}
    Should Be True    ${RET}


3. Only if if "${RET}" of  "${RET} =    Wait Until Device Is Pingable     ${GATEWAY_IP}" is "True" , execute the below statements otherwise fail this test case and don't execute the below statements.

     Log    Wi-Fi Signal Strength Threshold: ${SIGNAL_LEVEL_THRESHOLD}
     ${link}     ${level}     ${noise} =     Get Wifi Signal Quality
     Should Be True      ${level} >= ${SIGNAL_LEVEL_THRESHOLD}

4. Also Could you please let me know how can I check for two return values ie., "True" or "None" in the robot framework, ie., from the below python code, ie., for example I am disconnecting from the current network if it is connected and returning "True" and if it is not connected and returning "None" , the below python code seems to return "None" (yet to be implemented in python) to the robot frameowrk.


    def wifi_disconnect(self, timeout=10):
        """
            Connect to Access point using SSID and PW.

            :param ssid: SSID of the ACCESS POINT.
            :param pw: password for connecting to the access point.
            :return: command output as True or False.
        """

        cmd = "nmcli -t -f TYPE,UUID con"
        res = self._helper.execute_cmd_output_string(cmd)
        print(res)
        lines = res.split("\n")
        print(lines)

        for line in lines:
            parts = line.split(":")
            print(parts)
            print(parts[0])
            if (parts[0] == "802-11-wireless"):
                print("------------------------------------------------------")
                print("nmcli connection delete uuid " + parts[1])
                cmd = "nmcli connection delete uuid '%s'" % parts[1]
                for i in range(timeout // 2):
                    exit_code = self._helper.execute_cmd_return_code(cmd)
                    print(exit_code)
                    time.sleep(1)
                    print "%d seconds have passed" % i
                    if exit_code == 0:
                        return True

                return False

Viswanath Nagarajan

unread,
Nov 23, 2018, 11:09:38 PM11/23/18
to sriniva...@gmail.com, robotframe...@googlegroups.com
Hi Srinivasan,

I can see that there are two ways you can achieve your initial requirements.

1. You can use command line option "--exitonfailure" while executing the script. This will exit the script if any one the testcase fails.
2. If you dont want to use above approach, do the below steps.
     --> Initialise this variable in suite setup ${prev_test_result}  Set Variable  PASS
     --> Create your own test teardown keyword and set a variable like below
           ${prev_test_result}  Set Variable If  "${TEST STATUS}" == "FAIL"  FAIL  PASS
     --> Then in test setup do like below
             Run Keyword If  "${prev_test_result}" == "PASS"
             ...  Log  Proceeding with the testcase
             ...  ELSE
             ...  Fail  Failing the testcase
      --> Above will make sure to fail the remaining testcase if the previous testcase fails
3. You dont need to store the return value in a variable and then call should be true. If any keyword fails the testcase will automatically fails
    Wifi Disconnect Then Connect      ${SSID}      ${PW}     >>>>>> This is enough

Thanks,
Viswanath
--
Regards,
N.Viswanath

srinivasan

unread,
Nov 24, 2018, 6:24:45 AM11/24/18
to Viswanath Nagarajan, robotframe...@googlegroups.com
Dear Viswanath,

Thanks a lot for your quick and prompt responses,

Sorry for my poor understanding, as am very newbie to robot framework, finding little bit difficulty in understanding your following points,  Could you please help me to understand the following points, ie.,  Could you please provide some more explanation using my robot wifi code snippets posted below:

2. If you dont want to use above approach, do the below steps.
     --> Initialise this variable in suite setup ${prev_test_result}  Set Variable  PASS --- >Should I initialize this variable in the same robot  script file i.,e wifi_testing_robot ie., where I have the below code snippets?
     --> Create your own test teardown keyword and set a variable like below
           ${prev_test_result}  Set Variable If  "${TEST STATUS}" == "FAIL"  FAIL  PASS --> Could you please help me to understand,what is this teardown variable and where to create this test teardown keyword and set this variable?
     --> Then in test setup do like below -----------> Could you please help me to understand this with the wifi code snippets posted below? 
             Run Keyword If  "${prev_test_result}" == "PASS"
             ...  Log  Proceeding with the testcase
             ...  ELSE
             ...  Fail  Failing the testcase
      --> Above will make sure to fail the remaining testcase if the previous testcase fails
3. You dont need to store the return value in a variable and then call should be true. If any keyword fails the testcase will automatically fails ---> Sorry could you please help me to understand this point, as this was not very clear to me?
    Wifi Disconnect Then Connect      ${SSID}      ${PW}     >>>>>> This is enough


Code Spinets
*** Test Cases ***
Connect Wi-Fi to SSID
    ${RET} =     Wifi Disconnect Then Connect      ${SSID}      ${PW}
    Log    WIFI SSID: ${SSID}
    Log    WIFI PW: ${PW}
    Should Be True    ${RET}

Check for Wi-Fi Connectivity
    ${RET} =    Wifi Connect Verify
    Should Be True    ${RET}    

Get Gateway IP and Check Whether Wi-Fi is Pingable
    ${WIFI_MODULE_IP} =     Get Wifi Ip
    ${GATEWAY_IP} =     Get Gateway Ip
    ${RET} =    Wait Until Device Is Pingable     ${GATEWAY_IP}
    Should Be True    ${RET}

Check for Wi-Fi Signal Strength with Threshold
     Log    Wi-Fi Signal Strength Threshold: ${SIGNAL_LEVEL_THRESHOLD}
     ${link}     ${level}     ${noise} =     Get Wifi Signal Quality
     Should Be True      ${level} >= ${SIGNAL_LEVEL_THRESHOLD}

Thanks in advance,
Srini

Viswanath Nagarajan

unread,
Nov 24, 2018, 7:04:15 AM11/24/18
to Srinivasan Shanmugam, robotframe...@googlegroups.com
Hi Srinivasan,

Please find the full code below.

*** Settings ***
Suite Setup      My Suite Setup
Test Setup       My Test Setup
Test Teardown    My Test Teardown

*** Variables ***
${SSID}                           NasdfghFunkloch
${PW}                             0160717268088099898
${SIGNAL_LEVEL_THRESHOLD}         -90

*** Keywords ***
My Suite Setup
##############

    [Documentation]  My Suite Setup

    Set Suite Variable  ${prev_test_result}  PASS


My Test Setup
#############

    [Documentation]  My Test Setup

    Run Keyword If  "${prev_test_result}" == "PASS"
    ...  Log  Proceeding with the testcase
    ...  ELSE
    ...  Fail  Failing the testcase


My Test Teardown
################

    [Documentation]  My Test Teardown

    ${prev_test_result}  Set Variable If  "${TEST STATUS}" == "FAIL"  FAIL  PASS
    Set Suite Variable  ${prev_test_result}


*** Test Cases ***
Connect Wi-Fi to SSID
    Wifi Disconnect Then Connect      ${SSID}      ${PW}
    Log    WIFI SSID: ${SSID}
    Log    WIFI PW: ${PW}


Check for Wi-Fi Connectivity
    Wifi Connect Verify


Get Gateway IP and Check Whether Wi-Fi is Pingable
    ${WIFI_MODULE_IP} =     Get Wifi Ip
    ${GATEWAY_IP} =     Get Gateway Ip
    Wait Until Device Is Pingable     ${GATEWAY_IP}


Check for Wi-Fi Signal Strength with Threshold
     Log    Wi-Fi Signal Strength Threshold: ${SIGNAL_LEVEL_THRESHOLD}
     ${link}     ${level}     ${noise} =     Get Wifi Signal Quality
     Should Be True      ${level} >= ${SIGNAL_LEVEL_THRESHOLD}

Thanks,
Viswanath
--
Regards,
N.Viswanath

srinivasan

unread,
Nov 25, 2018, 4:37:29 PM11/25/18
to Viswanath Nagarajan, robotframe...@googlegroups.com
Thanks a lot Viswanath, for all your support and quick responses really appreciate the same keep it up :) as this is really needed for beginners who get started with Robot framework scripts.
  
I will try this code on my embedded Target will keep you updated further :)

Many Thanks,

Anne kootstra

unread,
Nov 26, 2018, 2:01:20 AM11/26/18
to robotframework-users
Following this thread from a distance I'd like make a comment. It seems to me that you inherited a Robot Framework script code base and have been tasked to maintain it. It also appears that the level of understanding of Robot Framework scripting needed for the task exceeds your current abilities. This doesn't mean you shouldn't do it, but it does put you in a situation where you need help. 

Feel free to ask your questions here, or in the Slack channel. That is what they are for. However, also realize that if you received some training or more direct guidance from someone more senior in this area, you'd be much faster up to speed for most tasks then having to figure everything out from the internet. 

Keep up the good work. 

Kind regards,

Anne

Op zondag 25 november 2018 22:37:29 UTC+1 schreef Srinivasan Shanmugam:
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
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-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.


--
Regards,
N.Viswanath


--
Regards,
N.Viswanath



--
Regards,
N.Viswanath

srinivasan

unread,
Nov 26, 2018, 5:53:10 AM11/26/18
to Viswanath Nagarajan, robotframe...@googlegroups.com
Dear Viswanath,

Many thanks a lot for all your help.

I tried using the simple way ie., using "--exitonfailure " as below on
my above code snippet, as soon as my wifi connectivity fails, I see
the messages "Critical failure occured and exit-on-failure mode is in
use" for the remaining test cases, are these messages are expected
whenever if we use "--exitonfailure " in the command line for the
remaining test cases?


logs:

"root:~# python3 -m robot --nostatusrc --exitonfailure --variable
SSID:"NIFunkloch" --variable PW:"T.f.o.s.1996!" --outputdir results
qa/robot_tests/wifi_testing.robot
==============================================================================
Wifi Testing :: This is the Maschine Native OS Build Wi-Fi Test.
==============================================================================
Connect Wi-Fi to SSID | FAIL |
'False' should be true.
------------------------------------------------------------------------------
Check for Wi-Fi Connectivity | FAIL |
Critical failure occurred and exit-on-failure mode is in use.
------------------------------------------------------------------------------
Get Gateway IP and Check Whether Wi-Fi is Pingable | FAIL |
Critical failure occurred and exit-on-failure mode is in use.
------------------------------------------------------------------------------
Check for Wi-Fi Signal Strength with Threshold | FAIL |
Critical failure occurred and exit-on-failure mode is in use.
------------------------------------------------------------------------------
Wifi Testing :: This is the Maschine Native OS Build Wi-Fi Test. | FAIL |
4 critical tests, 0 passed, 4 failed
4 tests total, 0 passed, 4 failed
==============================================================================
Output: /home/root/results/output.xml
Log: /home/root/results/log.html
Report: /home/root/results/report.html
root:~#

srinivasan

unread,
Nov 27, 2018, 9:59:21 AM11/27/18
to Anne kootstra, robotframe...@googlegroups.com
Hi Anne,

Could you please let me know the slack channel id for asking the questions please?

Thanks in advance,

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 https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.


--
Regards,
N.Viswanath


--
Regards,
N.Viswanath



--
Regards,
N.Viswanath

--
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.

Anne kootstra

unread,
Nov 28, 2018, 2:05:41 AM11/28/18
to robotframework-users
Through: https://robotframework-slack-invite.herokuapp.com/ you will be able to get an account on robotframework.slack.com

Kind regards,

Anne

Op dinsdag 27 november 2018 15:59:21 UTC+1 schreef Srinivasan Shanmugam:
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
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-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.


--
Regards,
N.Viswanath


--
Regards,
N.Viswanath



--
Regards,
N.Viswanath

--
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-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages