RaiMan / SikuliX-2014

SikuliX version 1.1.2 (until February 2018)
http://sikulix.com
806 stars 234 forks source link

[1.1.1.] Issue with observe() - Bug #1183671 #245

Closed cciola closed 7 years ago

cciola commented 7 years ago

I'm having the "observe()" issue simulated on 1.0.1 at this current 1.1.1. version. https://bugs.launchpad.net/sikuli/+bug/1183671

When observe() is declarated more than one time at same region, the waiting happens in loop and when the image appears, nothing happens.

while not exists("Image.png"): observe()

PS: sorry if this problem is already fixed on version 1.1.1., didn't find any comments about it. Also, when I was using 1.1.0, didn't find the correction either (see my comment on the same link above, wrote on 2017-01-20).

RaiMan commented 7 years ago

Uuups, looks strange and not like the intended use of observe.

see docs - to make observe() do anything at all, you have to define at least one expected event:

appeared = false;
def handler():
    global appeared
    appeared = True
someRegion.onAppear(someImage, handler)
observe(10)
if not appeared: exit(1)

which is the same as:

end = time.time() + 10
while time.time() < end:
    appeared = True
    if someRegion.exists(someImage): break
    appeared = False
if not appeared: exit(1)
cciola commented 7 years ago

@RaiMan , I have this code working perfectly on version 1.0.1:

strCaminhoChrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
strURL = "https://www.google.com.br/"
screenshotsPasta = "C:\\Users\\cciola\\Desktop\\Sikuli IDE\\Screenshots_Sikuli\\"
numPrint = 0

def numPrint_func():
    global numPrint 
    numPrint += 1

def capturaImagem_func():
    wait(1)
    numPrint_func()
    shutil.move(capture(Screen()), screenshotsPasta + 'Chrome_' + (str(int(numPrint))) + '.png')

paste(strURL)
type(Key.ENTER)
while not exists("Image.png"):
          observe()
onAppear("Image.png", capturaImagem_func)

I'm starting coding with Sikuli and don't know if is something strange or wrong with this, but it's working. Cannot guess the reason it stopped working on version 1.1.1.

RaiMan commented 7 years ago

In 1.1.1 the observe feature is very much refurbished and free of oddities to a high degree. Please read the docs, to be sure what you are doing.

your example above as I understand and would have implemented it using 1.1.1:

# supposing Chrome is running and frontmost app
strURL = "https://www.google.com.br/"
screenshotsPasta = r"C:\Users\cciola\Desktop\Sikuli IDE\Screenshots_Sikuli"
numPrint = 0

paste(strURL)
type(Key.ENTER)
while not exists("Image.png"): wait(0.5)
numprint += 1
capture(SCREEN).getFile(screenshotsPasta, "Chrome_%d"%numprint)

... no need to use observe

If you are sure about appearance and maximum waiting time:

# only the last part
paste(strURL)
type(Key.ENTER)
exists("Image.png", 30)
numprint += 1
capture(SCREEN). getFile(screenshotsPasta, "Chrome_%d"%numprint)