11

I am trying to automate some UI of my Android application(I do not have source code so I am using the APK file) .

I have gone through tutorial provided here and also some tutorial available at Google but all of them require source code.

If anyone have some idea how to automate the UI with Espresso without source code, please help.

I am using IntelliJ as IDE and app android version 5.0.2.

Reg
  • 10,717
  • 6
  • 37
  • 54

3 Answers3

7

The answer is yes, you can run automation test using Espresso without app source code.

Espresso is based on Android instrumentation framework, which means the automation test is built into a single test apk. This test apk is different from normal application apk:

  1. There is an instrumentation registered in AndroidManifest.xml, which will be registered to Android system once test apk is installed

  2. The test apk must be signed using the same signature with the application apk, in order to run automation test

  3. The test apk runs in the same process as application apk

Above are the only requirements of any instrument based test framework has. So there is no dependency of source code.

But why we find most of the Espresso tutorials are mixed with source code? Because it will make the test simpler:

  1. You can easily control the activity lifecycle using class ActivityTestRule.

  2. You can test application defined classes easily.

  3. You can test UI widgets using widget id

On the contrary, you have to write lots of reflection code to get the classes you need if you don't compile with source code. For example:

  1. You have to use Class.forName to load the entrance activity and launch it

  2. You have to use Java reflection to test application defined classes

  3. You have to use literal information to find UI widgets, because you don't have the id of the UI widgets

I think it's due to the above disadvantages, which makes Google prefer to building Espresso test together with source code.

To sum up, it is OK to run Espresso automation test without application source code, but it's much harder and make test codes ugly.

You can refer the example project from AndroidTestWithoutSource.

cmoaciopm
  • 2,076
  • 2
  • 22
  • 30
  • 1
    Do you have a running example of this? – zwebie Aug 28 '17 at 07:48
  • 1
    @zwebie Refer the example project from https://github.com/cmoaciopm/AndroidTestWithoutSource – cmoaciopm Aug 30 '17 at 02:39
  • Any Idea why I would get java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. When running this? – zwebie Aug 31 '17 at 13:18
  • @zwebie Please explain how you run the test. Which app do you test? Looks like, the exception is not relevant to my test project. – cmoaciopm Aug 31 '17 at 13:35
  • Never mind, got it working. The problem was that I created a new Android project. – zwebie Aug 31 '17 at 13:52
  • @cmoaciopm: First of all Thanks for the source code. Actually, I have a scenario where to setup an Env with only Espresso test cases without source code and this is the only link was helpful to me. But, many apis are deprecated. So, i tried to use updated apis. But, I am getting same error "java.lang.llegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity." Please need your help on this. – Akash Patra Feb 11 '21 at 08:23
3

I have gone through tutorial provided here and also some tutorial available at Google but all of them require source code.

That is because Espresso is part of instrumentation testing, and it requires source code.

Other tools — UI Automator and monkeyrunner, for example — do not require source code.

As Espresso is more backward compatible with previous version of Android and also have performance advantage over UIAutomator that why I want to use Espresso

Then talk to the developer of the app and arrange with that person to test the app, with full source code access.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

To use espresso you need to know something about the UI elements themselves (like id and type). When you don't have the source code you can use the "uiautomatorviewer" tool which is part of the Android SDK.

http://developer.android.com/tools/testing-support-library/index.html#uia-viewer

AndroidGuy
  • 3,371
  • 1
  • 19
  • 21
  • As Espresso is more backward compatible with previous version of Android and also have performance advantage over UIAutomator that why I want to use Espresso Also. I know how to automate the apk file with the help of "Robotium" and want to do the same with Espresso. Any input will be helpfull on that. – Dinesh Chandra Sep 07 '15 at 13:37
  • UIAutomator is different than "uiautomatorviewer". The later is a tool that let's you look at the view hierarchy of a running app (without source code) and see the view elements. You can use that knowledge to create Espresso tests. – AndroidGuy Sep 08 '15 at 14:51
  • With the help of "uiautomatorviewer" I can see the view element that's fine. but in script development while extending the InstrumentationTestCase2 class it requires "MainActivity" class name which I have given but when I am running the code its showing "ClassNotFoundException" error because in Android Studio it refers the app that is build with the code not the application which I have already installed on emulater.So I have tried with eclipse and downloaded the dependecy requiered for that but there I am not able to resolve depedency error causing due to some reason. – Dinesh Chandra Sep 10 '15 at 05:53