Flutter Driver - can we pass command line arguments to flutter driver tests
Asked Answered
E

1

17

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

Enneagon answered 23/12, 2019 at 21:50 Comment(1)
Hi, Did you managed to solve this?Wooer
I
3

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"
Intermit answered 1/12, 2020 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.