[HowTo] use getLastMatch()

Created by RaiMan on on 2011-02-26
Last updated by:
RaiMan on on 2011-04-06

All successful find operations ( explicit like find() or implicit like click() ), store the best match into lastMatch of the region that was searched. findAll() store all found matches into lastMatches of the region that was searched as an iterator.
To access these attributes use region.getLastMatch() or region.getLastMatches() respectively.

--- step by step:

hover(<some image>)
# searches SCREEN, since not qualified by a region
# if successful, the Region object SCREEN has now stored the match,
# that was produced by the implicit find() behind hover(<some image>)
# so it should be accessible with
lastMatch = SCREEN.getLastMatch()
# or shorter
lastMatch = getLastMatch() # uses implicit Region object SCREEN

now lastMatch can be used like any other Match or Region object

--- so this should work
hover(<some image>)

--- the typical usage is in all cases where you want to reuse the last match in a region without repeating the find() operation.

if exists(<some image>):

without a getLastMatch() you wold have to write (performance equivalent):

m = exists(<some image>)
if m:

or (additional find())

if exists(<some image>):
   click(<some image>)

--- one more thing:

I sometimes write

m = find(<some image>)
if m.exists(<some other image>)
   print "no success"

and wonder why it does not click AND does not print "no success"

Uuups, we have to write


since the respective last match is stored in Region object m and not in SCREEN, that is referenced by the "naked" getLastMatch()