Android 使用浓缩咖啡的testUI(詹金斯)

Android 使用浓缩咖啡的testUI(詹金斯),android,jenkins,android-espresso,Android,Jenkins,Android Espresso,该应用程序正在本地通过浓缩咖啡测试,我的意思是直接通过设备和genymotion模拟器。当我使用Jenkins构建应用程序的图像时。浓缩咖啡测试不成功,我得到这个错误 詹金斯: java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a

该应用程序正在本地通过浓缩咖啡测试,我的意思是直接通过设备和genymotion模拟器。当我使用Jenkins构建应用程序的图像时。浓缩咖啡测试不成功,我得到这个错误

詹金斯:

 java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
Root{application-window-token=android.view.ViewRootImpl$W@536a97d4, window-token=android.view.ViewRootImpl$W@536a97d4, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#100 ty=1 fl=#1810100 pfl=0x8 wanim=0x103028f}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=800, height=1184, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
. All Roots:
Root{application-window-token=android.view.ViewRootImpl$W@536a97d4, window-token=android.view.ViewRootImpl$W@536a97d4, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#100 ty=1 fl=#1810100 pfl=0x8 wanim=0x103028f}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=800, height=1184, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
at com.google.android.apps.common.testing.ui.espresso.base.RootViewPicker.get(RootViewPicker.java:84)
at com.google.android.apps.common.testing.ui.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:51)
at com.google.android.apps.common.testing.ui.espresso.ViewInteractionModule$$ModuleAdapter$ProvideRootViewProvidesAdapter.get(ViewInteractionModule$$ModuleAdapter.java:187)
at com.google.android.apps.common.testing.ui.espresso.ViewInteractionModule$$ModuleAdapter$ProvideRootViewProvidesAdapter.get(ViewInteractionModule$$ModuleAdapter.java:151)
at com.google.android.apps.common.testing.ui.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:52)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$2.run(ViewInteraction.java:141)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Stacktrace表示Espresso找不到应用程序窗口。在模拟器上进行测试时,屏幕通常隐藏在屏幕锁后面。您需要使用一些代码以编程方式禁用屏幕锁。 对我来说,解锁屏幕最方便的方法是使用Robotium。它有solo.unlockScreen()方法。我已经将其放入测试生命周期的setUp()方法中

链接:

解决方案是创建一个自定义的测试运行程序来解锁屏幕并启动唤醒锁,直到测试完成

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();
    }
}
然后检查测试的
AndroidManifest.xml
,并添加必要的权限:

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

不要忘记编辑您的
build.gradle
以使用新的
testInstrumentationRunner


在我看来,您需要按照以下方式组织Jenkins的工作:在运行浓缩咖啡测试之前(我假设您在gradle post build action中连接了AndroidTest或类似的内容),添加一个post build action以卸载应用程序包,再添加一个以卸载应用程序测试包。示例:com.Example.mypackage.debug和com.Example.mypackage.debug.test。这是一种将应用程序刷新到智能手机/模拟器中的好方法,以便让浓缩咖啡发挥其魔力

另外,请确保已激活“开发人员工具”中的“始终打开”选项(取决于设备,在“设置”中,您有“开发人员选项”或“开发人员工具”子菜单)

如果这些都不起作用,这意味着在詹金斯的工作中,你有一些行动顺序问题,需要澄清。对我来说,卸载软件包帮助我清理了系统中与我的应用程序相关的所有东西

希望它也能对你起作用

如果没有,请写评论,我会帮助你


祝你好运

我建议使用,在测试时禁用动画并解锁屏幕

这个堆栈跟踪来自哪个设备?jenkins是这么说的。堆栈跟踪来自运行在android模拟器或android设备上的Espresso。您是否在Jenkins服务器上运行模拟器?您的模拟器是否使用“--无窗口”运行?尝试在本地使用与Jenkins机器上完全相同的AVD进行复制。我的无人机也有同样的问题,您是否找到了解决此问题的方法?在scytale的回答之上,您还可以提供您建议的实现的简单示例。Test-Butler只能用于模拟器,因此,如果您使用模拟器来运行测试,这是一个好主意。如果您在实际设备上进行测试,我建议您使用
UiAutomator
。此解决方案使用一些不推荐使用的方法和常量。你能提供一些替代方案吗?