screencap method

Future<Uint8List?> screencap()

Executes a screen capture for the application. As a note, depending on the device this may take several seconds. The screenshot call is fully async so this will wait until up to TestStepDelays.screenshot for the response.

This will never trigger a failure, but it will return null if the device does not respond before the timeout.

Implementation

Future<Uint8List?> screencap() async {
  Uint8List? image;

  if (!kIsWeb) {
    final captureContext = CaptureContext(
      image: [],
    );
    status = '<screenshot>';
    try {
      _screencapController.add(captureContext);

      final now = DateTime.now().millisecondsSinceEpoch;
      while (captureContext.image.isNotEmpty != true &&
          now + delays.screenshot.inMilliseconds >
              DateTime.now().millisecondsSinceEpoch) {
        await Future.delayed(const Duration(milliseconds: 100));
      }
    } catch (e, stack) {
      _logger.severe(e, stack);
    }

    image = captureContext.image.isNotEmpty != true
        ? null
        : Uint8List.fromList(captureContext.image);
  }

  return image;
}