HomeAndroidAccessing Composables from UiAutomator | by Tomáš Mlynarič | Android Builders |...

Accessing Composables from UiAutomator | by Tomáš Mlynarič | Android Builders | Feb, 2023


How are you going to use UiAutomator in Jetpack Compose apps?

UiAutomator has a number of methods of accessing UI parts on display relying on a component sort. You should utilize the By selector class for accessing parts. You should utilize By.textual content() to entry parts by textual content label, By.desc() to entry by contentDescription, by aspect flags akin to By.scrollable(), By.checkable(), By.clickable(), and many others; and By.res() to entry a component by its resource-id android:id="@+id/some_id".

First, you might want to allow testTagAsResourceId within the composable hierarchy you need to take a look at. This flag will allow changing the testTag to useful resource identifiers for all nested composables. When you’ve got a single Exercise Compose venture, you’ll be able to allow it solely as soon as near the basis of the composable tree. It will guarantee all the nested composables with Modifier.testTag are accessible from the UiAutomator.

/* Copyright 2022 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

Scaffold(
modifier = Modifier.semantics {
testTagsAsResourceId = true
},
// ...
)

/* Copyright 2022 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

LazyVerticalGrid(
modifier = modifier
.fillMaxSize()
.testTag("forYou:feed"),
// ...
)

/* Copyright 2022 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

class BaselineProfileGenerator {
@get:Rule
val rule = BaselineProfileRule()

@Take a look at
enjoyable generate() {
rule.collectBaselineProfile(PACKAGE_NAME) {
// This block defines the app's vital consumer journey.
// Right here we're all in favour of optimizing for app startup.
pressHome()
startActivityAndWait()
}
}
}

/* Copyright 2022 Google LLC. 
SPDX-License-Identifier: Apache-2.0 */

class BaselineProfileGenerator {
@get:Rule
val rule = BaselineProfileRule()

@Take a look at
enjoyable generate() {
rule.collectBaselineProfile(PACKAGE_NAME) {
// This block defines the app's vital consumer journey.
// Right here we're all in favour of optimizing for app startup.
pressHome()
startActivityAndWait()

// Wait till content material is asynchronously loaded.
// We discover aspect with resource-id "forYou:feed", which equals to Modifier.testTag("forYou:feed")
system.wait(Till.hasObject(By.res("forYou:feed")), 5_000)
val feedList = system.findObject(By.res("forYou:feed"))

// Set some margin from the perimeters to forestall triggering system navigation
feedList.setGestureMargin(system.displayWidth / 5)

// Fling the feed
feedList.fling(Route.DOWN)
system.waitForIdle()
feedList.fling(Route.UP)
}
}
}

After studying this text you’ve seen that enabling UiAutomator interoperability with Jetpack Compose is simple and you’ll go away the content material description for accessibility functions.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments