sleep for random amount of time

Asked by Alex moneys

Brand new to Sikuli.
Been figuring things out pretty quick "just upgraded from batch" Nyahaha anywas, besides the point

I looking for a function that makes sikuli sleep for a random number of seconds. something like "sleep between 200-400 seconds"

thanks for any and all help much appreciated

also im wondering if i can get sikuli to type things from a .txt document. like having to read a list and type things from the list of words

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

For this you can use basic Python features.

1. --- random numbers: (http://docs.python.org/library/random.html)
# once at beginning of script
import random
random.seed()

# and then every time you need it
t = random.randint(200, 400)
wait(t)

2. --- reading from files
f = file("absolute-path-to-a-text-file")
lines = f.readlines()
f.close()

after that, all your lines from the file are in the list "lines" and can be accessed as line[1], line[2], ....
usually you do something like that:

for line in lines:
   line = strip(line) # get rid of the linebreaks
   words = line.split() # now words contains all words of a line (seperated by white space)
   for word in words: # assuming a text-editor has focus
      paste(word)
      type(Key.ENTER)
# now you have each word from your file in the editor window each on a seperate line.

Tip: be careful with typing text coming from a file. type() only handles the typical keys available on an US keyboard. The recommendation is, to always use paste() for "typing" text with Sikuli and only use type() for the functional keys like ENTER ...

Revision history for this message
Alex moneys (way-2-hot-a) said :
#2

wow thats lots of info. and im getting it to work! thanks for your help.

If i may ask one other question. I have sucessfuly used what you recommended above. Now i have this program that errors sometimes with a popup and i need to close the popup and tell it to enter a menu *using a pic to click which menu to enter* and get it to click a pic to restart the program.

i have something like this right now
ok im waiting for a program that automatically resets my connection. it says "connected" then resets after 2min "disconnected" and then back to connected. i want it to wait() for the disconnect image, then when seen wait() for a disconnect image. but during this duration there may be a popup preventing the "connected" image from appearing. so i would like it to execute commands to *lets say reset manually* (which i can do using pictures) i need the code on how to wait for the popup and continue if no popup occurs or intervene when the popup does appear. sorry for long explanation RaiMan

while True: #im hoping for this to look for the popup and if the popup is not found to continue waitin 4 anothr evnt to apear
 if not exists(png):
  exists(png,120)
  sleep(1)
while True:
 if exists( ): break
 click( png)
 click( png)

Revision history for this message
Alex moneys (way-2-hot-a) said :
#3

Correction to this paragraph sorry
Pictures in " " <quotes
*******i have something like this right now
ok im waiting for a program that automatically resets my connection. it says "connected" then resets after 2min showing "disconnected" and then back to "connected". i want it to wait() for the "disconnect" image, then when the image is seen, wait() for a "connected" image. but during this duration there may be a popup preventing the "connected" image from appearing. so i would like it to execute commands to *lets say reset manually* (which i can do using pictures) i need the code on how to wait for the popup and continue if no popup occurs or intervene when the popup does appear. sorry for long explanation RaiMan*****

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

go to your .sikuli folder, open the .py with a text editor and paste the relevant part from there - it is easier to read because you can see the picture strings.

let's try without

dis = disconnect-image
con = connect-image
pop = pop-image

while True:
   if not exists(dis, 0): # waiting for 1st event
      wait(2); continue
   loopAgain = False
   while True:
      if exists(con, 0): break # connected == done
      if exists(pop, 0):
         pass # do something
         # do we have to wait for dis again after that?
         # if yes: loopAgain=True
      wait(2) # should be so that both events can be catched
   if loopAgain: continue # again waiting for dis

Revision history for this message
Alex moneys (way-2-hot-a) said :
#5

Ok going to try this right now, im not sure what you mean by opening the .sikuli folder then opening .py

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

Windows or Mac?

opening means: look into the .sikuli folder/directory contents, there is a .py containing the script in plain python with the same name as the .sikuli.

Revision history for this message
Alex moneys (way-2-hot-a) said :
#7

Can you give me an example on how to use pass?

like this?

if exists(pop), ):
    pass
        clic()

??

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

pass is only needed to keep the indentation correct, when the if/else branches are still empty. it does nothing and can be deleted when you do something real there:

if True:
   pass
else:
   pass

no syntax error

if True:
else:

produces a syntax error.

so the 2 lines above have to look like this:
pop = captured-image
waitingTime = 10
someImage = some-other-captured-image

if exists(pop, waitingTime):
   click(someImage)

Revision history for this message
Alex moneys (way-2-hot-a) said :
#9

while True:
   if not exists(dis, 0): # waiting for 1st event
      wait(2); continue
   loopAgain = False # Here it should be True so it keeps looping untill it sees
   while True: # That it does in-fact how it is disconnected? before it continues?
      if exists(con, 0): break # connected == done
      if exists(pop, 0):
         pass # do something
         # do we have to wait for dis again after that?
         # if yes: loopAgain=True
      wait(2) # should be so that both events can be catched
   if loopAgain: continue # again waiting for dis

Questions are for line 4, should that keep looping until the programs timer disconnects it before the rest of the lines are being executed below line 4

Revision history for this message
Alex moneys (way-2-hot-a) said :
#10

It doesnt seem to be working
The image that is there will be a "connected" then it will turn into a "disconnected" then it will turn into a "connected" again between the disconnected image and connected there may be a pop up *Just clarifying*

heres the code:

dis = disconnected-image
con = connected-image
pop = pop-image

while True:
 if not exists(dis , 0): #disconnected is showing at this point
  wait(2); continue
 loopAgain = True # I THINK* this should be true
 while True:
  if exists(con , 0): break #connected is showing at this point.
  if exists( pop, 0): #i think* if exists "pop" popup window it will click the images to fix the problems click()
    click( )
    click( )
    click( )
    click( )
    click( )
    wait(2)
 if loopAgain: continue # again* i think this should be here

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

see my comments >>>

>>> you have to capture your images here replacing the -image items
>>> since I don't like these thumbnails in my code, I always assign the to variables
dis = disconnected-image
con = connected-image
pop = pop-image

while True:
 if not exists(dis , 0): #disconnected is showing at this point
  wait(2); continue
>>> continue jumps directly to top of loop
>>> here we come only if dis comes up
 loopAgain = False # I THINK* this should be true >>> No, not for this usage!
>>> the question was, what to do after reacting on the popup
>>> without the loopAgain, the loop would finish
 while True:
  if exists(con , 0): break #connected is showing at this point.
  if exists( pop, 0): #i think* if exists "pop" popup window it will click the images to fix the problems click()
    click( )
    click( )
    click( )
    click( )
    click( )
    wait(2)
    loopAgain = True
 if loopAgain: continue # again* i think this should be here

So if you want this outer loop to loop forever waiting for a dis, then waiting for connect/popup and then from beginning:

dis = disconnected-image
con = connected-image
pop = pop-image

while True:
 if not exists(dis , 0): #disconnected is showing at this point
  wait(2); continue
 while True: # dis has shown up
  if exists(con , 0): break #con has shown up, start again looking for dis
  elif exists( pop, 0): #i think* if exists "pop" popup window it will click the images to fix the problems click()
    click( )
    click( )
    click( )
    click( )
    click( )
    wait(2)
    break # leave the loop and start over again looking for dis
  else: wait(2)

Tip: in The Sikuli IDE use tab/shift-tab to indent/dedent, since this works for one line or a block of lines.
 wait(2)

Revision history for this message
Alex moneys (way-2-hot-a) said :
#12

Can please Help me fix my indentation
I think thats where some of my problems lie

Revision history for this message
Alex moneys (way-2-hot-a) said :
#13

what its doing is, it waits for the "disconnect-img" to come, then it moves on. now its EITHER waiting for the "pop-img" or the "connect-img" but before EITHER of them show up its alrdy going through the motions of *If the popup came up* (shown on line # 4) when the popup hasnt even show up. its not waiting for connect or popup. (First part works, loops till find "dis-img" part that doesn't work is #'s 1 - 3
*please fix my indents if they are not correct* thank you Raiman

while True:
 if not exists(dis , 0): #disconnected is showing at this point
  wait(2); continue
>>> continue jumps directly to top of loop
>>> here we come only if dis comes up
 loopAgain = False # I THINK* this should be true >>> No, not for this usage!
>>> the question was, what to do after reacting on the popup
>>> without the loopAgain, the loop would finish
 while True: #1
  if exists(con , 0): break #connected is showing at this point. #2
  if exists( pop, 0): #i think* if exists "pop" popup window it will click the images to fix the problems click() #3
    click( ) #Line 4
    click( )
    click( )
    click( )
    click( )
    wait(2)
# loopAgain = True #i dont need it to look again, just once,
#if loopAgain: continue # again* i think this should be here

Revision history for this message
Alex moneys (way-2-hot-a) said :
#14

*********EDITED*********
what its doing is, it waits for the "disconnect-img" to come, then it moves on. now its EITHER waiting for the "pop-img" or the "connect-img" but before EITHER of them show up its alrdy going through the motions of *If the popup came up* (shown on line # 4) when the popup hasnt even show up. its not waiting for connect or popup. (First part works, loops till find "dis-img" part that doesn't work is #'s 1 - 3
*please fix my indents if they are not correct* thank you Raiman

while True:
 if not exists(dis , 0): #disconnected is showing at this point
  wait(2); continue
>>> continue jumps directly to top of loop
>>> here we come only if dis comes up
 loopAgain = False #
>>> the question was, what to do after reacting on the popup
>>> without the loopAgain, the loop would finish
 while True: #1
  if exists(con , 0): break #connected is showing at this point. #2
  if exists( pop, 0): #3
    click( ) #Line 4
    click( )
    click( )
    click( )
    click( )
    wait(2)
# loopAgain = True #i dont need it to loop the whole thing over again, i have more script below this point.
#if loopAgain: continue #

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

reduced the script to what is really needed.
Mind the new/changed lines at #*1 and #*2

the workflow:
1. waits for dis
2. looks for con - if con there goes to 1.
3. if not con: looks for pop - if pop there: does something and goes to 1.
4. if neither con nor pop: waits 2 seconds and goes to 2.

each * is one tab (one indent)
while True:
*if not exists(dis , 0):
**wait(2); continue
*while True:
**if exists(con , 0): break
**elif exists( pop, 0): #*1
***click( )
***click( )
***click( )
***click( )
***click( )
***wait(2)
***break #*2
**else: wait(2)

you may repair your script, by selecting all relevant lines and using shift-tab as long as all lines are left bound. Then use tab to correct the indentation.
And really, stay with the tab for indent and shift-tab for dedent. The Sikuli IDE is really dumb, so protect yourself ;-) (IDE's like Netbeans or Eclipse show up indentation errors).

Revision history for this message
Alex moneys (way-2-hot-a) said :
#16

still something will be wrong my friend. it loops itself waiting for the disconnect which is good. then i need it to loop itself waiting for either the connect or the popup. if neither have shown up i need it to *keep looping untill #1 COnnected shows up OR #2 popup shows up*

Revision history for this message
Alex moneys (way-2-hot-a) said :
#17

ok im sorry, here is what i have now. Please advise based on this new code, which is simplified for me to be able to understand better.

dis = disconnect-image
con = connect-image
pop = popup-image

while True: #1
 if not exists( dis, 0): #2 this loop waiting for dis to appear before going to #4
  wait(2); continue #
 while True: #4
  if not exists(con , 70): #5 should wait up to 70 before executing line #6 if "con" not found
   wait(popup ,30)#6 looks for the popup that is preventing "con" from appearing
   click( )#7 this series of clicks #7-11 closes the popup and restarts the connection
   click( )#8
   click( )#9
   click( )#10
   click( )#11
   wait(con ,FOREVER)#12 now waits for con to appear
   click( )#13
   sleep(1) #14
##bot should stop/end here and NOT proceed to else##
  else: #15 this should be where line #5 continues at if no popup was found and "con" was found
   wait(con ,30) #16
   click() #17
                        sleep(1) #18
##if no "popup" was found and "con" WAS found at line #5 it should go to else at line #15

Revision history for this message
Alex moneys (way-2-hot-a) said :
#18

PS line #18 is indented properly in script

Revision history for this message
Alex moneys (way-2-hot-a) said :
#19

seems like loop is stuck at #15 "else" to #18 the keeps looping from there. looks like this:

    else:
    wait(con,30)
    click(img)
sleep(1) # not indented, so to end the else loop but i dont think its working.

thats where im stuck at now.

Revision history for this message
Alex moneys (way-2-hot-a) said :
#20

ok im sorry, here is what i have now. Please advise based on this new code, which is simplified for me to be able to understand better.

dis = disconnect-image
con = connect-image
pop = popup-image

while True: #1
 if not exists( dis, 0): #2 this loop waiting for dis to appear before going to #4
  wait(2); continue #
 while True: #4
  if not exists(con , 70): #5 should wait up to 70 before executing line #6 if "con" not found
   wait(popup ,30)#6 looks for the popup that is preventing "con" from appearing
   click( )#7 this series of clicks #7-11 closes the popup and restarts the connection
   click( )#8
   click( )#9
   click( )#10
   click( )#11
   wait(con ,FOREVER)#12 now waits for con to appear
   click( )#13
   exit() #14
##bot should stop/end here and NOT proceed to else##
  else: #15 this should be where line #5 continues at if no popup was found and "con" was found
   wait(con ,30) #16
   click() #17
    exit() # 18
##if no "popup" was found and "con" WAS found at line #5 it should go to else at line #15

i added the exits to make it stop the loops. thanks for all your help buddy

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

while True: #1
 if not exists( dis, 0): #2 this loop waiting for dis to appear before going to #4
  wait(2); continue #
 while True: #4
  if not exists(con , 70): #5 should wait up to 70 before executing line #6 if "con" not found
   wait(popup ,30)#6 looks for the popup that is preventing "con" from appearing
   wait(con ,FOREVER)#12 now waits for con to appear
   exit() #14
  else: #15 this should be where line #5 continues at if no popup was found and "con" was found
   wait(con ,30) #16
   exit() # 18

I left out all the click()'s, to see the workflow as such.
Because of the exit()'s, your loop now will only run once. it is the same as:

while True:
 if not exists( dis, 0): wait(2)
if not exists(con , 70): #1
   wait(popup ,30) #2
   wait(con ,FOREVER) #3
else:
   wait(con ,30)

at #1: if within 70 seconds con appears, it will continue with the else and wait another 30 seconds for con - does not make sense, because it is already there.

at #2: if for some reason, popup does not appear within 30 seconds, the script will abort with a FindFailed error. To easily handle such situations with if/else, we have exists().

at #3: wait(..., FOREVER) --- same problem as with the loop waiting for dis: if nothing happens, it will run forever. You have to kill the script, to stop it.

I have a helper for that:

def myWait(img, t=2, max=60):
   # looks every t seconds for image for max seconds (plus time for searches)
   # t should be > 0 and max > t, otherwise set to 1
   retVal = False
   t = 1 if t < 1
   for i in range(1 if max < t else int(max/t)):
      if exists(img, 0): retVal = True; break
      wait(t)
   return retVal

usage example:
if not myWait(some-image): popup("Sorry"); exit(1)

You could use it in your workflow:

if not myWait(dis): popup("Sorry, no dis"); exit(1)
if not myWait(con): # waits about 70 seconds
   if myWait(popup, max=30):
     # your clicks regarding popup
   else:
     # what happens if popup AND con do not appear within the given time?
else:
   # your clicks regarding con

This script will always behave well (no FindFailed errors because of timeouts).

Can you help with this problem?

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

To post a message you must log in.