我目前不熟悉使用Android Studio进行自动化测试并使用Espresso进行自动化测试,我尝试对登录屏幕执行自动化测试,我目前无法执行点击操作一个特定的按钮。 我尝试过多种方式按钮对我没用。我指的是以下文档来实现,下面是我的示例代码和崩溃报告。
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule =
new ActivityTestRule<>(LoginActivity.class);
@Test
public void OnLoginButtonClick() throws InterruptedException {
onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText("xxxxx"));
onView(ViewMatchers.withId(R.id.edtPassword)).perform(ViewActions.typeText("xxxxxxx"));
onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.scrollTo(),ViewActions.click());
}
}
android.support.test.espresso.PerformException: Error performing 'scroll to' on view 'Animations or transitions are enabled on the target device.
with id: xxx.xxxx.xxx.xxxx:id/btnLogin'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.msf.opx.LoginTest.OnLoginButtonClick(LoginTest.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
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$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
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 org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
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.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1873)
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView))
Target view: "Button{id=2131165273, res-name=btnLogin, visibility=VISIBLE, width=1320, height=152, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=60.0, y=1387.0, text=Login, input-type=0, ime-target=false, has-links=false}"
at
答案 0 :(得分:3)
目标设备上启用了动画或转场。
由于它们引入的视觉状态延迟,Espresso与动画效果不佳。您需要在设备上禁用动画。首先,enable developer options
打开“设置”应用。
滚动到底部,然后选择关于手机。
滚动到底部,然后点按内置号码7次。
从“设置”应用中访问“开发人员选项”,然后在“绘图”部分下,将以下所有选项切换为动画关闭: