Integrate BrowserStack with flutter integration Android test

Vinay Sharma
4 min readMar 13, 2024

--

In this article, we’ll explore how to run Flutter integration tests for Android on BrowserStack, a cloud-based testing platform. Before we begin, make sure you have the following prerequisites in place:

Flutter integration test
  1. Flutter installed on your machine. You can follow the installation guide here.
  2. Ensure all required libraries are installed by running flutter doctor in your terminal. This command should list all necessary libraries with proper checkmarks.

Additionally, you’ll need:

  • A working Flutter project. If you don’t have one, you can clone a sample project.
  • A BrowserStack account. You can sign up for a free account here.

With these prerequisites met, let’s dive into the steps to integrate BrowserStack with Flutter for Android integration testing.

Step 1:
Dependencies Setup
: Update your android/app/build.gradle file to ensure it uses AndroidX's version of AndroidJUnitRunner and includes AndroidX libraries as dependencies.

android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Step 2:
Create a new file named MainActivityTest.java under android/app/src/androidTest/java/com/example/myapp/. Customize the package structure (com.example.myapp) to match your app's actual package name.

These steps will help you prepare your Flutter project and set up BrowserStack for running integration tests on Android devices

package com.example.myApp;
import androidx.test.rule.ActivityTestRule;
import dev.flutter.plugins.integration_test.FlutterTestRunner;
import org.junit.Rule;
import org.junit.runner.RunWith;
import com.example.myApp.MainActivity;
@RunWith(FlutterTestRunner.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false);
}java

Step 3:
Build Instrumentation test .apk file :
Use the following Gradle commands to build an instrumentation test.apk file(test suite) using the MainActivityTest.java created in the androidTest directory as mentioned in step 1.

# Go to the android folder which contains the "gradlew" script used for building Android apps from the terminal
pushd android
# Build an Android test APK (uses the MainActivityTest.java file created in step 1)
./gradlew app:assembleAndroidTest
# Build a debug APK by passing the integration test file
./gradlew app:assembleDebug -Ptarget=`pwd`/integration_test/app_test.dart"
# Go back to the root of the project
popd
```

Step 4:
Executing Tests Locally
: After integrating the necessary dependencies as per the documentation, you can locally run the tests using Gradle to validate the Android setup.
Please remember: Navigate to your app’s android directory to run Gradle commands. Use cd android to move to the android directory, then execute the following command:

flutter build apk --debug
./gradlew app:assembleDebugAndroidTest
./gradlew app:connectedDebugAndroidTest -Ptarget=integration_test/tests/<testcase_name>.dart

If your setup is successful, the test cases will begin running on your local device.

Ready to Rock: Your Setup is Set, Let’s Test on BrowserStack!

Step 4:
Upload Your Apps to BrowserStack:
As per Step 3, we’ve generated two APKs — the Android test APK and the Android debug APK. Now, let’s upload both these APKs to BrowserStack to obtain the corresponding urls. Execute the following curl command to upload the Android test APK:”

curl -u "bs_accessKey:bs_pass" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/app" \
-F "file=@/path/to/app/file/Application-debug.apk"

Depending on your internet speed, the upload process may take some time. Once completed, you will receive a unique urls, for example: "app_url":"bs://j3c874f21852ea50957a3fdc33f47514288c4ba4",

Follow the same process to upload the Android debug APK and extract its URL as well.”

curl -u "bs_accessKey:bs_pass" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/test-suite" \
-F "file=@/path/to/app/file/Application-debug-test.apk"

Extracted url: "test_suite_url":"bs://f7c874f21852ba57957a3fde31f47514288c4ba4"

Step 5:
Run Android Flutter integration tests: To complete the process, use another curl command to trigger a test. This command will require both the URLs obtained earlier as parameters, along with any other customized parameters according to your requirements. This will initiate the Android Flutter integration tests

curl -u "bs_accessKey:bs_pass" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/build" \
-d '{"app": "bs://j3c874f21852ea50957a3fdc33f47514288c4ba4", "testSuite": "bs://f7c874f21852ba57957a3fde31f47514288c4ba4", "devices": ["Samsung Galaxy S9 Plus-9.0"]}' \
-H "Content-Type: application/json"

A sample response:

{
"message" : "Success",
"build_id" : "4d2b4deb810af077d5aed98f479bfdd2e64f36c3"
}

message — success means you have successfully triggered the flutter automation test on Browserstack and if you want to view the test result head to the flutter dashboard on https://app-automate.browserstack.com/dashboard/v2/builds/<build_id>

Just to simplify, you can add all the above steps in an single runner.sh

Step 6:
Runner.sh:

flutter build apk --debug 

pushd android
./gradlew app:assembleDebugAndroidTest
./gradlew app:assembleDebug -Ptarget=`pwd`/../integration_test/tests/<testCase_name>.dart
popd

apk_path=$(pwd)/build/app/outputs/apk/debug/app-debug.apk
test_apk_path=$(pwd)/build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk

# 1. Upload app
echo "Uploading test app to BrowserStack"
app_upload=$(curl -u "$BROWSERSTACK_USER:$BROWSERSTACK_ACCESS_KEY" -X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/app" \
-F "file=@$apk_path")
app_url=$(jq -r .app_url <<< "$app_upload")
echo -e "\napp_url: $app_url\n"

# 2. Upload test
echo "Upload debug app to BrowserStack"
test_upload=$(curl -u "$BROWSERSTACK_USER:$BROWSERSTACK_ACCESS_KEY" -X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/test-suite" \
-F "file=@$test_apk_path")
test_url=$(jq -r .test_suite_url <<< "$test_upload")
echo -e "\ntest_url: $test_url\n"

# 3. Trigger test
trigger_tests=$(curl -u "$BROWSERSTACK_USER:$BROWSERSTACK_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/flutter-integration-tests/v2/android/build" \
-d "{\"buildName\": \"$build_name\", \"buildTag\": \"$build_tag\", \"deviceLogs\": $device_logs, \"networkLogs\": $network_logs,\"project\": \"$project\", ,\"autoGrantPermissions\":true, \"devices\": $devices, \"app\": \"$app_url\", \"testSuite\": \"$test_url\"}" \
-H "Content-Type: application/json")
build_id=$(jq -r .build_id <<< "$trigger_tests")
echo -e "\ntrigger_tests: $trigger_tests\n"
echo -e "\nbuild_id: $build_id\n"

test_result_url="https://app-automate.browserstack.com/dashboard/v2/builds/$build_id"
echo -e "\nTest Execution is started and can be found on this link\n - $test_result_url"
exit $exit_code

After executing the runner.sh script, it will handle everything from generating the necessary APKs to uploading the tests to Browserstack, extracting the required information, triggering the tests, and finally displaying the results on the dashboard. Everything is set up for you to seamlessly run tests on Browserstack for Android.

Perfect! You are all good to run tests on the browserstack for Android.

--

--