roughly Accessing Composables from UiAutomator | by Tomáš Mlynarič | Android Builders | Feb, 2023 will lid the newest and most present steering simply in regards to the world. open slowly consequently you perceive skillfully and appropriately. will buildup your data properly and reliably
How are you going to use UiAutomator in Jetpack Compose apps?
UiAutomator is a UI testing framework appropriate for testing the whole system and put in functions. It lets you work together with the seen components on a display screen regardless of which utility is in focus.
Whereas many libraries depend on working as a part of the applying course of, UiAutomator doesn’t. It lets you write integration checks, which do not know something in regards to the inside workings of the applying. That is essential for benchmark profiling and efficiency checks (utilizing Jetpack Macrobenchmark) the place it’s endorsed to run a profilable launch construct.
To supply dependable outcomes, the testing framework should work together with the applying, however circuitously manipulate it. And UiAutomator does simply that, delivering enter occasions the identical manner an everyday person would, however extra persistently.
UiAutomator has a number of methods to entry UI components on the display screen relying on the kind of ingredient. you need to use the By
selector class to entry the weather. You should utilize By.textual content()
to entry components by textual content label, By.desc()
entry by contentDescription
by ingredient flags like By.scrollable()
, By.checkable()
, By.clickable()
, and so on; and By.res()
to entry a component by its useful resource ID android:id="@+id/some_id"
.
Within the View system, you usually set an identifier to allow entry and set the properties of that View
. On this case accessing a component isn’t an issue, as a result of you need to use By.res(packageName, "some_id")
to manage that View after which work together with it.
Jetpack Compose renders the person interface in a different way than the View system. Compose has a declarative manner of describing the person interface, so it would not require useful resource identifiers (android:id
). Whereas very handy for builders, it may be problematic for UiAutomator to entry components that do not have distinctive identifiers, akin to textual content tags or a component indicator. That is particularly essential for format parts, akin to Row
, Column
, LazyColumn
and others.
Technically, it’s potential to make use of the contentDescription
to get entry to a composable, however use it solely when it is smart for accessibility functions. He contentDescription
The accessibility framework makes use of the parameter, and when you add tags that are not essential to people, you make your app harder to make use of for individuals who depend on accessibility.
As an alternative, with Jetpack Compose you may benefit from Modifier.testTag()
. This isn’t enabled by default, so let’s examine how one can allow it.
First it is advisable allow testTagAsResourceId
within the composable hierarchy you need to take a look at. This flag will help you convert the testTag
to the useful resource ids for all nested composables. If in case you have a single exercise compose mission, you may allow it solely as soon as close to the foundation of the composable tree. This may be certain that all nested composables with Modifier.testTag
are accessible from the UiAutomator.
Be aware: This flag is on the market in Jetpack Compose 1.2.0 or later. When you use compose-bomany model will comprise this function.
Within the Now on Android sampler, we modified the Scaffold
which is a part of NiaApp
composable This composable is the foundation composable (apart from NiaTheme
that isn’t capable of set up any Modifier
). You may add the Modifier.semantics
with testTagAsResourceId = true
as proven within the following snippet:
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */Scaffold(
modifier = Modifier.semantics
testTagsAsResourceId = true
,
// ...
)
After you have that, you need to use Modifier.testTag("identifier")
anyplace within the Composables hierarchy and the ids might be propagated to UiAutomator as a useful resource ID.
Within the ForYouScreen composable, let’s add the Modifier.testTag("forYou:feed")
towards LazyVerticalGrid
. The parameter identify is unfair, it would not must comply with the identical sample that we choose for Now in Android.
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */LazyVerticalGrid(
modifier = modifier
.fillMaxSize()
.testTag("forYou:feed"),
// ...
)
Now you can entry the LazyVerticalGrid
UI testing with out sacrificing the content material description for the take a look at. From the checks, you need to use By.res("forYou:feed")
selector.
In our case, we used the UiAutomator for benchmarking and benchmark profiling.
For the baseline profiler, write an instrumentation take a look at like this following snippet.
/* 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 crucial person journey.
// Right here we're involved in optimizing for app startup.
pressHome()
startActivityAndWait()
This take a look at will launch the default exercise a number of occasions to generate a baseline profile.
However it could possibly even transcend app launch optimization. You may optimize the runtime efficiency of the feed that hundreds on the primary display screen. So wait and discover the listing of feeds utilizing By.res("forYou:feed")
selector and do some interactions with it.
/* 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 crucial person journey.
// Right here we're involved in optimizing for app startup.
pressHome()
startActivityAndWait()
// Wait till content material is asynchronously loaded.
// We discover ingredient 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(Path.DOWN)
system.waitForIdle()
feedList.fling(Path.UP)
Watch out! There’s a distinction in utilization
By.res(packageName, "identifier")
andBy.res("identifier")
. The primary might be on the lookout for@packageName:id/identifier
within the UI hierarchy, whereas the latter just for the identifier, which is required for theModifier.testTag
.
In our instance, when you had been to make use of By.res("com.google.samples.apps.nowinandroid", "forYou:feed")
would lead to UiAutomator looking for a UI ingredient with @com.google.samples.apps.nowinandroid:id/forYou:feed
resource-id, but it surely would not exist, so the take a look at fails with java.lang.NullPointerException
. The right manner, with out the bundle identify: By.res("forYou:feed")
will remedy forYou:feed
resource-id, which is appropriately discovered on the display screen.
After studying this text, you’ve got seen that enabling UiAutomator interop with Jetpack Compose is simple, and you may go away the content material description for accessibility.
To dive right into a extra complete pattern, try the Now in Android benchmarking module that benchmarks and measures efficiency. The Efficiency Samples Github repository additionally makes use of this interop and exhibits different efficiency samples. To be taught extra about baseline profiles, try the documentation, or for a extra hands-on strategy, try our baseline profiles code lab. Additionally, you may verify for brand spanking new UiAutomator updates.
Subsequent process: write the customized reference profiler and get the efficiency enhancements.
I want the article roughly Accessing Composables from UiAutomator | by Tomáš Mlynarič | Android Builders | Feb, 2023 provides perception to you and is helpful for toting as much as your data
Accessing Composables from UiAutomator | by Tomáš Mlynarič | Android Developers | Feb, 2023