Espresso:如何点击手机摄像头上的图像点击按钮

问题描述 投票:1回答:2

我正在使用espresso编写测试,我的应用程序打算手机摄像头,我手动按下单击按钮,然后它迁移到下一个屏幕,我无法自动化测试代码中的图像点击按钮,我怎么能访问相机使用代码我可以做同样的事情。谢谢。

android android-espresso
2个回答
1
投票

您不应该打开相机意图,否则您将无法从中获取任何结果图像(无需手动按相机按钮)。

看看本网站的Stubbing out the Camera部分:https://guides.codepath.com/android/UI-Testing-with-Espresso#stubbing-out-the-camera

这样,您可以通过模拟从相机“返回”到应用程序的实际图像来测试您的活动。

更新

这是我用来测试位图的方法:

public static Bitmap getTestBitmap(Context context, String resourceName) {
    Resources resources = context.getResources();
    Bitmap ret = null;
    int imageResource = resources.getIdentifier(
            resourceName, "drawable", context.getPackageName());

    Uri pictureUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
            + resources.getResourcePackageName(imageResource) + '/'
            + resources.getResourceTypeName(imageResource) + '/'
            + resources.getResourceEntryName(imageResource));
    try {
        ret = MediaStore.Images.Media.getBitmap(context.getContentResolver(), pictureUri);
    } catch (Exception e) {
    }
    return ret;
}

然后我将位图保存在内部存储中并获取uri:

public static Uri saveToInternalStorage(Context context, Bitmap bitmapImage, String fileName) {
    ContextWrapper cw = new ContextWrapper(context);
    // path to /data/data/yourapp/app_data/pictures
    File directory = cw.getDir("pictures", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, fileName);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (Exception e) {
        }
    }


    return Uri.fromFile(new File(mypath.getAbsolutePath()));
}

0
投票

我知道这已经很晚了,但这是我自己挣扎的事情,我想发一个答案来帮助别人。以下是如何从选择器中单击相机按钮(在设置之后),您使用UIAutomator,如PunitD在原始帖子的评论中所建议的那样。这将从测试在屏幕上显示选择器的位置开始。

public static final int waitTimeNativeApi = 6000;

public static void await(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            Log.e(TAG, "Interrupted while sleeping");
        }
    }

private void takePhoto() {

    boolean usePixels = true;

    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject titleTextUI = device.findObject(new UiSelector()
            .className("android.widget.TextView")
            .text("Camera")
    );

    try {
        titleTextUI.clickTopLeft();

        if (usePixels) {
            takePhotoForPixels(device);
        } else {
            takePhotoForSamsung(device);
        }

    } catch (UiObjectNotFoundException unofe) {
        unofe.printStackTrace();
    }


}

private void takePhotoForPixels(UiDevice device) {

    // close the app selector to go back to our app so we can carry on with Espresso

    await(waitTimeNativeApi);

    //TAP on the camera icon
    device.click(device.getDisplayWidth() / 2, device.getDisplayHeight() - 100);

    await(waitTimeNativeApi);

    //Get the OK button
    device.click(device.getDisplayWidth() / 2, device.getDisplayHeight() - 100);

    await(waitTimeNativeApi);

}

private void takePhotoForSamsung(UiDevice device) throws UiObjectNotFoundException {

    // close the app selector to go back to our app so we can carry on with Espresso
    UiObject titleTextUI = device.findObject(new UiSelector()
            .className("android.widget.TextView")
            .text("Camera")
    );

    titleTextUI.clickTopLeft();

    await(waitTimeNativeApi);

    //TAP on the camera icon
    device.click(device.getDisplayWidth() / 2, device.getDisplayHeight() - 50);

    //Get the OK button
    UiObject cameraOkUi = device.findObject(new UiSelector()
            .className("android.widget.TextView")
            .text("OK")
    );

    cameraOkUi.click();

    await(waitTimeNativeApi);

}

通过这种方式,您将拍摄实际照片并将结果返回onActivityResult。

© www.soinside.com 2019 - 2024. All rights reserved.