Need help to solve the regular expression issue with Robot Framework - Custom Library.

934 views
Skip to first unread message

balu

unread,
Feb 4, 2015, 11:50:44 AM2/4/15
to robotframe...@googlegroups.com
I am doing following things using RFW:

STEP 1 : Start a mobile app using emulator to create some traffic
STEP 2 : Check in my switch whether that traffic is recognized or not, using "show engine app stats test on AVC/test-15E8CC" command.

Following is the sample output of that command:

router-7F2C13#show engine app stats test on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  test                
0                 0                  4
--------------------------------------------------------------------------------
router
-7F2C13#
---------------------------------------------------------------
router
-7F2C13#


STEP 3 : I am trying to put PASS/FAIL criteria based on "NUM_FLOWS" from the above output. If its "0 - Zero" it should be FAIL. If its "1 or Greater than 1", Its PASS like that. (Example : In above command output - NUM_FLOWS is 4, So its PASS).

I am using following Python Library to achieve this.

My Python Library "regexp_def.py":


import re
def pass_fail_criteria(x):
   
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
       
return "pass"
   
else:
       
return "fail"


If I execute the above code in python interpreter, its working perfectly. It displays PASS/FAIL according to the output with :
print pass_fail_criteria(x)

But When I use this is in RFW. Its not working. I am getting "IndexError: index out of range: 0" error.

Following is my "test.robot" file:


***Settings***
Library  calc.Calculator  WITH NAME  Calculator
Library  Telnet
Library  regexp_def.py

*** Variables ***

$
{HOST}                1.1.1.1
$
{USERNAME}            test
$
{PASSWORD}            test


***Test Cases***
Start Blue stack
    startBluestack
    sleep            
40
    clickSearchinbluestaks    
    clickSearchtypeocto
    clickOctoapp
    sleep            
20
    clickClose
   
Check Deep Packet Inspection Stats
   
Open Connection            ${HOST}
   
Login    ${USERNAME}        ${PASSWORD}
   
Write                    enable
   
Write                    show engine app stats test on AVC/test-15E8CC
    $
{x}                    Read Until Regexp        .*#
   
Log                     ${x}
    $
{status}               pass fail criteria  ${x}



${x} is having all required data (command output), there is no buffer issue.


Please guide me to solve this issue.


Thanks in advance.

Vinicius Kwiecien Ruoso

unread,
Feb 4, 2015, 1:33:43 PM2/4/15
to balu.ne...@gmail.com, robotframework-users
Hi!

It looks like you are not communication with RF in the proper way.
Check [1] for more details. In order to FAIL a keyword, you must raise
an exception. If your method returns without raising any exceptions,
RF will consider it executed with success.

This should work, for example:
def pass_fail_criteria(x):
if not int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
raise Exception('NUM_FLOWS = 0')

Thanks,
Vinicius

[1] http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#communicating-with-robot-framework
> --
> 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.

balu

unread,
Feb 5, 2015, 5:32:37 AM2/5/15
to robotframe...@googlegroups.com, balu.ne...@gmail.com
Hi Vinicius,

   Thanks for your comment. I have modified my "regexp_def.py". But Still I am getting same error: "IndexError: index out of range: 0



Please find the logs below:


C
:\RFW>jybot.bat octo.robot
==============================================================================
Octo
==============================================================================
Start Blue stack                                                      | PASS |
------------------------------------------------------------------------------
Check Deep Packet Inspection Stats                                    | FAIL |

IndexError: index out of range: 0
------------------------------------------------------------------------------
Octo                                                                  | FAIL |
2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
==============================================================================
Output:  C:\RFW\output.xml
Log:     C:\RFW\log.html
Report:  C:\RFW\report.html

C
:\RFW>


My regexp_exp.py is :


import re


def pass_fail_criteria(x):

   
if not int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
       
raise Exception('NUM_FLOWS = 0')




If I use "Log    ${x}", I am seeing the required data to process the regexp. But still I am getting this error.


I have attached the My test run's html log file.


Please guide me to resolve this issue.


Thanks in advance.


Thanks,
Balu.
log.html

Vinicius Kwiecien Ruoso

unread,
Feb 5, 2015, 6:10:33 AM2/5/15
to Balu P, robotframework-users
Hi!

The code seems to be working with me. It may be possible that the
string you are trying to match break the lines with '\r\n' instead of
'\n'. Try changing the re and check the results. Note that it may be a
better idea to check if there was a match before trying to access the
first one ([0]).

Regards,
Vinicius
>> > 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.
>
> --
> 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.

balu

unread,
Feb 5, 2015, 7:17:49 AM2/5/15
to robotframe...@googlegroups.com, balu.ne...@gmail.com
Hi,

  I tried with the following regexp:

import re


def pass_fail_criteria(x):
   
if not int(re.findall(r"NUM_FLOWS\r\n-+[\s\S]*?(\d+)\s*-+",x)[0]):

       
raise Exception('NUM_FLOWS = 0')



But same error. I am also getting exepcted result when run this code in python interpreter. problem is only if I use this inside RFW.


Could you please tell me if I am doing mistake in the above regexp code.


Otherwise is it possible to achieve this pass/fail criteria with RFW built-in Library keywords?
>> > 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.
>
> --
> 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

Michael Russell

unread,
Feb 5, 2015, 11:40:12 AM2/5/15
to balu.ne...@gmail.com, robotframe...@googlegroups.com

Classification: //Dell SecureWorks/Confidential - Limited External Distribution:

Hello Balu,

  I have 3 points for you:

1)      “IndexError: index out of range: 0” MUST be coming from the end of the line with the “[0]”, so that means your regexp isn’t matching.

2)      For debugging issues like this, break things up and add print statements:

    def pass_fail_criteria(x):

    print "DEBUG: x = '%s'" % x

    match_check = re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)

    print "DEBUG: match_check = '%s'" % match_check

    if not int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):

        raise Exception('NUM_FLOWS = 0')

The output of the “print” statements should be in your “log.html” file.

3)      I think your real problem is that the lines end with a space before the “\n”, so change your regexp so it has a space between NUM_FLOWS and the “\n”.

 

-Michael

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

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

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

Reply all
Reply to author
Forward
0 new messages