onAppear and observe with Robot Framework

Asked by Peter Kim

Hi.

I am using Robot Framework with Sikuli.

I have many test cases in a test suite. Each test case is mapped to a Sikuli method.

While running the test suite of all test cases, I would like to use onAppear and observe calls to handle some of the popup messages.

Do I need to call onAppear, observe, stopObserver in each of the Sikuli methods? Or is there a way to call onAppear and observe once at the beginning and the end of the test suite?

Thanks.

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

If it is possible to start a script once at the beginning of the test run, that contains the observation coding and event handling and keeps alive parallel to the processing of the test cases, then you only need it once.

If this is not possible, then each test case needs its own observation and event handling (which can be put in a general def()).

Another possibility: put all the observation stuff in a totally separate script, that you run in parallel to your test stuff from a separate command line. You might use the new hot key feature to control the observation from your test cases.

Revision history for this message
Peter Kim (pd-kim) said :
#2

I created functions called suite_setup and suite_teardown. I placed onAppear and observe calls in suite_setup and stopObserver call in suite_teardown.

def repeat_alarm(image):
    assert(exists(image))
    v.click(image)
    print "repeat_alarm: A reminder was set."
    v.click("1318980623175-1.png")

def suite_setup():
    ...
    set_alarm()
    v.onAppear("giHAIWE.png", repeat_alarm("HAIWE.png"))
    v.observe(FOREVER,background=True)
    ...

def suite_teardown():
    ...
    v.stopObserver()
    ...

The main routine looks like:

suite_setup()

test_setup()
test_function1()
test_teardown()

test_setup()
test_function2()
test_teardown()

...

suite_teardown()

When I run this in Sikuli IDE, I get the following error message:

suite_setup: setting alarm ...

set_alarm: An alarm was set.

suite_setup: watch for an alarm ...

[error] 중지됨 <--- STOPPED
[error] 776 번째 행에서 오류 발생 <- ERROR on line 776
[error] 에러 메시지: Traceback (most recent call last): <--- Error Message
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3971633246168076025.py", line 776, in
 suite_setup()
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3971633246168076025.py", line 127, in suite_setup
 v.onAppear("giHAIWE.png", repeat_alarm("HAIWE.png"))
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3971633246168076025.py", line 106, in repeat_alarm
 assert(exists(image))
AssertionError

The handler function repeat_alarm is passed an image which will be clicked in the function.

What am I doing wrong?

Thanks.

Revision history for this message
Peter Kim (pd-kim) said :
#3

I found out that I could simply press the home button to do the same thing I was doing in the handler function. I renamed the handler function to be snooze_alarm.

def snooze_alarm():
    print "in snooze_alarm"
    v.click("home.png")

def suite_setup():
    ...
    set_alarm()
    v.onAppear("giHAIWE.png", snooze_alarm)
    v.observe(FOREVER,background=True)
    ...

def suite_teardown():
    ...
    v.stopObserver()
    ...

The main routine looks like:

suite_setup()

test_setup()
test_function1()
test_teardown()

test_setup()
test_function2()
test_teardown()

...

suite_teardown()

I get the following error message:

Exception in thread "Thread-9" Traceback (most recent call last):
 File "C:\W7\Sikuli X\sikuli-script.jar\Lib\sikuli\Region.py", line 287, in observe
 File "C:\W7\Sikuli X\sikuli-script.jar\Lib\sikuli\Region.py", line 255, in targetAppeared
TypeError: repeat_alarm() takes no arguments (1 given)

v_wait_click: could not find I4E-1.png
v_wait_click: saved the region image as C:\Sikuli\LU6200.sikuli\sikuli-scr-6209487362800751544.png

[error] 중지됨
[error] 782 번째 행에서 오류 발생
[error] 에러 메시지: Traceback (most recent call last):
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3763259516910263046.py", line 782, in add_contact(name,mobile_no,home_no,email,memo)
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3763259516910263046.py", line 196, in add_contact
 v_wait_click("I4E-1.png")
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3763259516910263046.py", line 28, in v_wait_click
 v.click(image)
 Line 29, in file C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3763259516910263046.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.GeneratedMethodAccessor16.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

org.sikuli.script.FindFailed: FindFailed: can not find I4E-1.png on the screen.
 Line 29, in file C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp3763259516910263046.py

I am using a Windows 7 32-bit PC.

Revision history for this message
Peter Kim (pd-kim) said :
#4

That is odd: The printout still had the old function name repeat_alarm. I reran it and the output had the new function name. The output looks similar.

Exception in thread "Thread-11" Traceback (most recent call last):
 File "C:\W7\Sikuli X\sikuli-script.jar\Lib\sikuli\Region.py", line 287, in observe
 File "C:\W7\Sikuli X\sikuli-script.jar\Lib\sikuli\Region.py", line 255, in targetAppeared
TypeError: snooze_alarm() takes no arguments (1 given)

[error] 중지됨
[error] 784 번째 행에서 오류 발생

[error] 에러 메시지: Traceback (most recent call last):
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp604084093402973929.py", line 782, in
 add_contact(name,mobile_no,home_no,email,memo)
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp604084093402973929.py", line 196, in add_contact
 v_wait_click("I4E-1.png")
 File "C:\Users\pd.kim\AppData\Local\Temp\sikuli-tmp604084093402973929.py", line 20, in v_wait_click
 elif (v.exists(image,25)):
java.lang.ThreadDeath
at java.lang.Thread.stop(Unknown Source)
at org.sikuli.ide.SikuliIDE$ButtonRun.stopRunning(SikuliIDE.java:1636)
at org.sikuli.ide.SikuliIDE.onStopRunning(SikuliIDE.java:121)
at org.sikuli.ide.SikuliIDE$12.hotkeyPressed(SikuliIDE.java:813)
at org.sikuli.script.HotkeyListener$1.run(HotkeyListener.java:22)

java.lang.ThreadDeath: java.lang.ThreadDeath

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

-- TypeError: snooze_alarm() takes no arguments (1 given)
Since the observe is threaded/subprocessed, you get 2 independent errors. This is the first one:
the observe handler functions are dynamically called with the event object as parameter, but you don't have defined a parameter in your handler function. So I guess the popup is not clicked away and in your main workflow you get the FindFailed.

# correct handler definition:
def snooze_alarm(e):
    print "in snooze_alarm"
    v.click("home.png")

-- If you are running this in IDE
I do not really know, what is happening to the observer in case of errors like you had. So to be clean you should edit your script in the IDE, but run it form command line.

Revision history for this message
Peter Kim (pd-kim) said :
#6

Thanks RaiMan, that solved my question.