Find by image (findImage, findImages, existImage)

in Japanese(日本語)

Finds the element on the screen whose appearance is nearest to a sample image. Use it to grab elements a selector cannot point at, such as icons with neither an id nor a label. existImage searches the same way and asserts that the image is on the screen.

Functions

function description
findImage(label, threshold:, aspectRatioTolerance:, waitSeconds:, scroll:, maxSwipes:) Grabs the one element nearest to the sample image. Returns an empty element instead of failing when nothing is found (branch with .isEmpty). By default it looks at the current screen once (waitSeconds defaults to 0). Pass seconds to waitSeconds to wait for it to appear.
findImages(label, threshold:, aspectRatioTolerance:) Returns every element below threshold, nearest first ([FTElement]). Looks at the current screen once (no waiting, no scrolling). threshold: nil returns every candidate.
existImage(label, threshold:, aspectRatioTolerance:, waitSeconds:, scroll:, maxSwipes:) Asserts that the sample image is on the screen. It searches the same way as findImage and fails when nothing is found. Returns the found element. Without waitSeconds it waits for the image to appear up to the run profile's default wait (same as exist).
element.tap(holdSeconds:, maxGestureSeconds:, settle:) Taps the grabbed element. An element grabbed by findImage / findImages is tapped at the centre of the found frame. holdSeconds greater than 0 makes it a long press, capped at 10s by default — pass maxGestureSeconds: to allow up to 60 for this one call.

Example

findImage("[Camera Icon]").tap()

let icon = findImage("[Camera Icon]", waitSeconds: 3)
if icon.isEmpty {
    // not found
}

findImage("[Share Icon]", scroll: .down).tap()

let stars = findImages("[Star Icon]")
stars.first?.tap()

existImage("[Camera Icon]")
existImage("[Share Icon]", scroll: .down).tap()

Notes

How it searches

  1. It looks up the sample images of DefaultClassifier. It uses the images in folders whose label ends with label, trying the ones for the running OS (marked @i for iOS / @a for Android) first.
  2. It cuts the accessibility elements of the screen out of the screenshot by their frames. Only the elements whose aspect ratio is close to the sample (tolerance aspectRatioTolerance, 0.2 by default) become candidates, closest first.
  3. For each candidate it measures the image feature print distance to the sample (Vision's FeaturePrint; smaller is more similar).
  4. findImage grabs the nearest one when its distance is at most threshold (0.15 by default). Otherwise it classifies that one with DefaultClassifier (the same classifier as imageIs). It grabs it only when the label matches and, in addition, its distance to one of that label's samples is at most threshold (or the classification confidence is at most threshold). A matching label alone is not enough, because the classifier always answers one of the sample labels.

Where the sample images go

It uses the same samples as imageIs.

<project>/vision/classifiers/DefaultClassifier/
  @i/Home/[Camera Icon]/   sample images (png / jpg)
  @a/Home/[Camera Icon]/