testUI (Jenkins) using espresso testUI (Jenkins) using espresso jenkins jenkins

testUI (Jenkins) using espresso


Stacktrace means that Espresso can't find application window. During testing on emulators screen usually hidden behind Screen Lock. You need use some code to disable ScreenLock programmatically.The most convenient way for me to unlock screen is using Robotium. It has solo.unlockScreen() method. I've put it into setUp() method of testing lifecycle.

Links:https://github.com/RobotiumTech/robotium/blob/master/robotium-solo/src/main/java/com/robotium/solo/Solo.java


The solution is to create a custom test runner to unlock the screen and start a wake lock until the tests are done.

public class TestRunner extends android.support.test.runner.AndroidJUnitRunner{    private PowerManager.WakeLock mWakeLock;    @Override    public void callApplicationOnCreate(Application app)    {        // Unlock the screen        KeyguardManager keyguard = (KeyguardManager) app.getSystemService(Context.KEYGUARD_SERVICE);        keyguard.newKeyguardLock(getClass().getSimpleName()).disableKeyguard();        // Start a wake lock        PowerManager power = (PowerManager) app.getSystemService(Context.POWER_SERVICE);        mWakeLock = power.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, getClass().getSimpleName());        mWakeLock.acquire();        super.callApplicationOnCreate(app);    }    @Override    public void onDestroy()    {        mWakeLock.release();        super.onDestroy();    }}

Then AndroidManifest.xml of your tests and add the necessary permissions:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/><uses-permission android:name="android.permission.WAKE_LOCK"/>

Don't forget to edit your build.gradle to use the new testInstrumentationRunner class.

Source


From my point of view, you need to organize your Jenkins job in the following way: before running Espresso tests (I suppose you have connectedAndroidTest in a gradle post-build action there or something like this), add a post-build action to uninstall the app package and another one to uninstall the app test package. Example: com.example.mypackage.debug and com.example.mypackage.debug.test. This is a good way to refresh the app into your smartphone/emulator, in order to let Espresso use its magic.

Also be sure that you have the Always On option in Developer Tools activated (depending on the device, in Settings you have Developer options or Developer tools submenu).

If none of this work, it means that somewhere in your Jenkins job you have some order of actions issue and need to be clarified. For me the uninstalling of the packages helped me clean the system of all the things related to my app.

Hopefully it will work for you too.

If not, please write comment and I will help you.

Good luck!