Mac: Getting FindFailed when using cron to run sikuli script --- use a LaunchAgent instead

Asked by lifenz

Hello everyone,
I am running Sikuli X-1.0rc3 (r905) on OSX Mountain Lion.
I had an issue when I tried to run sikuli scripts from a crontab for browser GUI testing.

The script starts on time, the browser Firefox is openned, but the Sikuli IDE crashes after the browser openning.

here is the crontab :
00 01 * * * /Applications/Sikuli-IDE.app/sikuli-ide.sh -s -r /Users/USER/Desktop/test.skl

here is the test.skl script :
openApp("Firefox")
wait("Serendresuru.png") # wait for the browser to be oppened
click("Serendresuru.png") # click on the browser URL bar
wait(1)
paste("www.google.com")
wait(1)
type(Key.ENTER)

Here is the standard output :
[info] Sikuli vision engine loaded.
[info] Mac OS X utilities loaded.
[info] VDictProxy loaded.
[log] App.open Firefox(0)
[log] openApp: "Firefox"
[info] locale: fr_FR

Here is the error output (sorry, there are some french sentences):
Erreur d'exécution lors de l'exécution /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-1320609112.sikuli/test.sikuli
Traceback (most recent call last):
  File "/var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-1320609112.sikuli/test.sikuli/test.py", line 2, in <module>
    wait("Serendresuru.png")
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/sikuli/Region.py", line 99, in wait
  Line 7, in file /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-1320609112.sikuli/test.sikuli/test.py

 at org.sikuli.script.Region.handleFindFailed(Region.java:420)
 at org.sikuli.script.Region.wait(Region.java:511)
 at org.python.proxies.sikuli.Region$Region$0.super__wait(Unknown Source)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)

org.sikuli.script.FindFailed: FindFailed: can not find Serendresuru.png on the screen.
  Line 7, in file /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-1320609112.sikuli/test.sikuli/test.py

I dont understand this issue, I tried to run the command line in a terminal, everything works properly.

Thanks in advance for your help.

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

For some reason in the crontab situation it takes longer than 3 seconds (standard waiting time) for the image Serendresuru.png to get visible or the browsers url bar looks different in this case, so you get a FindFailed exception.

so try this:

wait("Serendresuru.png", 10) # wait for the browser to be oppened
click(getLastMatch()) # click on the browser URL bar

or this
click(wait("Serendresuru.png", 10))

both versions wait for max 10 seconds and click on the match that was already found.

You might also try the option:
setFindFailedResponse=PROMPT

see docs: http://sikuli.org/docx/region.html#exception-findfailed

Revision history for this message
lifenz (rivenz) said :
#2

Hello RaiMan,
thanks for your answer. I tried to increase the waiting timeout. However, the script still crashes after firefox launch.
Here is the URL of the script I tried to run : http://dl.free.fr/uEwA3en1O

After the script launch, I have the following errors :
Erreur d'exécution lors de l'exécution /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-312126120.sikuli/test.sikuli
Traceback (most recent call last):
  File "/var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-312126120.sikuli/test.sikuli/test.py", line 4, in <module>
    wait("KSerendresur.png",10)
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/sikuli/Region.py", line 101, in wait
  Line 9, in file /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-312126120.sikuli/test.sikuli/test.py

 at org.sikuli.script.Region.handleFindFailed(Region.java:420)
 at org.sikuli.script.Region.wait(Region.java:511)
 at org.python.proxies.sikuli.Region$Region$0.super__wait(Unknown Source)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)

org.sikuli.script.FindFailed: FindFailed: can not find KSerendresur.png on the screen.
  Line 9, in file /var/folders/04/m1d41v257yxg1t1_h8n2ct5m0000gn/T/tmp-312126120.sikuli/test.sikuli/test.py

Close

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

I am not getting my cron environment on my Mac Lion to work (I know, there are some tricks, but I do not get it).
atrun is still disabled here - does this hinder cron to run?

So for the moment I cannot test the situation.

But this might be the cause: the cron jobs are started in background as far as I know. So they do not have access to the screen.
This might be the problem with Sikuli here.

If you want to run the Sikuli script at 01:00, just start it and implement a wait until that time.You can make it a loop, so it runs every day automatically.

and this is a snippet, how to calculate the time to wait until a specific time:

until = list(time.localtime())
until[3:6] = (15, 54, 0) # hour minute second
timeUntil = time.mktime(tuple(until))
timeToWait = int(timeUntil - time.time())

# only for debug
fmt = "time to wait from now %s til %s : %d sec"
print fmt%(time.strftime("%H:%M"),
        time.strftime("%H:%M", tuple(until)),
        timeToWait)

Revision history for this message
lifenz (rivenz) said :
#4

Thank you for the trick RainMan.

 "atrun is still disabled here - does this hinder cron to run?"
-> I dont know, cron was built in and activated on my computer.

"But this might be the cause: the cron jobs are started in background as far as I know. So they do not have access to the screen."
-> You are probably right, cron specific implementation on OSX may not simply work with sikuli script :(

My main goal was to implement a web page, that could run several schedule sikuli script at different defined time.
So I supposed that crontab was the best solution.

However, I found an interesting link in the Ubuntu community forum, according to this french thread : http://forum.ubuntu-fr.org/viewtopic.php?id=607831, cron would work on Linux Ubuntu.
The man who wrote this post succeeded in executing a sikuli script on Ubuntu using this following command :
"*/6 * * * * DISPLAY=:0 /home/solho/Sikuli/Sikuli-IDE/sikuli-ide.sh -r /home/solho/Sikuli/9boxAUTOparams.sikuli > /tmp/log9box 2>&1" (using DISPLAY=:0 doesnt work in OSX)

Im sad that crontab doesnt work on OSX (I was using OSX in view of avoiding installing the sikuli's dependencies), I may try to migrate to Ubuntu and trying again.

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

On Mac latest since 10.6 cron and atrun are finally deprecated and replaced by the launchd/launchctrl features.

The Apple docs say that a launch agent (controlled by a plist in ~/Library/LaunchAgents) should not use the GUI (which is ok for the general purpose to not disturb the user), but it can use the GUI, which in turn means: a process, that is triggered by a launch agent, has access to the screen.

So on Mac the method to run Sikuli scripts at specific times is to use LaunchAgents.
There is even an app Lingon (AppStore, 4$), that allows to implement and control the LaunchAgents with a nice GUI (in my opinion, one does not really need it, since setting up the needed plist is not that complex)

A developer oriented doc on daemon/agents/background processing:
https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Introduction.html#//apple_ref/doc/uid/10000172i-SW1-SW1

... and the content of the plist is described here:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html%23//apple_ref/doc/man/5/launchd.plist

Revision history for this message
lifenz (rivenz) said :
#6

Thanks RaiMan, that solved my question.