代码之家  ›  专栏  ›  技术社区  ›  Günter Zöchbauer

如何将环境变量传递给颤振驾驶员测试

  •  17
  • Günter Zöchbauer  · 技术社区  · 7 年前

    我想把一个环境变量传递给 flutter drive

    能够读取启动的应用程序或测试代码中的值将很好,因为我在应用程序中需要它,如果我只能在测试代码中获得它,我可以使用 driver.requestData()

    我想这样指定用户名和密码,以便在应用程序中使用。

    Setting environment variables in Flutter 是一个类似的问题,但对于我的用例来说,这似乎过于复杂。

    3 回复  |  直到 7 年前
        1
  •  8
  •   Matt S.    7 年前

    我试过用Dart的 Platform.environment 在运行驱动程序测试之前读入env变量,看起来效果很好。下面是一个简单的示例,使用 FLUTTER_DRIVER_RESULTS

    import 'dart:async';
    import 'dart:io' show Platform;
    
    import 'package:flutter_driver/flutter_driver.dart';
    import 'package:test/test.dart';
    
    void main() {
      // Load environmental variables
      String resultsDirectory =
        Platform.environment['FLUTTER_DRIVER_RESULTS'] ?? '/tmp';
      print('Results directory is $resultsDirectory');
    
      group('increment button test', () {
        FlutterDriver driver;
    
        setUpAll(() async {
          // Connect to the app
          driver = await FlutterDriver.connect();
        });
    
        tearDownAll(() async {
          if (driver != null) {
            // Disconnect from the app
            driver.close();
          }
        });
    
        test('measure', () async {
          // Record the performance timeline of things that happen
          Timeline timeline = await driver.traceAction(() async {
            // Find the scrollable user list
            SerializableFinder incrementButton = find.byValueKey(
                'increment_button');
    
            // Click the button 10 times
            for (int i = 0; i < 10; i++) {
              await driver.tap(incrementButton);
    
              // Emulate time for a user's finger between taps
              await new Future<Null>.delayed(new Duration(milliseconds: 250));
            }
    
          });
            TimelineSummary summary = new TimelineSummary.summarize(timeline);
            summary.writeSummaryToFile('increment_perf',
                destinationDirectory: resultsDirectory, pretty: true);
            summary.writeTimelineToFile('increment_perf',
                destinationDirectory: resultsDirectory, pretty: true);
        });
      });
    }
    
        2
  •  4
  •   Jannie Theunissen    6 年前

    这个 .env package 很适合我:

    包括:

    import 'package:dotenv/dotenv.dart' show load, env;
    

    load();
    

    使用:

    test('can log in', () async {
          await driver.tap(emailFieldFinder);
          await driver.enterText(env['USERNAME']);
          await driver.tap(passwordFieldFinder);
          await driver.enterText(env['PASSWORD']);
          await driver.tap(loginButtonFinder);
    
          await Future<Null>.delayed(Duration(seconds: 2));
    
          expect(await driver.getText(mainMessageFinder), "Welcome");
    });
    
        3
  •  4
  •   suztomo    5 年前

    在颤振驱动程序测试中,我遇到了同样的需要,即在设备上通过环境变量进行测试应用。挑战在于测试应用程序无法直接从 flutter drive

    下面是我如何解决这个问题的。的测试名称为“field\u value\u behaviors.dart”。环境变量名称为 FIRESTORE_IMPLEMENTATION

    颤振驱动指令

    运行时指定环境变量 颤振驱动 命令:

    $ FIRESTORE_IMPLEMENTATION=cloud_firestore flutter drive --target=test_driver/field_value_behaviors.dart
    

    驱动程序(“field\u value\u behaviors\u test.dart”)作为 颤振驱动

      String firestoreImplementation =
          Platform.environment['FIRESTORE_IMPLEMENTATION'];
    

    此外,驱动程序通过以下方式将值发送给在设备上运行的测试应用程序: driver.requestData .

      final FlutterDriver driver = await FlutterDriver.connect();
      // Sends the choice to test application running on a device
      await driver.requestData(firestoreImplementation);
      await driver.requestData('waiting_test_completion',
          timeout: const Duration(minutes: 1));
      ...
    

    测试应用程序

    group() test() 颤振驱动 命令幸运的是,测试应用程序可以通过 enableFlutterDriverExtension()

    void main() async {
      final Completer<String> firestoreImplementationQuery = Completer<String>();
      final Completer<String> completer = Completer<String>();
    
      enableFlutterDriverExtension(handler: (message) {
        if (validImplementationNames.contains(message)) {
          // When value is 'cloud_firestore' or 'cloud_firestore_mocks'
          firestoreImplementationQuery.complete(message);
          return Future.value(null);
        } else if (message == 'waiting_test_completion') {
          // Have Driver program wait for this future completion at tearDownAll.
          return completer.future;
        } else {
          fail('Unexpected message from Driver: $message');
        }
      });
      tearDownAll(() {
        completer.complete(null);
      });
    

    测试应用程序根据的解析值更改行为 firestoreImplementationQuery.future :

      firestoreFutures = {
        // cloud_firestore_mocks
        'cloud_firestore_mocks': firestoreImplementationQuery.future.then((value) =>
            value == cloudFirestoreMocksImplementationName
                ? MockFirestoreInstance()
                : null),
    

    结论:通过读取环境变量 Platform.environment 在您的驱动程序中。通过将其传递给您的测试应用程序 论点

    https://github.com/atn832/cloud_firestore_mocks/pull/54