Take photo using camera

Patrol provides functionality to take a photo using the Android and iOS camera.

Due to many differences between devices, this method will not work on 100% of devices but should work on most of them. If the device that you are testing on is not working with this command, you can provide your own selectors. To get native selectors, you can use Patrol DevTools Extension.

How it works

This method does two actions:

  1. Tap on shutter button
  2. Tap on confirm button

The table below shows the native selectors that the takeCameraPhoto() method uses internally for each platform:

PlatformShutter ButtonConfirm Button
Physical Androidcom.google.android.GoogleCamera:id/shutter_buttoncom.google.android.GoogleCamera:id/done_button
Emulator Androidcom.android.camera2:id/shutter_buttoncom.android.camera2:id/done_button
Simulator and Physical iOSPhotoCaptureDone

This method should work without custom selectors on iOS devices. For Android, it should work with most emulators and Pixel physical devices.

Examples

When you are using supported devices, you can use this method without any additional arguments:

import 'package:example/main.dart';
import 'package:patrol/patrol.dart';

void main() {
  patrolTest(
    'Take a photo using android or iOS camera',
    ($) async {
      await $.pumpWidgetAndSettle(const MyApp());
      await $.tap(#addPhotoButton); // Clicks a photo button inside your app to open camera
      await $.native2.grantPermissionWhenInUse(); // Some devices require permission to be granted before taking a photo
      await $.native2.takeCameraPhoto(); // Takes a photo using the camera
    },
  );
}

When you are using unsupported devices, you can provide your own selectors:

import 'package:example/main.dart';
import 'package:patrol/patrol.dart';

void main() {
  patrolTest(
    'Take a photo using android or iOS camera with custom selectors',
    ($) async {
      await $.pumpWidgetAndSettle(const MyApp());
      await $.tap(#addPhotoButton); // Clicks a photo button inside your app to open camera
      await $.native2.grantPermissionWhenInUse(); // Some devices require permission to be granted before taking a photo
      await $.native2.takeCameraPhoto(shutterButtonSelector: NativeSelector(
        android: AndroidSelector(
          resourceName: 'com.oplus.camera:id/shutter_button',
        ),
        ios: IOSSelector(label: 'Take Picture'),
      ),
      doneButtonSelector: NativeSelector(
        android: AndroidSelector(
          resourceName: 'com.oplus.camera:id/done_button',
        ),
        ios: IOSSelector(label: 'Done'),
      ),
      );
    },
  );
}