Android UI testing is far from intuitive and there is a lot to learn about it. We made a promise to document and share the knowledge we accumulated over the last years.
12 November 2020 stoefln Leave a comment Uncategorized
The year is slowly coming to an end, so just the right time to look into what’s new around test automation frameworks.
Top on the list of the most used testing tools for Android is still Espresso, so I’ll give a brief overview of developments, but then also introduce some new test automation frameworks.
Continue reading 5 New Test Automation Frameworks for Android (2020)
16 September 2020 stoefln Leave a comment Uncategorized
Now, 2 years later, I am dealing with the least favourite topic of software development almost full time. Why? Because I decided to write my own Android testing tool.
Continue reading Why I Chose to Write my Own Android UI testing tool
2 June 2020 stoefln Leave a comment Uncategorized
Writing UI tests on Flutter is not that different from other mobile technologies. We basically need to interact with our app as any other user would do and double check that our UI looks exactly as it should look.
In order to allow us to write UI, each technology has its own component that allows this programmatic interaction. In the Flutter case we have the flutter_driver package which, according to the Flutter documentation “provides tools to create instrumented apps and drive those apps from a test suite”.
Our example will be based on the test project created by default whenever we create a new Flutter project. For those not familiar with the project, it basically shows a counter text in the middle of the screen and a floating action button used to increment the counter. Our goal is to write a simple flutter UI test to verify the counter works as expected.
5 May 2020 stoefln Leave a comment Uncategorized
Choosing the right Android UI Testing tool can be a challenge. There is not a single best UI test automation tool for each case. What “the best” automation tool is depends clearly on your requirements.
There are a lot of parameters you should consider when it comes to UI testing.
Continue reading How to choose the Best Android UI Testing Tool in 2020
7 April 2020 stoefln Leave a comment Uncategorized
It’s a common issue that espresso does not wait for an element to show up before executing the next step. The result is an error similar to this one:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.mycompany.myapp:id/gone
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=2160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x10302f8
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
We covered how to use espresso idling resources and also the problems of idling resources. A nicer way to handle this is to use ViewActions.
The ViewAction class is part of the public test framework API which allows devs to write their own ViewAction implementations when necessary. Here you can see how a wait-for-ui-element implementation would look like:
// CustomViewActions.kt:
/**
* This ViewAction tells espresso to wait till a certain view is found in the view hierarchy.
* @param viewId The id of the view to wait for.
* @param timeout The maximum time which espresso will wait for the view to show up (in milliseconds)
*/
fun waitForView(viewId: Int, timeout: Long): ViewAction {
return object : ViewAction {
override fun getConstraints(): Matcher {
return isRoot()
}
override fun getDescription(): String {
return "wait for a specific view with id $viewId; during $timeout millis."
}
override fun perform(uiController: UiController, rootView: View) {
uiController.loopMainThreadUntilIdle()
val startTime = System.currentTimeMillis()
val endTime = startTime + timeout
val viewMatcher = withId(viewId)
do {
// Iterate through all views on the screen and see if the view we are looking for is there already
for (child in TreeIterables.breadthFirstViewTraversal(rootView)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return
}
}
// Loops the main thread for a specified period of time.
// Control may not return immediately, instead it'll return after the provided delay has passed and the queue is in an idle state again.
uiController.loopMainThreadForAtLeast(100)
} while (System.currentTimeMillis() < endTime) // in case of a timeout we throw an exception -> test fails
throw PerformException.Builder()
.withCause(TimeoutException())
.withActionDescription(this.description)
.withViewDescription(HumanReadables.describe(rootView))
.build()
}
}
}
And then it’s actually quite simple to use:
// LoginTest.kt:
@Test
fun shouldCloseDialog(){
onView(withId(R.id.show_dialog_button)).perform(click())
// here espresso needs to wait till the dialog actually shows and the close_button is visible
onView(isRoot()).perform(waitForView(R.id.close_button, 5000))
}
7 April 2020 stoefln Leave a comment espresso
When you read the docs, you will notice that Thread.sleep should be avoided. Still, you might run into situations where you find that your tests fail without a line of Thread.sleep(). This can happen if you are doing background work like fetching data from a server or calculating something on another thread via RxJava or RxKotlin.
Continue reading Android Espresso: Why too much Thread.sleep is bad for you
27 March 2020 stoefln Leave a comment Uncategorized
“How do we make espresso wait for animations to finish?” you might ask. While espresso does a good job of waiting for certain things happening on the UI thread, espresso does not wait for animations to finish. While this does not have to be a problem (we have a bunch of tests which work just fine with having animations enabled), it can cause issues.
20 March 2020 stoefln Leave a comment Uncategorized
Are you looking for a suitable framework for automated Android UI testing? Is the testing process taking away from the time you would rather spend developing more apps?
An Android framework for automated UI testing is a crucial part of the process of app development. Automated testing is necessary to produce high-quality applications in due time.
There are many such frameworks available today. However, all these frameworks have their pros and cons. To optimize automated testing for your android apps, the right choice of frameworks is critical. But a large number of available frameworks make this decision rather tricky.
Continue reading 8 Android Frameworks for Automated UI Testing
8 March 2020 stoefln Leave a comment espresso
While waiting for a view to show up is usually handled by espresso itself, there might be cases where your app is doing work on another thread (fetching data from a server, calculating something) and therefore espresso does not know about it.
2 March 2020 stoefln Leave a comment espresso
Espresso is aware that it needs to wait with assertions and view interactions until an activity is launched and displayed on the screen. So there is nothing special to consider for you.
By default, Espresso waits for UI events in the current message queue to process and default AsyncTasks* to complete before it moves on to the next test operation. This should address the majority of application/test synchronization in your application.
© NetRabbit e.U. 2020. All rights reserved.
Imprint | Privacy Policy
This site is protected by reCAPTCHA. The Google Privacy Policy and Terms of Service apply.