I am trying to automate my android application. When I start the test I try to switch environment from the debug drawer. While switching environment the application closed and opens a new instance for that particular environment. The test fails the time the application closes.
Getting this error: Reason: 'Instrumentation run failed due to 'Process crashed.''
IS there any way the script does not fail and wait until the new application opens
You need to use uiautomator along with espresso for this kind of use cases.Use Uiautomator to switch the environment and then let the espresso steps run.
For more details refer-https://developer.android.com/topic/libraries/testing-support-library/index.html#UIAutomator
The UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writing black box-style automated tests, where the test code does not rely on internal implementation details of the target app.
Related
In Android Studio, in the androidTest folder, I have a couple of test cases, like as follows :
Screenshot : Android Studio
After each test class is executed, the app exits and is re-launched for next test. User have to login every time.
Is there any way I can login just once and execute all the test cases (entire test suite) without app exiting every-time ?
You can 'mock' this step before each test method (#Before annotation in JUnit) or before the entire test class (#BeforeClass annotation in JUnit) but it requires external dependency for example Mockito.
I need to detect at runtime from the code if the application is run using an Instrumented Test. I'm looking for a solution that works without knowing the Test class.
You can read logcat logs in launcher activity of your application under test and find the instrumented test launch command in logs.
My android app has a service which sends notifications to user based on parameters like number of runs of the app. The notifications are sent at different times in different situations. I want to test whether notifications are sent at the right times in all the different cases. Does android provide a way of such a testing ?
Testing Notification using UIAutomator:
Just go through the below code. It will help you in testing the notification.
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.openNotification();
device.wait(Until.hasObject(By.text(NOTIFICATION_TITLE)), TIMEOUT);
UiObject2 title = device.findObject(By.text(NOTIFICATION_TITLE));
UiObject2 text = device.findObject(By.text(NOTIFICATION_TEXT));
assertEquals(NOTIFICATION_TITLE, title.getText());
assertEquals(NOTIFICATION_TEXT, text.getText());
title.click();
device.wait(Until.hasObject(By.text(ESPRESSO.getName())), TIMEOUT);
Don't forget to add the UIAutomator dependencies in build.gradle.
// UIAutomator dependency
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
Please read this article
http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html
Here is a nice explanation of this topic:
Espresso for Android is perfect and fast test automation framework,
but it has one important limitation - you are allowed to operate only
inside your app under test context.
This means, it is not possible to automate tests for app features such as:
application push notifications
contact synchronization
navigating from another app to your app under test,
Since you have to deal with other apps from the mobile device -
NotificationBar, Contacts or People app, etc.
In fact it wasn't possible until the release of UIAutomator 2.0. As
stated in Android Developers blog post - "...Most importantly, UI
Automator is now based on Android Instrumentation...". And because
of that we can run UIAutomator tests as well as Espresso tests
using Instrumentation test runner.
In addition to that we can combine UIAutomator tests together with
Espresso tests and this gives us the real power and control over the
phone and application under test.
Is there a way out that i can perform actions using espresso once i leave my application to some external application?
I am opening android image picker and want to select an image from the activity
Generally, this is not possible with Espresso. You will get a SecurityException, if you send events to any window controlled by a different user id.
Here are a few things you could try:
create a custom AOSP build and disable the security checks
for API 16+ you can use uiautomator tests instead of instrumentation tests
on API 18+ you can use instrumentation.getUIAutomation()
I have a scenario to test:
In my app, click a button - say, btn1.
btn1 click uses an intent action=VIEW, uri=http://www.m10v.com, category=DEFAULT to launch another activity. This activity belongs to another package (e.g.: browser)
How do I:
Ensure that an application with an activity this intent filter is installed?
How do I test that the activity actually started?
Robolectric should be used for unit testing your code. It stubs out the android runtime, allowing for a fast test/refactor cycle. Tests run on the JVM and not on an emulator or device. Therefore you cannot use it for interacting with other components in the system.
For your scenario there are two options:
Write a unit test and only verify interactions, i.e. checking Intent attributes. You can use Robolectric or ActivityUnitTestCase for that.
Write a functional Ui test using the UiAutomator Framework (Api 16) or use the new UIAutomation Apis introduced with Api 18.