Set up Flutter for Android apps

When testing your Flutter application with Perfecto, the following assumptions apply:

  • You build your own application and should have two .apk files: one for the real application and one for the test application.

  • Running the Perfecto Gradle Plugin is best done outside of the source code folder. The application can reside in the cloud, in Git/NAS, and so on.

  • Running the Perfecto Gradle Plugin requires Gradle.

  • Running Gradle requires a gradle wrapper along with the following two files:

    • build.gradle (to allow running the plugin, not to build the application)

    • A config file in JSON format

  • You reference the build.gradle file and the JSON config file through CLI params. It is good practice to keep both files in the folder from where you run the gradle wrapper.

Watch this video to see a demo of the Perfecto-Flutter integration setup for Android. Detailed steps follow.

Important: This document includes references to a third-party product, <product name>. The user interface and usage of third-party products are subject to change without notice. For the latest published information about <product name>, see <link to third-party product documentation>.

Prerequisites

Before you get started, make sure you have installed the following:

  • Gradle
  • Java
  • Flutter
  • Gradle plugin

    You can download the Gradle Plugin either automatically, after adding the required lines of code to the build.gradle file, as described in step 2 below, or, if your organization does not permit direct download, by pre-downloading it to a local libs folder (see also Install the Perfecto Gradle plugin manually).

1 | Get started

The starting point is the following Flutter project: https://github.com/PerfectoMobileSA/PerfectoFlutterSample

We will use the following tests for integration testing: https://github.com/PerfectoMobileSA/PerfectoFlutterSample/tree/main/testing_app/integration_test

Flutter projects are integrated with Gradle and include several build.gradle files – one at the project level and one for each application. 

The following steps explain how to get started with Flutter using Android Studio, but you can use any other preferred IDE that supports Flutter. For the latest published information about Android Studio, see https://developer.android.com/studio/intro.

To get started: 

  1. Clone the project: https://github.com/PerfectoMobileSA/PerfectoFlutterSample

  2. (Optional) If you want to browse the project, open it in Android Studio or any other IDE of your choice that supports Flutter.

  3. Open a terminal window in the root folder testing_app.

  4. Execute the following command:

    Copy
    flutter test integration_test/app_test.dart

    This will run the app_test.dart file and let you see the results on the screen.

    Important: If no Android device is connected to your Mac, you are prompted to choose between Chrome and MacOS. Choosing Chrome will return an error. Choosing MacOS will trigger the test on Safari.

    If you do not specify the particular .dart file, Flutter will run all tests in the integration_test folder.

    Tip: For better log data generated on the application under test (AUT), consider adding the following empty methods in each test file in the group definition. These methods can be empty or include log statements.
    Copy
    setUpAll(() {
    });
    tearDownAll(() {
    });

2 | Configure the project for Perfecto

In this step, we work with build configuration scripts called build.gradle. Flutter projects are integrated with Gradle and include several build.gradle files – one at the project level (in this case, testing_app/android/build.gradle) and one for each application (in this case, testing_app/android/app/build.gradle).

To configure the project for Perfecto:

  1. (Optional) Open your app-level build.gradle file (in our example, this is testing_app/android/app/build.gradle) and make sure the following line is included in the defaultConfig section:

    Copy
     testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  2. Save the file.

  3. (Optional) Open your main test file (in our example, this is https://github.com/PerfectoMobileSA/PerfectoFlutterSample/blob/main/testing_app/android/app/src/androidTest/java/dev/flutter/testing_app/MainActivityTest.java) and make sure that the following line is included:

    Copy
    import dev.flutter.plugins.integration_test.FlutterTestRunner;

    If the following line appears instead, make sure to replace it with the expected line above:

    Copy
    import dev.flutter.plugins.e2e.FlutterTestRunner;
  4. Open a terminal window, navigate to the android folder, and run the following command:

    Copy
    gradle wrapper
  5. To build the required APKs, run the following commands.

    For information on adding the Gradle Wrapper, see the Gradle documentation.

  6. View the resulting APKS in the following folder: build/app/outputs/apk

3 | Run the plugin and view the report

In this step, you create a Flutter configuration file, run the Perfecto Gradle plugin, and view the test report in Perfecto.

The JSON file holds all Perfecto configurations, including security information, the Perfecto cloud name, Smart Reporting information, and test data.

Expand a step to view its content.

Proxy connection

If you run the Gradle plugin over a proxy connection, you can supply the proxy information as follows:

  • As Java parameters when activating the plugin. You can use the same Java Proxy parameters also for proxies supporting SSL encryption.

    http.proxyHost: The IP address of the proxy

    http.proxyPort: The IP port used for the connection

    http.proxyUser: The username for connecting to the proxy server

    http.proxyPassword: The password for connecting to the proxy server

    For example:

    Copy
    ./gradlew perfecto-android-inst -DconfigFileLocation=configFile.json -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword
  • As parameters defined explicitly in the gradle.properties file. For example:

    Copy
    systemProp.http.proxyHost=<http-proxy-host>
    systemProp.http.proxyPort=<http-proxy-port>
    systemProp.https.proxyHost=<https-proxy-host>
    systemProp.https.proxyPort=<https-proxy-port>