Kumar Kumar -4 years ago 78
Python Question

Robot Framework Keyword with Python: Error with unmatched string or IP. How to fix this?

I am trying to write a keyword for Robot Framework as @Brayan Oakley suggesed in Question :

How to write python function to test the matched strings (to use for Robot framework keyword)?

My Python file:

import os,re

def check_IP():
cmd = ' netstat -ano '
output = os.popen(cmd).read()
match1 = re.findall('.* (*',output)
mat1 = ['']
if match1 == mat1:
print "IP addr found"
if match1 != mat1:
raise Exception('No matching IP...')


I am trying to match a IP address in "netstat -ano" command. If its matches, I am getting "IP addr found" message as expected.

But if IP address is not found I am getting exception as expected, but with below error messages.

C:\Users\test\Desktop>python check.py
Traceback (most recent call last):
File "check.py", line 13, in <module>
File "check.py", line 11, in check_IP
raise Exception('No matching IP...')
Exception: No matching IP...


Any clue to fix this please?

Answer Source

The code is doing precisely what you told it to do. You are running the code outside of the context of robot, and this is how python treats exceptions.

If you don't want to see the stack trace, catch the exception and print whatever message you want.

    except Exception as e
        print str(e)

Of course, you'll want to remove all that code if you use check_IP as a keyword.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download