java Android 浓缩咖啡:PerformException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44005338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:52:56  来源:igfitidea点击:

Android Espresso: PerformException

javaandroidandroid-espresso

提问by Andrey Danilov

I`m getting

我得到

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.

android.support.test.espresso.PerformException:在视图“目标设备上启用动画或转换”上执行“发送 keyCode:4,元状态:0 键事件”时出错。

I read articles with same error but didnt find answer.

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

I have simple android app and I try to test simple thing:

我有一个简单的 android 应用程序,我尝试测试简单的事情:

  1. Load RecyclerView
  2. Click on RecyclerView`s item
  3. Open Activity
  4. Press button inside Activity
  5. Press back
  6. Repeat from part 1.
  1. 加载 RecyclerView
  2. 点击 RecyclerView 的项目
  3. 打开活动
  4. 按下 Activity 内的按钮
  5. 按回
  6. 从第 1 部分重复。

Codeis extremely easy:

代码非常简单:

@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;
}

It throws exception after 10-50 iterations (randomly) so basically code is right.

它在 10-50 次迭代(随机)后抛出异常,所以基本上代码是正确的。

EspressoTestDev.java:88 line where it crashes is

EspressoTestDev.java:88 崩溃的行是

Espresso.pressBack();

Exactly after this line I`m getting exception.

就在这一行之后,我遇到了异常。

Exception stacktrace:

异常堆栈跟踪:

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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access
    - 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 &
0(ParentRunner.java:58) at org.junit.runners.ParentRunner.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.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access
onView(...).perform(scrollTo(), click());
0(ParentRunner.java:58) at org.junit.runners.ParentRunner.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.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)

Looks like it found problem with PopupViewContainerbut I dont use it, dont click it and I have no idea how can it impact to Espresso.pressBack()

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

What I tried:

我试过的:

  1. Adding getInstrumentation().waitForIdleSync();before and after Espresso.pressBack()

  2. Making delay as suggested here

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

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

The question is:how can I avoidthis error or how can I ignoreand continue iterations in my case?

问题是:我怎样才能避免这个错误,或者在我的情况下我怎样才能忽略并继续迭代?

回答by Michael Dodd

Animations or transitions are enabled on the target device.

在目标设备上启用动画或转换。

Espresso doesn't work well with animations due to the visual state delays they introduce. You need to disable animations on your device. Firstly, enable developer options:

由于动画引入了视觉状态延迟,Espresso 不能很好地处理动画。您需要禁用设备上的动画。首先,启用开发者选项

  1. Open the Settings app.
  2. Scroll to the bottom and select About phone.
  3. Scroll to the bottom and tap Build number 7 times.
  4. Return to the previous screen to find Developer options near the bottom.
  1. 打开设置应用程序。
  2. 滚动到底部并选择关于手机。
  3. 滚动到底部并点击 Build number 7 次。
  4. 返回上一屏幕以在底部附近找到开发人员选项。

Access Developer Options from Settings app, and under the Drawingsection, switch all of the following options to Animation Off:

从 Settings 应用程序访问 Developer Options,然后在Drawing部分下,将以下所有选项切换到Animation Off

  • Window animation scale
  • Transition animation scale
  • Animator duration scale
  • 窗口动画比例
  • 过渡动画比例
  • 动画持续时间比例

回答by Simon

In addition to the accepted answer, if you want to open the emulator from console, you have to add the following commands:

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

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{...}"

回答by Atupele4

ensure the control you want to work with is visible. this worked for me. add "scrollTo()"

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

##代码##

回答by Michael Osofsky

I've run into the error Animations or transitions are enabled on the target deviceseveral times. In the latest case, the real problem was that my UI code was throwing a NullPointerException. Unfortunately Espresso was "swallowing" the NullPointerExceptionand giving me the irrelevant error Animations or transitions are enabled on the target device.

我已经Animations or transitions are enabled on the target device多次遇到错误。在最新的案例中,真正的问题是我的 UI 代码抛出了一个NullPointerException. 不幸的是,Espresso 正在“吞下” theNullPointerException并给我一个无关紧要的错误Animations or transitions are enabled on the target device

回答by mokagio

In my case, Espresso gave me the "Animations or transitions are not enabled..." error, but that wasn't actually the root cause.

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

Scrolling through the stack trace I found this:

滚动堆栈跟踪我发现了这个:

##代码##

That is, I was trying to scroll something that can't be scrolled.

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

So, always read through the stacktrace folks

所以,请务必通读堆栈跟踪人员

回答by pricimus

I had a similar error but after inspecting the logcat, I realised the fragment under test was trying to do a navigation and the nav graph wasn't defined in the test because the fragment had been launched with launchFragmentInContainer. Fixing that fixed the issue.

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

回答by Peter J

A simple try catch. Where you only pick up the exception if it is an PerformException.

一个简单的尝试捕获。如果它是一个 PerformException,你只拿起异常。

Alternativ add the 500 items to your shopping cart by code, and then add 1 extra by UI test.

另一种方法是通过代码将 500 件商品添加到您的购物车,然后通过 UI 测试额外添加 1 件。