[HowTo] use getLastMatch()

Created by RaiMan
Keywords:
getlastmatch
Last updated by:
RaiMan

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
click(lastMatch)

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

--- 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>):
   click(getLastMatch())

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

m = exists(<some image>)
if m:
   click(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>)
   click(getLastMatch())
else:
   print "no success"
   exit(1)

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

Uuups, we have to write

   click(m.getLastMatch())

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