Reading values of the grabbed element (.text, .value, .id, lastElement)

in Japanese(日本語)

How to read a grabbed element's own data — its label, value, and identifier — instead of just asserting against it.

Properties

property description
.text The grabbed element's displayed text (label). String?
.value The grabbed element's value. String?
.id The grabbed element's identifier. String?
.isEmpty / .isNotEmpty Whether the element was not grabbed / was grabbed
lastElement The element grabbed last (next section)

Example

orderNumber = exist("#order_id").text   // read the displayed text
let e = select("#total")
if e.isNotEmpty { total = e.text }      // check the grab with .isNotEmpty

Grabbing an element's value (.text / .value / .id)

The return value of exist (and select) exposes the value itself. Use this when the expected value can't be written into the scenario in advance — capturing an order number to check on a later screen, or reading a total shown on screen to use in a calculation.

var orderNumber: String?

scene(2, "Confirm the order and note the order number") {
    action { tap("#btn_order") }
    .expectation {
        select("#order_id").textStartsWith("Order:")   // settle the value first, then read it
        orderNumber = exist("#order_id").text
    }
}
scene(3, "The confirmation screen shows the same order number") {
    action { tap("#tab_orders") }
    .expectation { select("#confirm_order_id").textIs(orderNumber ?? "") }
}
let e = select("#total")
if e.isNotEmpty { total = e.text }   // absent or invisible → an empty element, so don't read it

The last grabbed element (lastElement)

Even without capturing the return value, the element grabbed last is readable via lastElement.

select("#total")
lastElement.text.thisContains("1,200")   // readable without assigning to a variable
tap("#order_btn")
lastElement.idIs("order_btn")            // operation commands also replace the grabbed element

First-check-the-grabbed-value order

A chained check (exist(…).textIs(…), lastElement.textIs(…), or the implicit textIs(…)) first evaluates against the value already grabbed. If that already satisfies the assertion, the step is still recorded, but with no device round trip — the message shows (from the grabbed value). Otherwise it polls the device up to waitSeconds.

However, in runs where the text visibility check is on (fmTextVisibilityCheck or ocrTextVisibilityCheck is true, the default), positive forms that check visibility (textIs, valueIs, … unless requireVisible: false) always look at the device instead of the grabbed value. checkIsON / checkIsOFF / imageIs always look at the device too.

let total = exist("#total")
total.textIs("1,200")             // 0 round trips if the grabbed value is already "1,200" (when the visibility check is off, or for negative forms)
tap("#reload_btn")
total.textIs("1,500")             // the grabbed value is stale → re-fetches until it becomes "1,500"

The longer you wait after grabbing before checking, the more likely a stale value happens to match by coincidence and passes without waiting for the real update — be especially careful with lastElement used far from where it was grabbed.