← Back to list

How to Automate Flutter Mobile Application with Java?

In this Blog we will cover

Farhan Sharif · 2024-06-23 14:27 · 1 claps · 7.6 min read
#flutter-automation #appium-flutter-driver #appium #flutter
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

How to Automate Flutter Mobile Application with Java?

In this Blog we will cover

  1. What is Flutter?
  2. Native vs Flutter App Difference
  3. Step-by-Step Guide to Automate Flutter Apps
  4. Integrating CI/CD with Bitbucket and Running Tests on Sauce Labs
  5. Sauce Labs:
  6. Conclusion
  7. References

What is Flutter?

Flutter is an open-source UI software development kit created by Google. It can be used to develop cross platform applications from a single codebase for the web, Fuchsia, Android, iOS, Linux, macOS, and Windows. Automating Flutter applications can be challenging due to the custom rendering engine it uses. However, with the Appium Flutter Driver, this process becomes more manageable. This blog will guide you through automating a Flutter mobile application using Appium Flutter Driver and highlight the differences between automating Flutter apps and native apps.

Native vs Flutter App Difference

The following is a summarised list that shows the difference between testing the Native and Flutter applications:

Native Application

Programming Language

Java, Kotlin, Swift and Objective-C

Appium UiAutomator2 driver, Appium xcuitest driver used for Android, IOS app automation

Appium Inspector is used for inspecting Native mobile apps.

Element Locator:

For Android we can use

  1. Accessibility ID
  2. ClassName
  3. ID
  4. UISelector
  5. XPath(not recommended)

For iOS we can use

  1. Test ID
  2. iOS Class Chain
  3. iOS Predicate String
  4. XPath(not recommended)

Access to Code base Not required to Automate Native apps.

Flutter Application

Dart Programming Language is used to develop Flutter apps

Appium Flutter driver used for Android and IOS Flutter app automation

Flutter Inspector for Inspecting elements

Locator Strategies

Using Appium Flutter Finder, following can be used:

  1. byValueKey(String key)
  2. byValueKey(int key)
  3. byToolTip(String toolTipText)
  4. byType(String type)
  5. byText(String input)
  6. byAncestor(FlutterElement of, FlutterElement matching, boolean matchRoot, boolean firstMatchOnly)
  7. byDescendant(FlutterElement of, FlutterElement matching, boolean matchRoot, boolean firstMatchOnly)
  8. bySemanticsLabel(String label)
  9. bySemanticsLabel(Pattern label)

Access to Code base required for using Flutter Inspector

Step-by-Step Guide to Automate Flutter Apps

  • Java

Install Java and the path of the Java should be set in the Environment variable.

  • Install Node.js:

https://nodejs.org/en

  • Maven

Download and setup the Maven.

  • Appium:

Install Appium 2.0.

https://appium.io/docs/en/2.0/quickstart/install/

  • Download and Install Flutter SDK

https://docs.flutter.dev/get-started/install

Once the flutter SDK is downloaded, add the Flutter SDK to the environment variable path to use it seamlessly from the command line.

  • Install the Appium Flutter Driver

npm install appium-flutter-driver

  • Install Android Studio

Install Android Studio for Android emulators or Xcode for iOS simulators.

  • Setup Flutter Project

Open the flutter project in the Android Studio

  • Update the dev dependencies in the pubspec.yaml file

dev_dependencies:

test: any

flutter_test:

sdk: flutter

flutter_driver:

sdk: flutter

  • Import the dev dependencies

After updating the pubspec.yml file, Open the terminal and run the following command to import the dev dependencies.

flutter pub get

All the dev_dependencies should be imported successfully

  • Update the main.dart file

Add the following statement in main.dart file for importing the flutter_driver_extension

import ‘package:flutter_driver/driver_extension.dart’;

Add the enableFlutterDriverExtension() before the runApp statement in the main.dart file.

void main() {

enableFlutterDriverExtension();

runApp(MyApp()); }

Once the above code is added in the main.dart file, it should look as shown in the screenshot below:

  • Launch the App in Emulator

Click on the Play icon and run the Emulator and launch the app in the selected Emulator.

  • Inspect the Element

Once the application is launched, use Flutter Inspector to inspect elements and get their properties. This tool is available in Android Studio.

It will open the Flutter Inspector window as shown in the screenshot below

Now, to locate the mobile elements, we need to follow the below steps:

  1. Click on the Select Widget Mode button in Flutter Inspector.
  2. Navigate to the Android Emulator and select the field for which the mobile element needs to be located.

For the Next button, the mobile element located is Text and its value is “Next” so we will locate this element with text

WebElement nextbutton = this.finder.byText(“Next”);

nextbutton.click();

To locate an element with key

WebElement button = this.finder.byKey(“timeslot_0”);

  • Generate Mobile builds

As we will be using Appium Flutter Driver to locate mobile elements and write automation tests, we need to compile and generate the Flutter build in debug or profile mode as Appium Flutter Driver does not support the apps in release mode.

Open the Terminal and run the command given below to generate the Android and IOS build in debug mode –

flutter build apk –debug

flutter build ios –debug

  • Implementation of Test Scenarios

Tech Stack used:

Programming Language — Java 17

Mobile app development tool — Android Studio

Flutter App — AskEddy

Tool for inspecting mobile elements — Flutter Inspector

Driver to run mobile automation tests — Appium Flutter Driver

Mobile automation framework — Appium 2.0

Mobile automation tool to locate Flutter Elements — Appium Flutter Finder — Java

Build Tool — Maven

Test Runner — TestNG

IDE- Intellij

Pattern- POM pattern

Database connection- mysql

Report- Extent Report

  • Created a Maven project with this structure

  • Add the dependencies in the POM.XML

  • before Suite Method Explanation

Date Formatting:

  • A SimpleDateFormat object is created to format the current date in the “yyyy-MM-dd” format.
  • The current date is obtained and formatted as a string using the formatter object.

HTML Reporter Setup:

  • An ExtentHtmlReporter object is instantiated with a file path that includes the formatted date, ensuring each report file is uniquely named based on the date.
  • The HTML report’s document title, report name, and theme are configured.

ExtentReports Initialization:

  • An ExtentReports object is created and the previously configured ExtentHtmlReporter is attached to it.
  • System information like the environment (“QA”) and user name (“Farhan”) is added to the report for reference.

Driver Manager Initialization:

  • A RemoteDriverManager object is instantiated and used to start the Appium service, which is likely needed for mobile application testing.

Database Connection:

  • An attempt is made to connect to a database using the connectDb method.
  • If the connection is successful, the isDbConnected flag is set to true, and a database query is executed using the executeQuery method.
  • If an exception occurs during the database connection, the stack trace is printed, an error message is logged, and the isDbConnected flag is set to false.

  • Defined the capabilities and create the drivers in the RemoteDriverManager class

The BaseTest class has the setup() method that will run before any test is invoked. The setup() method will instantiate the RemoteDriverManager class. Since it runs on both Android and IOS, the platform name is passed as a parameter.

tearDownMethod has been called after the Method, tearDownSuite has been called after the Suite and closeDatabaseConnection has been called to close the database connection.

  • HelperClass

The HelperClass provides utility methods for generating random strings, random emails, and random numbers, as well as taking screenshots in case any test cases fail. This class enhances the flexibility and robustness of automated tests by supplying essential auxiliary functions.

  • Config Reader

LoginTest File:

LoginPage:

  • Test Execution

Run the test using testing.xml file

This TestNG XML configuration file defines a suite named “Ask Eddy flutter tests” that includes two separate test groups: one for Android and one for iOS. Each test group specifies the platform parameter and the test classes to be executed. The “AskEddy Android test” group runs the LoginTest class, while the “AskEddy IOS test” group runs the HomeScreenTest class.

Integrating CI/CD with Bitbucket and Running Tests on Sauce Labs

Integrating CI/CD with Bitbucket, combined with running tests on Sauce Labs, enhances your development workflow by automating the build, test, and deployment processes.Integrating Sauce Labs with automation framework ensures that you can run your tests on a variety of devices and operating systems, providing comprehensive test coverage. Here’s how you can set up and run your Flutter app automation tests on Sauce Labs:

Sauce Labs:

Step 1: Create an Account on Sauce Labs

Visit https://saucelabs.com/ and create an account

Step 2: Upload Your App to Sauce Labs

Upload your Android or IOS App.

Step 3: Update Desired Capabilities to Run Tests on Sauce Labs

Step 4: Execute Your Tests

Step 5: Monitor Tests in Sauce Labs:

Setting Up CI/CD with Bitbucket

Step 1: Create a Bitbucket Repository

https://bitbucket.org/

Step 2: Configure Pipeline

Step 3: Run the Pipeline

Conclusion

Automating Flutter applications using Appium Flutter Driver with Java, Maven, and TestNG can significantly improve the efficiency and coverage of your testing process. By following the steps outlined in this guide, you can set up and execute automated tests for your Flutter apps, ensuring they are reliable and perform well across different devices. By integrating Sauce Labs with your Flutter app automation, you gain access to a wide range of devices and operating systems, ensuring comprehensive test coverage and enhanced reliability. Additionally, by integrating CI/CD with Bitbucket and running tests on Sauce Labs, you can streamline your development workflow, automate test execution, and quickly identify and resolve issues, leading to a more robust and efficient development cycle.

References

https://github.com/appium/appium-flutter-driver

https://docs.flutter.dev/testing/integration-tests

https://docs.saucelabs.com/mobile-apps/automated-testing/appium/appium-flutter/#configuring-your-appium-capabilities


메타데이터
post_id
906f508b60a5
slug
how-to-automate-flutter-mobile-application-with-java-906f508b60a5
url
https://medium.com/@farhan.malik/how-to-automate-flutter-mobile-application-with-java-906f508b60a5
canonical_url
https://medium.com/@farhan.malik/how-to-automate-flutter-mobile-application-with-java-906f508b60a5
author_url
https://medium.com/@farhan.malik
status
ok
fetched_at
2026-08-24 10:15:10