I want to pass the test environment as a command line argument to my flutter driver integration test. Is it possible ?
I did read in the below post that we can use environment variables in flutter driver tests.But I was more interested in command line arguments.
How to pass an environment variable to a flutter driver test
Integration tests in Flutter are written using the integration test package, provided by the SDK. This is Flutter's version of Selenium WebDriver (generic web), Protractor (Angular), Espresso (Android), or Earl Gray (iOS). The package internally uses flutter driver to drive the test on a device.
flutter: The Flutter command-line tool. The flutter command-line tool is how developers (or IDEs on behalf of developers) interact with Flutter. For Dart related commands, you can use the dart command-line tool.
There are three types of tests that Flutter supports. A unit test verifies the behavior of a method or class. A widget test verifies the behavior of Flutter widgets without running the app itself. An integration test (also called end-to-end testing or GUI testing) runs the full app.
Once the app is complete, you will write the following tests: Unit tests to validate the add and remove operations. Widgets tests for the home and favorites pages. UI and performance tests for the entire app using integration tests.
How to Pass Arguments to Test App?
Whilst command line arguments are not supported in the traditional console command sense, you can use --dart-define
to pass variables to the main test class. (Although if you are testing a desktop platform you can use the -a
argument to Flutter Driver)
Under the covers it uses environment variables to pass arguments in.
See How do you pass arguments from command line to main in Flutter/Dart? for more details.
How to Pass Arguments to Each Test?
From within the test, the environment variables are not available, so we can use the DataHandler of the Flutter Driver Extension to get the data.
In your main test app, the one that Flutter Driver uses to start the test, you can set up a DataHandler, like this:
void main() async {
const testsString = String.fromEnvironment("tests");
enableFlutterDriverExtension(handler: (request) async {
String returnString;
switch (request) {
case "get_tests":
{
returnString = testsString;
break;
}
}
return returnString;
});
// Call main app
app.e2e(configName: 'server-1');
}
Now, from your individual tests, you can call the DataHandler and request the 'command arguments' as data:
// A test file
void main() async {
// Connect to app
FlutterDriver driver = await FlutterDriver.connect();
String testsString = await config.driver.requestData("get_tests");
print("Args: $testsString");
}
Kick off the tests like this:
flutter drive --profile --target ./test_driver/app.dart --dart-define="tests=home_page" --dart-define="sample_data=ABC;XYZ"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With