return sikuli error message

Asked by rarelam

hi, i use python to run sikuli script to do testing. my problem is that if sikuli don't find any image in the script, the sikuli script will break and halt. can i return the error message to python?

please use other method like if not exists(img)...because i am new in programming and sikuli, so i not that powerful to implement error prone in my programming code. i have one idea but i don't sure it can work or not, my idea is use try... except.... but it don't tell much of which image is missing....

anyway, welcome to give me any idea.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

How do you run the sikuli scripts from Python?
Using popen() to run a .sikuli/.skl from commandline?

Since in Sikuli you always have some kind of workflow, like "if this gets visible then click that and wait for something else to come up .....".

For me this is best done with exists().

the principle:
if not exists(img):
    print "[Error] not Found:", img
    exit() # or whatever recovery you want to do.
click(getLastMatch()

with popen e.g., you would have the error message as the last record in the retuned list of sysout-lines.

try: ... except: ... is another possibility. but in many cases it is not very clear and you need more indents.

If you catch a FindFailed or other errors, the error and its message containing the image filename can be taken from sys.exc_info().

Revision history for this message
rarelam (siongwai) said :
#2

hi, i am using popen to run the sikuli script from python. popen is help a lot when i need to return back some certain value to my main python script.

my problem is when exception happen in sikuli and halt the function in the middle of sikuli script. something like image do not found because of clicking another similar button and make the script is break. exit() would not reach because the script is break in middle and sikuli exception is raised.

so i need to return the exception raised by sikuli to my python script. it is sys.exc_info() will do the stuffs?

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

Ok, since you said, you are using Python, I thought you are more experienced than it seems now.

When I asked about popen, I wanted to know, what exactly you are telling popen to do and how.

Since Sikuli scripts are written in Python language (in fact it is Jython internally, C-type Python modules are not available), you can do everything, that is possible with standard Python.

I guess in the moment you get a popup with the error message and your popen'd process silently dies without returning anything.

I think, you have 2 options, to make your scripts treturn something, that makes sense:

--- 1.
use exits() instead of find() to avoid FindFailed exceptions and have your own messages (I prefer this)
(example see my first comment)

--- 2.
wrap your whole script into a try: except: construct:

try:
 # your script goes here
except FindFailed:
  print [Error], sys.exc_info()[0], str(sys.exc_info[1]).replace("\n", " ") # check it out and make your appropriate message
  exit(1)

In your calling Python (if you do not need the returned stuff from popen):
retVal = popen("whatever").readlines()[-1].strip() # gets the last line
if retVal[:6] == "[Error]":
   # test failed - do something

hope it helps.

Revision history for this message
rarelam (siongwai) said :
#4

Thanks RaiMan, that solved my question.