Features

Appium Testing for Flutter Apps

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

Flutter is an open-source UI development framework, created by Google, to do cross-platform app development on iOS, Android, Linux, macOS and Windows.
The idea is that you write code once, then compile it for different platforms, similar to the React Native framework by Facebook.


Flutter offers nice features for developers:

  • Extensive documentation to get started.
  • Native Performance: Flutter code is compiled to native code for iOS/Android and other platforms.
  • Hot reload to instantly see code changes.

Because of the increasing popularity in Flutter, this article will give an overview on how to run automated mobile app tests with Flutter apps and Appium.

What is Flutter Framework?

Flutter is a UI development framework, open-sourced by Google. It allows developers to create mobile apps on multiple platforms (including iOS and Android), with a single codebase.


The advantage of using a framework such as Flutter is that you no longer need to learn multiple programming languages for multiple platforms. You no longer need to learn Objective-C or Swift for iOS, or Java/Kotlin for Android.


Maintaining a single code base for app development means developers will be able to switch more easily between platforms and save time fixing bugs and implementing features.


What programming language does Flutter use?

Flutter apps are written in the Dart programming language. The app runs in the Dart virtual machine, which supports hot reloading. The apps are compiled directly to machine code, either x86 or ARM, or in JavaScript if compiled for the web.

At the core of Flutter is the Flutter engine, written in C++, which is responsible for UI, networking, compiling and more.


The Flutter framework is the component that is used by the developers. Flutter uses a reactive UI framework, similar to Facebook's React framework.

How to create a Flutter app?

Flutter is easy to install and configure. Simply follow the getting started documentation in the official Flutter documentation.


You'll need to download and install the Flutter SDK and XCode or Android Studio, depending on which targets you want to provide.
You can use the flutter doctor command to see if you have everything set up correctly.


Once you've configured everything, you can start making a layout in Flutter, widgets, navigation and routing, and other UI elements. When your app is ready, you can build and release the iOS app or Android app.

How to use the Appium Flutter Driver?

The Appium Flutter Driver allows you to run test automation against Flutter apps.
Before you install the Appium Flutter Driver, make sure you have a working setup of Appium.
You'll also need to compile your Flutter app in either debug or profile mode and use the enableFlutterDriverExtension before runApp.


To get started, please make sure to follow these steps:

  1. Make sure your app's pubspec.yaml file contains:

    dev_dependencies:
    
    test: any
    
    flutter_test:
    
    sdk: flutter
    
    flutter_driver:
    
    sdk: flutter
  2. Your main.dart file should contain enableFlutterDriverExtension() before runApp.

    void main() {
    	enableFlutterDriverExtension();
    	init();
    	runApp(MyApp());
    }
  3. The automationName in your desired capabilities should be set to Flutter.

    caps = Selenium::WebDriver::Remote::Capabilities.new
    caps["automationName"] = "Flutter"
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("automationName", "Flutter");
    $caps = array(
    "automationName" => "Flutter"
    );
    capabilities = {
    "automationName" : "Flutter"
    }
    const capabilities = {
    "automationName" : "Flutter"
    }
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.SetCapability("automationName", "Flutter");

Below is a simple example on how to run a Flutter Automation test on a TestingBot device.

const wdio = require('webdriverio');
const assert = require('assert');
const { byValueKey } = require('appium-flutter-finder');

const caps = {
  platformName: 'Android',
  deviceName: 'Samsung Galaxy S21',
  app: 'tb://[yourapp]',
};

const opts = {
  capabilities: {
    ...caps,
    automationName: 'Flutter',
    retryBackoffTime: 500,
    hostname: 'hub.testingbot.com',
    user: '...',
    key: '...'
  }
};

(async () => {
  const counterTextFinder = byValueKey('counter');
  const buttonFinder = byValueKey('increment');

  const driver = await wdio.remote(opts);

  if (process.env.APPIUM_OS === 'android') {
    await driver.switchContext('NATIVE_APP');
    await (await driver.$('~fab')).click();
    await driver.switchContext('FLUTTER');
  } else {
    console.log('Switching context to `NATIVE_APP` is currently only applicable to Android demo app.')
  }

  assert.strictEqual(await driver.getElementText(counterTextFinder), '0');

  await driver.elementClick(buttonFinder);
  await driver.touchAction({
    action: 'tap',
    element: { elementId: buttonFinder }
  });

  assert.strictEqual(await driver.getElementText(counterTextFinder), '2');

  driver.deleteSession();
})();

Conclusion


The Appium Flutter driver allows developers and testers to easily test mobile Flutter apps with Appium.
The advantage of using a cloud based mobile device provider is that you can run tests on multiple devices, simultaneously.
You no longer need to set up, configure, maintain and update a fleet of devices. Rely on a device farm such as TestingBot to do the work for you.


More information is available in our Flutter Documentation section.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

How to do localisation testing for mobile apps

Find out how to perform localisation testing with mobile apps. Change your language or locale with Appium, XCUITest and Espresso.

Read more
Automated Testing with Puppeteer

Puppeteer combined with a test framework provides a great way to run automated browser tests. Follow this guide for more information.

Read more
Tutorial on debugging Cypress tests

This article will focus on how to debug your Cypress tests with Cypress debugger and other developer tools.

Read more