Android Espresso: PerformException



我得到

android.support.test.espresso.PerformException:执行"发送"时出错 键代码:4,元状态:0 关键事件"在视图"动画或过渡 在目标设备上启用。

我阅读了具有相同错误的文章,但没有找到答案。

我有简单的安卓应用程序,我尝试测试简单的东西:

  1. 负载回收器视图
  2. 点击回收查看的项目
  3. 打开活动
  4. 按下活动内的按钮
  5. 按回
  6. 重复第 1 部分。

代码非常简单:

@Test
public void getOver500Products() {
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
onView(withId(R.id.layout2)).perform(click());
clickExactItem(2);
for (int i = 0; i< 501; i++) {
clickRandomItem();
addedToCart();
Espresso.pressBack();
}
}
public void clickRandomItem() {
try {
int x = getRandomRecyclerPosition(R.id.list);
clickExactItem(x);
} catch (NoMatchingViewException e) {
}
}
public void clickExactItem(int position) {
onView(withId(R.id.list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(position, click()));
}
public boolean addedToCart() {
try {
onView(withId(R.id.product_add_in_cart)).perform(click());
} catch (NoMatchingViewException e) {
return false;
}
return true;
}

它在 10-50 次迭代(随机)后引发异常,因此基本上代码是正确的。

EspressoTestDev.java:88 行崩溃的地方是

Espresso.pressBack();

正是在这一行之后,我得到了例外。

异常堆栈跟踪:

android.support.test.espresso.PerformException: Error performing 'send keyCode: 4, metaState: 0 key event' on view 'Animations or transitions are enabled on the target device.
is a root view.'.
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 android.support.test.espresso.Espresso.pressBack(Espresso.java:189)
at com.app.myapp.EspressoTestDev.getOver500Products(EspressoTestDev.java:88)
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:1851)
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
is displayed on the screen to the user
Target view: "PopupViewContainer{id=-1, visibility=VISIBLE, width=0, height=0, 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=0.0, y=0.0, child-count=1}"
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:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

看起来它发现了PopupViewContainer的问题,但我不使用它,不要点击它,我不知道它如何影响Espresso.pressBack()

我尝试过:

  1. Espresso.pressBack()之前和之后添加getInstrumentation().waitForIdleSync();

  2. 按照此处的建议进行延迟

问题是:如何避免此错误,或者如何忽略并继续迭代?

在目标设备上启用动画或过渡。

浓缩咖啡不适用于动画,因为它们引入了视觉状态延迟。您需要禁用设备上的动画。首先,启用开发人员选项:

  1. 打开"设置"应用。
  2. 滚动到底部,然后选择关于手机。
  3. 滚动到底部,然后点击内部版本号 7 次。
  4. 返回上一个屏幕,在底部附近找到开发人员选项。

从"设置"应用访问"开发人员选项",然后在"绘图"部分下,将以下所有选项切换到"动画关闭":

  • 窗口动画比例
  • 过渡动画比例
  • 动画师持续时间比例

除了接受的答案之外,如果要从控制台打开模拟器,还必须添加以下命令:

- adb shell settings put global window_animation_scale 0 &
- adb shell settings put global transition_animation_scale 0 &
- adb shell settings put global animator_duration_scale 0 &

就我而言,Espresso 给了我"动画或过渡未启用..."错误,但这实际上不是根本原因。

滚动浏览堆栈跟踪,我发现:

androidx.test.espresso.PerformException: Error performing 'scroll to' on view 'Animations or transitions are enabled on the target device.
...
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 or
is assignable from class: class android.widget.ListView
))
Target view: "LinearLayout{...}"

也就是说,我试图滚动无法滚动的内容。

所以,一定要通读堆栈跟踪的人 👌

确保要使用的控件可见。 这对我有用。 添加"滚动到()">

onView(...).perform(scrollTo(), click());

我已经多次遇到错误Animations or transitions are enabled on the target device。 在最新情况下,真正的问题是我的 UI 代码抛出了一个NullPointerException。 不幸的是,浓缩咖啡正在"吞下"NullPointerException并给了我无关紧要的错误Animations or transitions are enabled on the target device

我有一个类似的错误,但在检查日志猫后,我意识到被测试的片段正在尝试进行导航,并且导航图未在测试中定义,因为片段已使用 launchFragmentInContainer 启动。修复解决了问题。

在单击特定位置的项目之前,应滚动该位置,

onView(withId(R.id.list))
.perform(RecyclerViewActions
.scrollToPosition(position));
onView(withId(R.id.list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(position, click()));

一个简单的尝试捕获。 仅当异常是执行异常时,您才会选取异常。

Alternativ 通过代码将 500 件商品添加到您的购物车,然后通过 UI 测试额外添加 1 件商品。

相关内容

  • 没有找到相关文章

最新更新