A Simple Region.onChange request

Asked by Tim Brayne

Firstly still somewhat a beginner with sikuli and python so please bear with me.
I am writing automation tests for a flash app and i simple want to print a message every time i navigate a menu and the menu icon differs from the one before, this is what i have and have tried a few different things but they either tell me that what i am trying to call isn't defined or it just observes and doesn't perform the rest of the script, the example below runs and passes the test but it does not seem to call my defined event as nothing is printed to the test results...please help....

def test_exampleTest(self):
        nav = Region(Region(569,809,144,36))
        nav.observe()
        def changeHandler(event):print"work ya bastard"
        nav.onChange(5,changeHandler)
        type(Key.DOWN)
        wait(1)
        type(Key.RIGHT)
        wait(1)
        type(Key.RIGHT)
        wait(1)
        type(Key.RIGHT)
        wait(1)

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
Best RaiMan (raimund-hocke) said :
#1

this should work.

def test_exampleTest(self):
        nav = Region(Region(569,809,144,36))
        def changeHandler(event):print"work ya bastard"
        nav.onChange(5,changeHandler)
        nav.observe()
        type(Key.DOWN)
        wait(1)
        type(Key.RIGHT)
        wait(1)
        type(Key.RIGHT)
        wait(1)
        type(Key.RIGHT)
        wait(1)

The onXXX() add an attribute to the region, that should be observed. This has to be done only one time. The region knows this stuff as long as it lives.
Saying observe() switches the observation on (in your case for about 3 seconds) and then stops it. If something changed in the region meanwhile the handler is called every time something changes.
Therefor, one usually stops the observation in the handler with the first event and gives the observation some time to wait.

So the above part would be e.g.

        def changeHandler(event):
            print"work ya bastard"
            event.region.somethingChanged = True
            event.region.stopObserver()
        nav.onChange(5,changeHandler)
        nav.somethingChanged = False
        nav.observe(10)
        if not nav.somethingChanged:
            print nab, "did not change within observation time"
            exit(1)

I have added some coding, to check, wether the handler was visited (something changed) in the code following the observe.

Revision history for this message
Tim Brayne (timothy-brayne) said :
#2

Thanks RaiMan, that solved my question.