Espresso Web对于现有元素

时间:2018-08-23 10:04:03

标签: android automation android-espresso

我正在尝试使Webview登录屏幕自动化,并且使用了Espresso和Cucumber测试运行程序的组合来运行自动化脚本。 依赖项:

 androidTestImplementation 'com.android.support:support-annotations:27.1.1'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude module: 'junit'
    exclude module: 'runner'
}
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'design'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}
androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestImplementation 'info.cukes:cucumber-jvm-deps:1.0.3', {
    exclude module: 'cucumber-jvm-deps'
    exclude module: 'guava'
}
androidTestImplementation 'com.squareup.spoon:spoon-client:1.6.4'
androidTestImplementation 'io.cucumber:cucumber-java:3.0.2'
androidTestImplementation 'io.cucumber:cucumber-android:3.0.2'
androidTestImplementation 'io.cucumber:cucumber-core:3.0.2'
androidTestImplementation('com.android.support.test:runner:1.0.2') {
    exclude module: 'junit'
}

当我运行以下代码以查找Web视图是否已加载时:

 while (!isLoginScreenAppeared) {
        //try catch block to avoid atom evaluation returned null exception in few devices
        try {
            Log.i("Automation", "trying to find webview ");
            onWebView().withElement(findElement(Locator.XPATH, "//h1[contains(text(),'Login')]"));
            isLoginScreenAppeared = true;
        } catch (Exception ex) {
            if (Calendar.getInstance().getTime().after(waitTime.getTime()))
                throw new RuntimeException("login screen didn't appear with in 30 seconds. Exception : " + ex.getMessage());
        }
    }

即使在加载登录屏幕并且存在带有文本Login的元素H1时(我已经使用uiautomatorviewer确认该元素存在,以上代码仍返回空指针异常。

任何帮助解决这种奇怪行为的方法??

StackTrace:

08-23 11:00:13.265 W / System.err:java.lang.NullPointerException 08-23 11:00:13.266 W / System.err:在android.support.test.espresso.core.internal.deps.guava.base.Preconditions.checkNotNull(Preconditions.java:882) 08-23 11:00:13.266 W / System.err:位于android.support.test.espresso.PerformException。(PerformException.java:41) 08-23 11:00:13.267 W / System.err:位于android.support.test.espresso.PerformException。(PerformException.java:31) 08-23 11:00:13.267 W / System.err:位于android.support.test.espresso.PerformException $ Builder.build(PerformException.java:82) 08-23 11:00:13.268 W / System.err:位于android.support.test.espresso.web.action.AtomAction.perform(AtomAction.java:122) 08-23 11:00:13.268 W / System.err:at android.support.test.espresso.ViewInteraction $ SingleExecutionViewAction.perform(ViewInteraction.java:356) 08-23 11:00:13.269 W / System.err:在android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248) 08-23 11:00:13.269 W / System.err:at android.support.test.espresso.ViewInteraction.access $ 100(ViewInteraction.java:63) 08-23 11:00:13.270 W / System.err:at android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:153) 08-23 11:00:13.270 W / System.err:at android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:150) 08-23 11:00:13.270 W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237) 08-23 11:00:13.271 W / System.err:at android.os.Handler.handleCallback(Handler.java:751) 08-23 11:00:13.271 W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95) 08-23 11:00:13.272 W / System.err:位于android.os.Looper.loop(Looper.java:154) 08-23 11:00:13.272 W / System.err:位于android.app.ActivityThread.main(ActivityThread.java:6823) 08-23 11:00:13.272 W / System.err:at java.lang.reflect.Method.invoke(本机方法) 08-23 11:00:13.273 W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1557) 08-23 11:00:13.273 W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

谢谢!

0 个答案:

没有答案