How to grant permission in flutter integration tests?

63 Views Asked by At

I have setup integration tests for firebase using guide. I am running the same tests at local using following command:

  flutter build apk --debug --flavor dev
  ./gradlew app:assembleDevDebugAndroidTest
  ./gradlew app:connectedDevDebugAndroidTest -Ptarget=integration_test/main_test.dart

I'm able to run tests successfully but the apk asks for permissions if it's new device or reinstalled the app.

I added following code to flutter_app/android/app/src/androidTest/java/com/domain/myapp/MainActivityTest.java to implement GrantPermissionRule as mentioned still it didn't work.

@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);

    @Rule
    public GrantPermissionRule mRuntimePermissionRule =
        GrantPermissionRule.grant(android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_MEDIA_VIDEO,
            android.Manifest.permission.READ_MEDIA_IMAGES,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.ACCESS_FINE_LOCATION
    );
}

Permissions Is there a way to grant permissions in Flutter Instrumentation tests?

1

There are 1 best solutions below

1
Amit Kumar On

In driver folder test_driver/integration_test: Below example is to accept permission for location and similarly you can add other permissions also using adb for android

import 'dart:io';
import 'package:integration_test/integration_test_driver.dart';
                
        future<void> main() async {
        final adbPath = path.join(
              androidHomePath,
              'platform-tools',
              Platform.isWindows ? 'adb.exe' : 'adb',
            );
              await process.run(
               'adbPath',
               [
                 'shell',
                 'pm',
                 'grant',
                 'com.example.<your app name>', // todo: update this
                 'android.permission.access_coarse_location'
               ],
             );
         await process.run(
               'adbPath',
               [
                 'shell',
                 'pm',
                 'grant',
                 'com.example.<your app name>', // todo: update this
                 'android.permission.READ_CONTACTS'
               ],
             );
             // todo: add more permissions as required
             await integrationdriver();
            }