我试图通过以下方式更新EditTextEspresso测试的一部分:
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(clearText())
.perform(click())
.perform(typeText("Another test"));
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: (with class name: a string ending with "EditText" and with text: is "Test")
Run Code Online (Sandbox Code Playgroud)
通过分解测试线我可以看到这在执行之后发生clearText(),因此我假设匹配器在每个之前重新运行perform并且在第二个动作之前失败.虽然这是有道理的,但是让我对如何更新EditText使用Espresso 感到有些困惑.我该怎么做?
请注意,我无法在此方案中使用资源ID或类似信息,并且必须使用上面显示的组合来标识正确的视图.
使用带有子图像视图的自定义小部件视图时,使用Espresso,我可以使用哪种Matcher类型来选择第n个孩子?例:
+--------->NumberSlider{id=2131296844, res-name=number_slider, visibility=VISIBLE, width=700, height=95, has-focus=false, has-focusable=false, has-window-focus=true, 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=10.0, y=0.0, child-count=7}
|
+---------->NumberView{id=-1, visibility=VISIBLE, width=99, height=95, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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}
|
+---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=99.0, y=0.0}
|
+---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=199.0, y=0.0}
|
+---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, has-focus=false, …Run Code Online (Sandbox Code Playgroud) 我有一个ImageButton设计不完全可见的自定义,因此当我执行单击操作时,我收到此错误:
android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: test.com.myproject.app:id/navigationButtonProfile'.
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
按钮的一小部分位于屏幕之外(即,它在顶部被裁剪),可能按钮的12%在屏幕外.这是设计使然,无法滚动或执行任何视图操作以使其可见.任何人都知道如何超越这个90%的约束?
解决方案: 我按照建议创建了自己的点击操作,并且完美运行.我从Google Espresso中复制了该类,并在此方法中将其从90更改为75: …
我花了一整天用Espresso设置Junit4仪器测试,但似乎无法完成最后一步.无论我做什么,都不会认出这种onView()方法.我已经尝试了多个SDK/support-lib版本,到目前为止还没有.我已经浏览了所有谷歌设置指南和许多stackoverflow帖子,转到这里作为我的最后希望.
Gradle版本: com.android.tools.build:gradle:1.5.0
Gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "lv.my.android"
minSdkVersion 9
targetSdkVersion 22
testApplicationId "lv.my.android.tests"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
compileOptions {
sourceCompatibility = 'VERSION_1_7'
targetCompatibility = 'VERSION_1_7'
}
signingConfigs {
beta {
storeFile file("beta.keystore")
storePassword "betabuild"
keyAlias "key"
keyPassword "betabuild"
}
release
}
buildTypes {
debug {
debuggable true
applicationIdSuffix '.debug'
versionNameSuffix '-DEV'
minifyEnabled false …Run Code Online (Sandbox Code Playgroud) 在Android Studio 2.2中应该有一个测试录音功能?我在哪里找到它以及如何使用它?
android automated-tests android-espresso android-studio-2.2 android-espresso-recorder
我一直在努力处理一个遗留的 Android 应用程序,试图为它添加测试和适当的架构。该应用程序有一个主程序LaunchActivity,它在启动时运行一系列检查。最初,该活动使用 Dagger 来“注入依赖项”,活动将使用该依赖项来运行检查。
我将齿轮转向 MVVM,以便我可以单独测试视图模型,无需检测,并且只需要为 UI 测试注入模拟视图模型。我跟着这篇文章介绍了这些变化,包括切换到使用新的 Dagger Android 方法,如AndroidInjection.inject.
我希望测试能够尽可能多地指导任何更改,因此当我的基本架构正常工作时,我转而编写 UI 测试。现在,必须使用 Dagger 将模拟视图模型注入到活动中被证明是一项艰巨的任务,但我认为我已经找到了一个可行的解决方案。
我已经在使用TestApp带有自定义检测运行器的DexOpener,我将其更改为也实现HasActivityInjector,就像App我的应用程序的实际自定义(都扩展Application)。
对于 Dagger,我创建了单独的模块和一个用于测试的组件:
测试应用组件
@Component(
modules = [
TestDepsModule::class,
TestViewModelModule::class,
TestAndroidContributorModule::class,
AndroidSupportInjectionModule::class
]
)
@Singleton
interface TestAppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun testViewModelModule(testViewModelModule: TestViewModelModule): Builder
fun build(): TestAppComponent
}
fun inject(app: TestFieldIApp)
}
Run Code Online (Sandbox Code Playgroud)
测试视图模型模块
@Module
class TestViewModelModule {
lateinit var mockLaunchViewModel: …Run Code Online (Sandbox Code Playgroud) 我有一个ViewPager只包含图片和按钮的项目.
我无法与项目(页面)的UI成功交互,因为除了显示的图片之外,没有什么可以区分(从UI的角度来看)所有项目ViewPager.
我试图只选择一个有位置的项目:
onData(is(instanceOf(ItemClass.class)))
.atPosition(0)
.onChildView(withId(R.id.button))
.perform(click());
Run Code Online (Sandbox Code Playgroud)
造成:
NoMatchingViewException:层次结构中找不到匹配的视图:可从类中分配:class android.widget.AdapterView
如何ViewPager使用Espresso 访问和测试项目?
我一直试图通过测试来覆盖我的Android应用程序,并且最近开始使用espresso.到目前为止,它印象深刻.但是,我的应用程序的大多数功能都要求用户登录.由于所有测试都是独立的,因此需要为每个测试注册一个新用户.这很好,但是每次测试所需的时间因此而大大增加.
我试图找到一种方法在类(测试)中注册一次用户,然后使用相同的用户帐户执行该类中的所有测试.
我能够做到这一点的一种方法是实际上只有一个test(@Test)方法按我想要的顺序运行所有其他测试.然而,这是一种全有或全无的方法,因为gradle cAT任务仅在结束时输出结果一次,而不提供有关可能已通过/失败的中间测试的信息.
我也尝试了这种@BeforeClass方法,但是没有用(没有使用调试选项我使用过它的类的gradle输出,看起来它需要很长时间才能进入下一类测试).
是否有更好的方法在课程开始时注册用户一次,然后在测试结束时注销一次?
任何帮助赞赏.
我正在学习使用浓缩咖啡的android仪器测试.我有一个有抽屉菜单的应用程序,有一个叫做关于的菜单.我正在测试点击该菜单项和活动内容.
testfunction:
@Test
public void testNavigationDrawerAboutMenu() {
onView(withId(R.id.drawer_layout))
.perform(DrawerActions.open()); //open drawer
onView(withText("About")).perform(click());
onView(withId(R.id.aboutsptemail)).check(matches(withText(R.string.screen_about_support_email)));
onView(withId(R.id.aboutcpright)).check(matches(isDisplayed()));
onView(withId(R.id.aboutprivacy)).check(matches(isDisplayed()));
onView(withId(R.id.abouttermsconditions)).check(matches(isDisplayed()));
onView(withId(R.id.aboutsptemail)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
现在最后一个textview中嵌入了weblink.因此,当您点击它时,它会自动在应用程序的Web视图中打开链接(www.support.com).我想测试这个功能.所以我试过这个:
intended(hasComponent(WebViewActivity.class.getName())); //check if webview called on supportEmail link click
Run Code Online (Sandbox Code Playgroud)
但是此错误跟踪测试失败:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference
at android.support.test.espresso.intent.Intents.intending(Intents.java:155)
at com.ScanBuy.SmartLabel.NavigationDrawerActivityTests.testNavigationDrawerAboutMenu(NavigationDrawerActivityTests.java:94)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at …Run Code Online (Sandbox Code Playgroud) 我知道Espresso可以按照UiAutomator的方式点击边界.(x和y坐标)我已经阅读了文档,但我似乎无法找到它.任何帮助表示赞赏.谢谢
编辑
我找到了这个链接,但没有示例如何使用它,我主要关心的是它UiController是如何使用它.
android ×10
android-espresso ×10
ui-testing ×3
junit4 ×2
dagger-2 ×1
java ×1
kotlin ×1
testing ×1
unit-testing ×1