How do I catch switchApp failure?

Asked by Osiris2258

I need to make sure it switches to an application window before proceeding. The switchApp method doesn't seem to return a python error, so I can't figure out how to try-catch. If it fails to switch to the app I need it to print an error code (Which I will pull back into normal python from the windows command line) and then abort. How do I catch the error?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
Eugene S (shragovich) said :
#1

Hi,

The only way for you to verify anything in Sikuli is visually inspect the screen.
So if you are expecting a certain application/webpage to appear on the screen, you should choose a visual component that will ensure that the relevant content is actually visible.

Cheers,
Eugene

Revision history for this message
Osiris2258 (void2258) said :
#2

Sikuli IDE is showing an error message saying it failed to find the window to switch to, therefore it is internally registering the issue. It is simply producing an error message that doesn't seem to have an associated exception I can use in the try-catch.

Revision history for this message
Eugene S (shragovich) said :
#3

So, you mean that the problem is not with identifying the correct window but with switchApp itself?

Revision history for this message
Osiris2258 (void2258) said :
#4

If the application fails, the following will show up in the IDE messages but will not stop execution.

IDE messages:

[log] App.focus APPLICATION_NAME
[error] App.focus failed: APPLICATION _NAME not found

I need to have it stop execution and pass an error back to python (it is being called using subprocess.Popen to a premade script). What I don't see is a catchable exception, unlike if it fails to find a click target which gives a catchable 'FindFailed' exception. The reason this is important is that the software I have to work with is prone to crashing or popups, and in either case this will break sikuli, so I need to be able to handle this.

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

myApp = App.focus(...) # returns None in case of failing, App object otherwise

if not myApp:
    print "could not focus"

Revision history for this message
Osiris2258 (void2258) said :
#6

This works well for catching the error. However if I try to use sys.exit() to stop the script on this error (since if this error pops the script can't proceed), it throws a separate set of errors. Is there a special function I should be using to stop execution cleanly?

Revision history for this message
Osiris2258 (void2258) said :
#7

Error messages from using sys.exit():

[log] App.focus APPLICATION_NAME
[error] App.focus failed: APPLICATION_NAME not found
Focus_Fail
[error] script [ SCRIPT_NAME ] stopped with error in line 8
[error] Could not evaluate error source nor reason. Analyze StackTrace!
[error] Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Temp\sikuli-3814866394256187498.py", line 8, in <module>
sys.exit()
SystemExit

Revision history for this message
Osiris2258 (void2258) said :
#8

Thanks RaiMan, that solved my question.

Revision history for this message
Osiris2258 (void2258) said :
#9

After examining the documentation for sys more closely, I realize that this is standard behavior.

Revision history for this message
Osiris2258 (void2258) said :
#10

After more careful evaluation, this method for catching the error can only tell if the window has closed. It will return null if the window is not present. But if the window is present and simply fails to move to the front, this will not be caught. That seems to happen if running from inside another python script: the window icon on the taskbar will flash as if a message has appeared, but it will not actually bring the window forward, and thus the script fails to find things as they are behind other objects.

Is there a way to tell sikuli to look inside a specific window even if it is not on focus? Alternately, is there a way to get it to override the above issue and force the window forward?

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

--- sys.exit()

yes, this should not be used in Sikuli scripts.

Sikuli has it's own exit(n), to gracefully stop the script with an error code. exit() returns 0.

--- App.focus() and similar leads to flashing icon only
As far as I understand: this is a standard behaviour on Windows with the internally used Windows API features (... and can be seen with many other app situations running in background, but need the user's attention)

Thanks for posting a bug on this. I have accepted it for version 1.2 (... where the revision of the App class features is on the list anyway)

Can you help with this problem?

Provide an answer of your own, or ask Osiris2258 for more information if necessary.

To post a message you must log in.