trying to trap errors in sikili script not quite working --- indentation problem

Asked by ironmantis7x

Hello:

I am trying to trap errors in a sikuli script and it is not quite doing what I expect.
here is the scenario:

I look for an image on the screen. If the image does not exist, I display an error message and then exit the script at that point. Well .. .this is my intent. What sikuli does is try to execute the next few statements afterwards and then the script stops with an error a few lines down.

Here is a snippet of what I am talking about in code:

------------------------------------------------------------
find("1344004317402.png")
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
else:
    click("1344004329798.png")

-------------------------------------------------------------

Just need some guidance on what I may be doing not quite right.
thanks!

ironmantis7x

Question information

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

If the standards are in effect, find() will abort the script if not found.

exists() always returns with either None (which is false) or the match object (which is true). So exists() usually is the first choice to check wether something is there, before going on.

your case:

# find("1344004317402.png") # delete it - does not make sense, you use exists
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
else: # not needed here, since script will end if not found anyway
click("1344004329798.png") # next statement, if above image is found

so what is left:
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
click("1344004329798.png")

Another trick, to make the script robust:

if not exists(some_image): print "problem"; exit(1)
click(getLastMatch()) # no additional search, if the image just found should be clicked

Revision history for this message
ironmantis7x (ironmantis7x) said :
#2

I am still not getting the app to exit on a fail.  It seems to fail on the step afterwards which is a wait for image to appear step eventhough it can't find the image.
Any ideas??

ironmantis7x

----------------------------------------------------
Lizard (#4): Leave it to my friend. He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4): A first class kung fu man; an expert!

________________________________
 From: RaiMan <email address hidden>
To: <email address hidden>
Sent: Thursday, August 9, 2012 10:06 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

    Status: Open => Answered

RaiMan proposed the following answer:
If the standards are in effect, find() will abort the script if not
found.

exists() always returns with either None (which is false) or the match
object (which is true). So exists() usually is the first choice to check
wether something is there, before going on.

your case:

# find("1344004317402.png") # delete it - does not make sense, you use exists
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
else: # not needed here, since script will end if not found anyway
click("1344004329798.png") # next statement, if above image is found

so what is left:
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
click("1344004329798.png")

Another trick, to make the script robust:

if not exists(some_image): print "problem"; exit(1)
click(getLastMatch()) # no additional search, if the image just found should be clicked

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/sikuli/+question/205413/+confirm?answer_id=0

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/sikuli/+question/205413

You received this question notification because you asked the question.

Revision history for this message
ironmantis7x (ironmantis7x) said :
#3

oh ... forgot to include my code... sorry about that...

if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
click("1344004317402.png")

click(getLastMatch())
## need to wait for splash screen to disappear, if it comes up
wait("UserName-3.png", 40)

...

----------------------------------------------------
Lizard (#4): Leave it to my friend. He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4): A first class kung fu man; an expert!

________________________________
 From: RaiMan <email address hidden>
To: <email address hidden>
Sent: Thursday, August 9, 2012 10:06 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

    Status: Open => Answered

RaiMan proposed the following answer:
If the standards are in effect, find() will abort the script if not
found.

exists() always returns with either None (which is false) or the match
object (which is true). So exists() usually is the first choice to check
wether something is there, before going on.

your case:

# find("1344004317402.png") # delete it - does not make sense, you use exists
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
else: # not needed here, since script will end if not found anyway
click("1344004329798.png") # next statement, if above image is found

so what is left:
if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
click("1344004329798.png")

Another trick, to make the script robust:

if not exists(some_image): print "problem"; exit(1)
click(getLastMatch()) # no additional search, if the image just found should be clicked

--
If this answers your question, please go to the following page to let us
know that it is solved:
https://answers.launchpad.net/sikuli/+question/205413/+confirm?answer_id=0

If you still need help, you can reply to this email or go to the
following page to enter your feedback:
https://answers.launchpad.net/sikuli/+question/205413

You received this question notification because you asked the question.

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

if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
   # should exit here with the error message
   # if image 1344004317402 is not found
# click("1344004317402.png") # delete - double with next statement

# this clicks image 1344004317402, if it was found
click(getLastMatch())

## need to wait for splash screen to disappear, if it comes up
# this will fail if image UserName-3 does not appear within 40 secs
wait("UserName-3.png", 40)

-------- So, exactly, what is your problem?

Revision history for this message
ironmantis7x (ironmantis7x) said :
#5

Question: Does Sikuli allow for nested if statements?
In my app it is skipping whole sections of written code that should be failing...

ironmantis7x

----------------------------------------------------
Lizard (#4): Leave it to my friend. He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4): A first class kung fu man; an expert!

________________________________
 From: RaiMan <email address hidden>
To: <email address hidden>
Sent: Friday, August 10, 2012 8:21 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

    Status: Open => Needs information

RaiMan requested more information:

if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
   # should exit here with the error message
   # if image 1344004317402 is not found
# click("1344004317402.png") # delete - double with next statement

# this clicks image 1344004317402, if it was found
click(getLastMatch())

## need to wait for splash screen to disappear, if it comes up
# this will fail if image UserName-3 does not appear within 40 secs
wait("UserName-3.png", 40)

-------- So, exactly, what is your problem?

--
To answer this request for more information, you can either reply to
this email or enter your reply at the following page:
https://answers.launchpad.net/sikuli/+question/205413

You received this question notification because you asked the question.

Revision history for this message
ironmantis7x (ironmantis7x) said :
#6

Found my error -- whitespace matters.  i rewroet the program with different levels of white space (this time minimizing the whitespace and matching levels more carefully) and the program worked!!
ironmantis7x

----------------------------------------------------
Lizard (#4): Leave it to my friend. He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4): A first class kung fu man; an expert!

________________________________
 From: ironmantis7x <email address hidden>
To: <email address hidden>
Sent: Friday, August 10, 2012 10:31 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

    Status: Needs information => Open

You gave more information on the question:
Question: Does Sikuli allow for nested if statements?
In my app it is skipping whole sections of written code that should be failing...

ironmantis7x

----------------------------------------------------
Lizard (#4): Leave it to my friend.  He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4):  A first class kung fu man; an expert!

________________________________
From: RaiMan <email address hidden>
To: <email address hidden>
Sent: Friday, August 10, 2012 8:21 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

    Status: Open => Needs information

RaiMan requested more information:

if not exists("1344004317402.png"):
    print"[error]: Can't Start Application"
    exit(1)
   # should exit here with the error message
   # if image 1344004317402 is not found
# click("1344004317402.png") # delete - double with next statement

# this clicks image 1344004317402, if it was found
click(getLastMatch())

## need to wait for splash screen to disappear, if it comes up
# this will fail if image UserName-3 does not appear within 40 secs
wait("UserName-3.png", 40)

-------- So, exactly, what is your problem?

--
To answer this request for more information, you can either reply to
this email or enter your reply at the following page:
https://answers.launchpad.net/sikuli/+question/205413

You received this question notification because you asked the question.

You received this question notification because you asked the question.

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

Oh yes, I thought you'd be aware, that it is Python language, that you are scripting and one of the most critical things with Python is leading whitespace (named indentation).

If you want to understand the magic and do things right in the future: faq 1771

Revision history for this message
ironmantis7x (ironmantis7x) said :
#8

Thanks for all your help on my issues.
I am going with the approach to make the scripts smaller and therefore more simple in order to capture errors better.
This is working out for me very nicely.

Once again, thanks for all your help and guidance!

ironmantis7x

----------------------------------------------------
Lizard (#4): Leave it to my friend. He can do it!
Scorpion (#3): And who is your friend?
Lizard (#4): A first class kung fu man; an expert!

________________________________
 From: RaiMan <email address hidden>
To: <email address hidden>
Sent: Friday, August 10, 2012 11:06 AM
Subject: Re: [Question #205413]: trying to trap errors in sikili script not quite working --- indentation problem

Your question #205413 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/205413

Summary changed to:
trying to trap errors in sikili script not quite working --- indentation problem

--
You received this question notification because you asked the question.