I have a problem and a don't know how to solve it. I saw some tools for automation testing for android apps, like appium and others. They connect to the android device, emulator, from outside the device and open the application to be tested. I want to know how can i build a native android app that can to the same thing. Open another application and start executing different operations on the UI of that app. For a simple example, let's say i have a social app that i want to test. I want another that runs on the phone that opens my social app and starts running some operations like searching inside the app, clicking on different posts, liking post, s.o. Is there a way to do this? Are there any frameworks or methods for doing this?
Regards.
You have two kinds of automation frameworks for Android.
Instrumentation-based:
Robotium
Espresso
And black-box frameworks for functional testing:
Appium
Perfecto
ATMOSPHERE
The instrumentation frameworks work in the following way: As each android application runs in a sandbox and other applications can not change their behavior once they are installed, the instrumentation-based frameworks change the installation package of the application injecting hooks in the method definitions that allow them to interact with the application. This allows bypassing the sandbox in Android systems.
The other three all have UIAutomator Android service as basis for the interaction with the application. UIAutomator is part of hte Android SDK and allows emulation of real user interactions (e.g. click, scroll etc.), rahter than simulating them on lower, code level.
I believe that any of the five listed frameworks will allow you achieve what you want. I personally recommend black-box testing frameworks, as instrumentation can hide defects. My favorite framework is ATMOSPHERE - free to use, open sourced and although very recent a lot easier to use.
You would use an explicit intent to open a separate application.
public void openApplication(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i != null) {
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
} else {
Log.e(TAG, "Unable to start application");
return;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package name not found");
return;
}
}
You would then call this method by providing the context and the package name of the application you want to open. For example, to open Instagram:
openApplication(this, "com.instagram.android");
Related
Looking for solution to integrate COSU mode in Android.I have already gone through below links,
https://developer.android.com/work/cosu.html
Has anyone implemented it successfully?
Take a look at this tutorial and that repository with example.
...
if (mDevicePolicyManager.isLockTaskPermitted(
getApplicationContext().getPackageName())) {
Intent lockIntent = new Intent(getApplicationContext(),
LockedActivity.class);
lockIntent.putExtra(EXTRA_FILEPATH, mCurrentPhotoPath);
startActivity(lockIntent);
finish();
} else {
Toast.makeText(getApplicationContext(),
R.string.not_lock_whitelisted,Toast.LENGTH_SHORT)
.show();
}
...
You might want to try Google's new Android Management API, it allows to manage COSU devices without having to build an on-device agent (a device policy controller).
I decided neither way was very good. I used device policy ownership to prevent installing any other apps, wrote a launcher app (so we were the homescreen), made it a system app and used the statusbar manager apis (which aren't well known) to remove the recents button and prevent the status bar from being opened so there was no way to launch any app but mine.
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.
I am looking for a way to automatically open an application on Android. Something like QTP on windows. I read about quite a few testing tools for Android that need the phone to be connected to the laptop via USB. Is it possible to code an android application that can open another specific application on the device automatically?
I understand that if it is my application or an open source one, I can get the UI element and perform click or type into it automatically using code but how can I access UI elements of other apps on the device.
Example: My app should be able to open my phone keypad and type in a number or open an app like Truecaller and type into the textview on main screen? Something like web automation but for Android device. Any help would be appreciated! Thank you!
You can use Intents:
//consider context as being the Context of your current app
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(PACKAGE_NAME);
context.startActivity(intent);
device.wait(Until.hasObject(By.pkg(PACKAGE_NAME)), 10000); //this is a UiAutomator method to wait for the application to be started (or 10 seconds).
You can also use solo UiAutomator methods: open the apps menu, click on the app icon. You can see an example here
I am trying to open a different, already installed android application within another, on click of a button. The new application should be opened in a part of the screen within the calling application.
Currently, my code creates a new intent and runs the called application in that. the calling application disappears. Here's my code:
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.ritwik.camera");
startActivity(intent);
}
});
Ideally, it should open as a part of the same screen, without sidelining the parent(calling) application. How do I do that?
When you start an Intent to execute another application (i.e. because you are implementing a launcher or a main menu replacement) you are actually asking android to execute the application identified with a specific package (or the one satisfying some specific constraints, like the ability to handle images, videos, etc), without any clue or reference about the Activities it contains (nor the ability to get any...).
Therefore I don't think that what you are trying to achieve is possible with the current version of the OS (unless some vendor is providing extensions to do just that, see the comment by Pratik).
The new application should be opened in a part of the screen within the calling application.
This is not possible with conventional third-party application UIs.
AFAIK, the split-screen feature (Adaptive UI) is supported from Android 3.0 onwards.
That has nothing to do with embedding the UI of third-party apps into your own.
So I didn't get what you meant to say by "it's not possible with the current version of the OS"
It is not available on any stock version of Android released up through March 26, 2013 at 9:50am Eastern Time.
Certain device manufacturers, like Samsung, have extended Android with multi-window capabilities. However, the control over those windows lies with the user and the (modified) OS. Unless there is something in their S-Pen SDK for this, you have no way of starting another window.
Android also has RemoteViews, which is a means of passing a simplified UI between processes. Using this, it is possible for one app to embed RemoteViews published by another app. You see this with app widgets on the home screen, for example. However, both apps have to be written with this in mind, such as an app publishing an AppWidgetProvider to supply app widgets to home screens.
As far as I know, this is NOT possible. You can only launch the new activity, but you have no control of it.
EDIT: Some devices offer this possibility using Cornerstone or similar frameworks, but I haven't seen an option for developers to use this for their own apps.
I am trying to automate android app(Relocation services)using robotium. In this app there are media section and email and phone native dialer options so when i click on any of this option(Video, audio, phone, email) it takes you to the native app(video,audio, phone, email) of the phone. So how to handle external application activity using robotium(Like click on back button to come back to my app, or close native camera or video app to come back to my app)
Any answer will be great help.
You can only access your own app from within the instrumentation framework.
There are some options:
remove the default apps and add some fake apps to handle the intent (see https://github.com/bryanl/FakeCamera) for an example. to remove the app: http://oneclickandroid.blogspot.de/2009/01/how-to-remove-defaultpreloaded.html
resign the apps you want to control with your key so you can instrument them (see http://code.google.com/p/robotium/wiki/RobotiumForPreInstalledApps)
install your app with system permissions ( Android INJECT_EVENTS permission ), but haven't tried method yet
Sorry to bump this...
I've just put the camera stub and gallery stub that I made / use on the play store... thought might be of use to you / others for testing the camera and gallery in automated tests :)
https://play.google.com/store/apps/details?id=com.hitherejoe.CameraStub&hl=en
https://play.google.com/store/apps/details?id=com.hitherejoe.GalleryStub&hl=en_GB
I think, Using Robotium you can't access other application resources from your target testing application.
Just use Instrumentation for this,
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
But be sure if you are doing this in Activity then put this in separate thread for run this code..