Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow permission dialog in Flutter end-to-end testing?

In Flutter end-to-end testing, a permission is to be allowed during the test. driver.tap(find.text("ALLOW") does not work. How to click "ALLOW".

enter image description here

like image 373
Kyaw Tun Avatar asked Dec 05 '18 21:12

Kyaw Tun


3 Answers

Other answers will not help you if you are using the latest Flutter Integration Testing as there we don't require FlutterDriver to connect.

So now, you need to add the below code in test_driver.dart file where we initialize integrationDriver()

Future<void> main() async {
    final Map<String, String> envVars = Platform.environment;
    String? adbPath = join(envVars['ANDROID_SDK_ROOT'] ?? envVars['ANDROID_HOME']!,
      'platform-tools',
      Platform.isWindows ? 'adb.exe' : 'adb',
    );
  await Process.run(adbPath , ['shell' ,'pm', 'grant', 'com.example', 'android.permission.CAMERA']);
  await Process.run(adbPath , ['shell' ,'pm', 'grant', 'com.example', 'android.permission.WRITE_EXTERNAL_STORAGE']);
  await integrationDriver();
}

This change is required as FlutterDriver makes the connection once you initialized and connect it, but the new flow of integration testing already had a connection, so we need to initialize it before the connection happens.

To run code, use the below command on terminal, as you see for better result I have created two folder,

  1. test_driver: Contains the driver code mentioned above.

  2. integration_test: Contains test files.

    flutter drive --driver=test_driver/test_driver.dart --target=integration_test/my_test.dart
    
like image 139
Jitesh Mohite Avatar answered Oct 29 '22 23:10

Jitesh Mohite


You can grant the permissions before running the test.

import 'dart:io';
import 'package:path/path.dart';

// ...

setUpAll(() async {
  final envVars = Platform.environment;
  final adbPath = join(
    envVars['ANDROID_SDK_ROOT'] ?? envVars['ANDROID_HOME'],
    'platform-tools',
    Platform.isWindows ? 'adb.exe' : 'adb',
  );
  await Process.run(adbPath, [
    'shell',
    'pm',
    'grant',
    'com.example.yourapp', // replace with your app id
    'android.permission.RECORD_AUDIO'
  ]);
  driver = await FlutterDriver.connect();
});
like image 17
apaatsio Avatar answered Oct 29 '22 22:10

apaatsio


for me above code is not working don't know why

then i'm tried with below code and its working

setUpAll(() async {

      await Process.run('add_adb_path/adb.exe' , ['shell' ,'pm', 'grant', 'add_app_package_name', 'android.permission.ACCESS_MEDIA_LOCATION']);
      await Process.run('add_adb_path/adb.exe' , ['shell' ,'pm', 'grant', 'add_app_package_name', 'android.permission.READ_EXTERNAL_STORAGE']);
      await Process.run('add_adb_path/adb.exe' , ['shell' ,'pm', 'grant', 'add_app_package_name', 'android.permission.WRITE_EXTERNAL_STORAGE']);


      driver = await FlutterDriver.connect();
    });
like image 2
Susovan Das Avatar answered Oct 29 '22 23:10

Susovan Das