小编How*_*ieH的帖子

使用Espresso单击不完全可见的imageButton

我有一个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: …

testing android ui-testing android-espresso

30
推荐指数
5
解决办法
1万
查看次数

测试背景颜色浓咖啡Android

是否可以使用浓缩咖啡检查背景颜色是否与给定颜色匹配?

我制作了一个自定义匹配器,类似于@Irfan建议的,谢谢!

public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) {
    return buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) {
    final int[] color = new int[1];
    return new BoundedMatcher<Object, Button>( Button.class) {
        @Override
        public boolean matchesSafely(final Button actualObject) {

            color[0] =((ColorDrawable) actualObject.getBackground()).getColor();


            if( expectedObject.matches(color[0])) {
                return true;
            } else {
                return false;
            }
        }
        @Override
        public void describeTo(final Description description) {
            // Should be improved!
            description.appendText("Color did not match "+color[0]);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

testing android android-espresso

15
推荐指数
3
解决办法
1万
查看次数

第一次打开fragment时ComposeView非常慢

我将 compose 添加到现有项目中。我用compose重写了一个fragment ui,当我启动该fragment时,需要很长时间才能启动。

从另一个片段添加一个片段:

 val fragment = FragmentWithComposeUi()
 requireActivity().addFragment(fragment, R.id.fragment_container, "FragmentWithComposeUi")
Run Code Online (Sandbox Code Playgroud)

addFragment()添加片段的功能。

fun FragmentActivity.addFragment(fragment: Fragment, container: Int, tag:String) {
    val currentFragment = supportFragmentManager.findFragmentByTag(tag)
    if (currentFragment == null) {
        supportFragmentManager.beginTransaction()
            .setReorderingAllowed(true)
            .add(container, fragment, tag)
            .addToBackStack(tag)
            .commit()
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentWithComposeUi 类:

class FragmentWithComposeUi: Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply{
            setContent {
               //some ui
            }
        }
    }

Run Code Online (Sandbox Code Playgroud)

构建.gradle

我也尝试使用新的 compose 版本 1.3.0-alpha01,但没有帮助。

buildscript {
    ext {
        compose_version = '1.1.1'
        compose_compiler_version = …
Run Code Online (Sandbox Code Playgroud)

android android-fragments kotlin android-jetpack-compose

11
推荐指数
0
解决办法
602
查看次数

Espresso:如何使用结果RESULT_OK测试活动结束

在我的应用程序中,当用户单击"注册"按钮时,将启动RegisterActivity.一旦用户填写表单,详细信息将发布到Web服务,如果注册成功,则RegisterActivity会使用RESULT_OK.这在以下代码示例中进行了总结:

public void submitRegistration() {

    showProgressDialog(R.string.registration, R.string.please_wait);  

    getWebApi().register(buildRegistrationFromUI(), new Callback<ApiResponse>() {
        @Override
        public void success(ApiResponse apiResponse, Response response) {

            hideProgressDialog();

            setResult(RESULT_OK);
            finish();
        }

        @Override
        public void failure(RetrofitError error) {

            hideProgressDialog();
            showErrorDialog(ApiError.parse(error));
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

使用Espresso,如何检查活动是否已完成setResult(RESULT_OK).

请注意:我希望创建一个模拟的意图,我想查的意图结果状态.

android android-intent android-espresso

7
推荐指数
2
解决办法
3002
查看次数

使用Samsuns Galaxy S3单击全屏视图时,Espresso会抛出错误

我尝试用浓咖啡点击全屏视图,如下所示:

onView(withId(R.id.id)).perform(click());
Run Code Online (Sandbox Code Playgroud)

但得到这个错误:

Error performing 'Send down montion event' on view 'unknown'
Run Code Online (Sandbox Code Playgroud)

...

Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
Run Code Online (Sandbox Code Playgroud)

当我在其他设备(如Nexus 6和S4)上运行测试时,测试运行正常,但在三星Galaxy S3上它会抛出此异常.

testing android automated-tests android-espresso

5
推荐指数
1
解决办法
783
查看次数