Home Test Flutter app with Appium
Post
Cancel

Test Flutter app with Appium

appium flutter driver

Enviroment

1
2
3
Flutter (Channel stable, 2.8.1)
ruby 3.1.0p0
appium-flutter-driver@0.0.34

Install Appium

1
2
3
4
5
6
7
8
9
10
# shell
npm install -g appium
npm install -g appium-flutter-driver
npm install -g appium-doctor

# verify install
appium-doctor

# run server
appium

Prepare Flutter demo project

1
2
3
4
5
6
# shell
mkdir FlutterAppium
cd FlutterAppium

# create a flutter demo project
flutter create demo

Add integration_test

1
2
3
4
5
6
# pubspec.yaml
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter

Install packages

1
2
# shell
pub get

Modify main.dart, add enableFlutterDriverExtension

1
2
3
4
5
6
7
8
// main.dart
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';

void main() {
  enableFlutterDriverExtension(); // Add it before runApp
  runApp(const MyApp());
}

Add ValueKey for counter result widget:

1
2
3
4
5
6
// main.dart
Text(
  '$_counter',
  style: Theme.of(context).textTheme.headline4,
  key: const ValueKey('counter'), // Add value key
),

Compile a debug apk:

1
2
# shell
flutter build apk --debug

We will get an app-debug.apk, and we will use this apk to run the test.

Prepare ruby test code

Make sure you have already installed ruby and bundler.

1
2
3
4
5
6
7
# shell
cd ..
mkdir test
cd test

# Generate Gemfile
bundle init

Add gems to Gemfile

1
2
3
4
5
6
7
8
# Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem 'appium_flutter_finder'
gem 'appium_lib_core'
gem 'minitest'

Install gems:

1
2
# shell
bundle install

Add test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# android_app_test.rb
require 'appium_lib_core'
require 'appium_flutter_finder'

require 'minitest/autorun'

class ExampleTests < Minitest::Test
  include ::Appium::Flutter::Finder

  ANDROID_CAPS = {
    caps: {
      platformName: 'Android',
      automationName: 'flutter',
      deviceName: 'Pixel 2',
      app: "../demo/build/app/outputs/flutter-apk/app-debug.apk"
    },
    appium_lib: {
      export_session: true,
      wait_timeout: 20,
      wait_interval: 1
    }
  }.freeze

  def test_run_example_android_scenario
    @core = ::Appium::Core.for(ANDROID_CAPS)
    @driver = @core.start_driver

    @driver.execute_script 'flutter:getRenderTree'
    assert_equal 'ok', @driver.execute_script('flutter:checkHealth', {})

    # Find and click FloatingActionButton
    tooltip_finder = by_tooltip 'Increment'
    @driver.execute_script('flutter:waitFor', tooltip_finder, 100)
    floating_button_element = ::Appium::Flutter::Element.new(@driver, finder: tooltip_finder)
    floating_button_element.click

    # Confirm the count result
    counter_finder = by_value_key 'counter'
    counter_element = ::Appium::Flutter::Element.new(@driver, finder: counter_finder)
    assert_equal '1', counter_element.text
  end
end

Run an Android emulator from Android Studio or command line, then run command to test the app.

1
2
# shell
ruby android_app_test.rb

We will get the test result:

flutter-appium-android-01

Appium@next

Next, we will try to use latest appium 2.x.

Enviroment

1
Appium v2.0.0-beta.24

Install appium

1
2
3
4
5
6
7
8
9
# shell
npm install -g appium@next
appium driver install flutter

# confirm install
appium driver list --installed

# run appium server
appium

All other steps are the same as appium flutter driver, except we add the server_url in configuration:

1
2
3
4
5
6
7
# android_app_test.rb
appium_lib: {
  server_url: 'http://127.0.0.1:4723', // Add server url
  export_session: true,
  wait_timeout: 20,
  wait_interval: 1
}

Run test

1
2
# shell
ruby android_app_test.rb

Read more

https://github.com/appium/ruby_lib

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

This post is licensed under CC BY 4.0 by the author.