HowTo distinguish between enabled and disabled state of a button

Asked by bgbig

I have one situation wherein the 'stop' button disables on GUI when the test is done successfully.

I need to wait on that screen (wait for the 'stop' button to be greyed out ) and then move on to close the window in which this operation is being performed.

I tried the following code :
while exists("img1"): wait(25)
   sleep(1)
    if exists("img2"):
    print "Test Done"
    type(Key.F4,KEY_ALT)

###
where img1='stop' button enabled
            img2='stop' button disabled
and the test takes around 20 seconds to run.
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

To differentiate between a button being enabled/disabled, usually it is sufficient to use:

if exists(Pattern(image_enabled_button).exact():
     print "enabled"
else:
     print "disabled"

This works in situations, where you are sure, that the button is visible at all in one of the 2 states.

In your case (supposing, the button is always in the same place):

option 1:
button = Pattern("img1").exact()
while button.exists(Pattern("img1").exact()):
   wait(1)
print "Test Done"
type(Key.F4,KEY_ALT)

option 2:
button = Pattern("img1").exact()
button.waitVanish(Pattern("img1").exact(), 30)
print "Test Done"
type(Key.F4,KEY_ALT)

In both cases we only look in the region of the button, wether it is still found with a similarity of >0.99.
If the button gets disabled, it produces a similarity of <0.99 and hence is no longer found.

The first version has more flexibility, to handle special situations.

The second version will produce a FindFailed, if the button is visible longer than 30 seconds and uses more cpu (3 searches per second vs. 1 per second with option 1)

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

one more thing:
The shot of the button should be optimized, so that it contains as little background as possible (cut just around the border of the button including the border)

Revision history for this message
bgbig (bgbig) said :
#3

Thanks RaiMan, that solved my question.