How to test activity start in another app using Robolectric - android

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.

Related

Switching environment on android Espresso Test

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.

Android Espresso: How to test whether a button click migrates to a new screen?

onView(withId(R.id.BUTTON))
.perform(click())
.check(matches(withContentDescription(R.id.my_layout)));
onView(withId(R.id.tp_layout))
.check(matches(isDisplayed()));
I am trying to test whether button click migrates to next screen or not..
how can I match, with this only first test is running and it displays tests failed.
You can use an Espresso extension - Espresso-Intents.
Here is the official tutorial.
Quick quote:
Espresso-Intents is an extension to Espresso, which enables validation
and stubbing of intents sent out by the application under test. It’s
like Mockito, but for Android Intents.
If your app delegates functionality to other apps or the platform, you
can use Espresso-Intents to focus on your own app's logic while
assuming that other apps or the platform will function correctly. With
Espresso-Intents, you can match and validate your outgoing intents or
even provide stub responses in place of actual intent responses.

Does jBehave work with Robolectric?

I'd like to use Behavior Driven Development (BDD) to develop an Android app. I'm thinking of using jBehave with Robolectric. Someone wrote about using jBehave with Robotium but does anyone know if Robolectric can be used instead of Robotium with jBehave?
Any known issues?
Robotium is a tool for unit/acceptance testing on emulator or real device, Robolectric is library to unit test android code on desktop jvm. I think it would be possible to pair jBehave with Robolectric but again as for me it would be something that doesn't fit well.
As example simple behaviour test on android:
Activity A with list, user press item at position 2, user see Activity B details
for for the second object in the list.
Test on Robotium will:
press on activity A list item
check that activity B shown with UI that represents details
Tests with Robolectric are smaller:
you could check that pressing item on A will fire Intent for launching new activity with specified details
another test would check that B will show details
So you could try with jBehave map "see item details" to Robolectric “Intent fired” but this check is misleading because there so many things could happen from firing Intent to showing Activity with details.
You could in Robolectric also try to fire activity and check details but this level of mocking is too much for BDD which suppose to have as less mocking as possible.

Can we use Espresso for external applications?

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()

Eclipse debugging - any way to pass in "extras" for launching intent?

I'm developing Activity which works on data passed in Intent in extras.
This Activity is supposed to be launched by other activities in my app.
However, during development/debugging, I launch this Activity directly, and want to simulate extras in Intent (obtained from getIntent) to pass in desired testing params (sort of command-line parameters).
In Eclipse Run configurations, I can just select Launch action, but no additional data.
Is there some way? Or I must hardcode testing params in java, and not forget to comment them out when testing is finished?
I think eclipse is just using something comparable to the am start method to launch an application. You should be able to do this manually via adb and specify extras; then once you have it working from the command line you can put it behind a button using eclipse's extensibility features.
Here's a writeup found during a brief search: http://learnandroid.blogspot.com/2008/01/run-android-application-from-command.html
I think you may want to just write some proper tests for this purpose.
Take a look at this:
Android Testing fundamentals
You could then be running your test during development, which would launch the Activity as you want it.

Categories

Resources