Flutter Integration Testing
Steps to be done for Flutter Integration Testing using Appium:
Installing Appium:
i. Before installing Appium, install node.
ii. After installation of node, install Appium using command 'npm i -g appium'.
iii. We have install the Flutter Driver. For that use the command 'appium driver install --source=npm appium-flutter-driver'.
iv. Start the Appium Server using command 'appium server'.
Building apk for Testing:
i. In pubspec.yaml add the following line under dev dependencies.
dev_dependencies: flutter_driver: sdk: flutter
ii. Create test_main.yaml in lib folder with following code.
import 'package:flutter_driver/driver_extension.dart'; import 'package:hrms_flutter/main.dart' as app; void main() { enableFlutterDriverExtension(); app.main(); }
iii. Build apk using the command 'flutter build apk -t lib/test_main.yaml --debug'.
Add the key for the widgets in the Application. We have to add the key for each widget, so that we can identify that element in appium with that key. Every widget will have the key as the parameter, add key my include the line 'key: Valuekey(keyValue)'.
I have used Python for writing the Test cases for Appium. I am attaching the Python Script for Reference.
For running the below script, we need Appium-Flutter-Finder and Appium-Python-Client. We can install it using 'pip install package_name'.
import os import time from appium import webdriver from appium.options.common.base import AppiumOptions from appium_flutter_finder.flutter_finder import FlutterFinder, FlutterElement from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = AppiumOptions() options.set_capability('platformName', 'Android') options.set_capability('automationName', 'Flutter') options.set_capability('deviceName', 'emulator-5556') options.set_capability('app', 'C:\\Users\\TFS-25\\StudioProjects\\hrms-flutter\\build\\app\\outputs\\apk\\debug\\app-debug.apk') options.set_capability('retryBackoffTime', 500) options.set_capability('maxRetryCount', 20) finder = FlutterFinder() print("Connecting to Appium server...") driver = webdriver.Remote('http://localhost:4723', options=options) print("Connected to Appium server.") try: username_finder = finder.by_value_key('username') username_element = FlutterElement(driver, username_finder) print("Username Field Found") username_element.click() driver.execute_script('flutter:enterText', 'username') password_finder = finder.by_value_key('password') password_element = FlutterElement(driver, password_finder) print("Password Field Found") password_element.click() driver.execute_script('flutter:enterText','password') login_finder = finder.by_value_key('login_button') login_element = FlutterElement(driver, login_finder) print("Login Button Found") login_element.click() time.sleep(10) except Exception as e: print(f"An error occurred: {e}") finally: print("Quitting driver...") driver.quit() print("Driver quit successfully.")
When running this, the emulator will start the application and start doing the instructions given in the script.