Skip to content

Commit 1499c58

Browse files
committed
Throw descriptive error from waitToAppear on timeout.
waitToAppear and waitToAppear(withIndex) previously NPE'd via the findObject() extension's non-null declaration when the selector didn't match before timeout. Now they throw IllegalStateException with the selector, timeout, and (for the indexed overload) matched object count.
1 parent 8a8e6db commit 1499c58

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

  • stream-chat-android-e2e-test/src/main/kotlin/io/getstream/chat/android/e2e/test/uiautomator

stream-chat-android-e2e-test/src/main/kotlin/io/getstream/chat/android/e2e/test/uiautomator/Wait.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,33 @@ public fun sleep(timeOutMillis: Long = defaultTimeout) {
2525
Thread.sleep(timeOutMillis)
2626
}
2727

28+
/**
29+
* Waits up to [timeOutMillis] for an object matching this selector and returns it.
30+
*
31+
* @param timeOutMillis Maximum time to wait before failing.
32+
* @throws IllegalStateException when the timeout elapses without a matching object.
33+
*/
2834
public fun BySelector.waitToAppear(timeOutMillis: Long = defaultTimeout): UiObject2 {
2935
wait(timeOutMillis)
30-
return findObject()
36+
return device.findObject(this)
37+
?: error("waitToAppear timed out after ${timeOutMillis}ms; no object matched selector: $this")
3138
}
3239

40+
/**
41+
* Waits up to [timeOutMillis] for objects matching this selector and returns the one at [withIndex].
42+
*
43+
* @param withIndex The zero-based index of the object to return.
44+
* @param timeOutMillis Maximum time to wait before failing.
45+
* @throws IllegalStateException when the timeout elapses without enough matching objects.
46+
*/
3347
public fun BySelector.waitToAppear(withIndex: Int, timeOutMillis: Long = defaultTimeout): UiObject2 {
3448
wait(timeOutMillis)
35-
return findObjects()[withIndex]
49+
val objects = device.findObjects(this)
50+
return objects.getOrNull(withIndex)
51+
?: error(
52+
"waitToAppear(withIndex=$withIndex) timed out after ${timeOutMillis}ms; " +
53+
"only ${objects.size} objects matched selector: $this",
54+
)
3655
}
3756

3857
public fun BySelector.wait(timeOutMillis: Long = defaultTimeout): BySelector {
@@ -46,9 +65,9 @@ public fun BySelector.waitToDisappear(timeOutMillis: Long = defaultTimeout): ByS
4665
}
4766

4867
/**
49-
* Polls by re-finding the object on each iteration so a mid-poll recomposition does not produce
50-
* a [StaleObjectException]. Returns the text that was observed when the match succeeded, or the
51-
* last observed text on timeout — never throws. Callers typically wrap the result in an
68+
* Waits for an object matching this selector whose text matches [expectedText]. Returns the
69+
* matched text, or the last observed text on timeout. Never throws — recompositions and
70+
* mid-poll node recycling are absorbed internally, so callers should wrap the result in an
5271
* assertion to surface mismatch/timeout.
5372
*
5473
* @param expectedText The text to match.

0 commit comments

Comments
 (0)