Skip to content

Commit 7bee6d2

Browse files
committed
Add waitAndFind helper
Change-Id: I7498dda3eb686d15829d89688695866ae8382c6e
1 parent 0f98e4b commit 7bee6d2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

benchmarks/src/main/java/androidx/test/uiautomator/UiAutomatorHelpers.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ enum class HasChildrenOp {
4646
EXACTLY,
4747
AT_MOST,
4848
}
49+
50+
/**
51+
* Waits until an object with [selector] if visible on screen and returns the object.
52+
* If the element is not available in [timeout], throws [AssertionError]
53+
*/
54+
fun UiDevice.waitAndFindObject(selector: BySelector, timeout: Long): UiObject2 {
55+
if (!wait(Until.hasObject(selector), timeout)) {
56+
throw AssertionError("Element not found on screen in ${timeout}ms (selector=$selector)")
57+
}
58+
59+
return findObject(selector)
60+
}

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ForYouActions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import androidx.benchmark.macro.MacrobenchmarkScope
2020
import androidx.test.uiautomator.By
2121
import androidx.test.uiautomator.Until
2222
import androidx.test.uiautomator.untilHasChildren
23+
import androidx.test.uiautomator.waitAndFindObject
2324
import com.google.samples.apps.nowinandroid.flingElementDownUp
2425

2526
fun MacrobenchmarkScope.forYouWaitForContent() {
2627
// Wait until content is loaded by checking if topics are loaded
2728
device.wait(Until.gone(By.res("loadingWheel")), 5_000)
2829
// Sometimes, the loading wheel is gone, but the content is not loaded yet
2930
// So we'll wait here for topics to be sure
30-
val obj = device.findObject(By.res("forYou:topicSelection"))
31+
val obj = device.waitAndFindObject(By.res("forYou:topicSelection"), 10_000)
3132
// Timeout here is quite big, because sometimes data loading takes a long time!
3233
obj.wait(untilHasChildren(), 60_000)
3334
}

0 commit comments

Comments
 (0)