How leancodepl/patrol works
Project Type & Purpose
• Patrol is a comprehensive UI testing framework built for Flutter apps. It overcomes limitations in flutter_test and integration_test by providing native automation capabilities (e.g., handling OS interactions like permissions, notifications, and toggling system features) and a set of custom finders that simplify test authoring.
• The project is structured as a monorepo with multiple interrelated packages (for example, patrol, patrol_cli, patrol_finders, patrol_gen, etc.) and includes integrations with native mobile platforms (Android, iOS, macOS).
File Structure
• Key directories include:
– Top-level configuration files (CI workflows in the .github folder, build scripts, and configuration files for CI/CD).
– The dev folder that contains sample integration tests and demonstration apps (e2e tests).
– The packages folder, which is segmented into components such as:
• patrol: the core testing framework with custom finder APIs and native automation extensions.
• patrol_cli: the command-line interface that facilitates test execution, contract generation, and native automation communication.
• patrol_finders: a companion package providing enhanced finders for Flutter UI.
• patrol_gen: a code-generation tool for bridging Dart and native contracts.
• Others like adb, patrol_devtools_extension, and patrol_log that extend functionality (e.g., logging, integration with devtools, and ADB communication).
• Additionally, documentation folders (docs and dev_docs) supply the user and developer guides.
README for Additional Insights
• The README explains the framework’s purpose: to simplify writing UI tests with improved APIs (using “patrolTest,” custom finders, and native automation calls).
• It showcases examples where test functions use a DSL to interact with both Flutter widgets and native features.
• It also provides instructions for generating native contracts, highlighting how the CLI and code generator (patrol_gen) integrate with the core library.
a. Main Components:
– Flutter App/Test Runner: Represents the environment where integration tests are executed.
– Patrol Core Library (packages/patrol): Contains the testing DSL (e.g., patrolTest), custom finder implementations, and the binding between Flutter and native automation.
– Patrol CLI (packages/patrol_cli): Acts as the orchestration engine to run tests, manage device communication, and enable native automation features.
– Native Automation Layer:
• For Android: Modules written in Kotlin that interface with native device features.
• For iOS/macOS: Swift/Objective-C and related native code (also seen in packages like patrol_devtools_extension and patrol_finders).
– Contract/Code Generator (packages/patrol_gen): Generates bridge code (“contracts”) ensuring that communication between the Dart layer and native automation is synchronized.
– Supplementary Components: adb package (providing tooling for Android Debug Bridge interactions), logging (patrol_log), and integrations with developer tools.
– Continuous Integration & Deployment (CI/CD): GitHub workflows and scripts for testing on various platforms, device emulators, and publishing artifacts.
b. Relationships & Interactions:
– The CLI invokes the core testing library and passes commands and test instructions to the native automation layer.
– The core library in the Flutter app communicates with native modules (via platform channels or defined contracts) to perform OS-level tasks.
– The patrol_gen tool updates or generates native contracts that both the Dart layer and native code expect, ensuring consistent communication.
– Auxiliary packages (like patrol_finders and patrol_devtools_extension) extend core functionality and might plug into the testing workflow.
c. Architectural Patterns & Design Principles:
– Modularized, multi-package monorepo: Each package represents a clear concern (test DSL, CLI, native automation, logging, etc.).
– Decoupling between test writing (DSL) and native interactions: The contract generator enforces this boundary.
– Client-server style communication between the Flutter testing interface and native services (with the CLI acting as an orchestrator and mediator).
– Extensibility: Custom finders and plugins (e.g., devtools extension) allow for easier integration with additional debugging or testing tools.
– Use of CI/CD pipelines to drive multi-platform testing, reflecting the real-world device and emulator interactions.
d. Relevant Technologies & Frameworks:
– Flutter and Dart for the main testing API.
– Kotlin (Android) and Swift/Objective-C (iOS/macOS) for native automation.
– GitHub Actions for CI/CD workflows.
– Standard Dart tooling (pubspec, analysis_options) and code generation libraries.