Dart 如何将环境变量传递给颤振驾驶员测试
我想将环境变量传递给颤振驱动测试 能够读取启动的应用程序或测试代码中的值就可以了,因为我在应用程序中需要它,如果我只能在测试代码中获得它,我可以使用Dart 如何将环境变量传递给颤振驾驶员测试,dart,flutter,Dart,Flutter,我想将环境变量传递给颤振驱动测试 能够读取启动的应用程序或测试代码中的值就可以了,因为我在应用程序中需要它,如果我只能在测试代码中获得它,我可以使用driver.requestData()将它传递给应用程序。 例如,Travis允许我指定不以任何方式公开的环境变量(如脚本内容和日志输出) 我想以这种方式指定用户名和密码,以便在应用程序中使用 这是一个类似的问题,但对于我的用例来说似乎过于复杂了 在运行驱动程序测试之前,我尝试使用Dart的Platform.environment读取环境变量,它似
driver.requestData()将它传递给应用程序。
例如,Travis允许我指定不以任何方式公开的环境变量(如脚本内容和日志输出)
我想以这种方式指定用户名和密码,以便在应用程序中使用
这是一个类似的问题,但对于我的用例来说似乎过于复杂了 在运行驱动程序测试之前,我尝试使用Dart的
Platform.environment
读取环境变量,它似乎工作正常。下面是一个简单的示例,它使用flatter\u DRIVER\u RESULTS
env变量为测试摘要设置输出目录
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);
});
});
}
导入'dart:async';
导入“dart:io”展示平台;
进口“包装:颤振驱动程序/颤振驱动程序.dart”;
导入“包:test/test.dart”;
void main(){
//负载环境变量
字符串结果目录=
平台环境['flatter_DRIVER_RESULTS']??'/tmp';
打印('Results目录为$resultsDirectory');
组('增量按钮测试',(){
颤振驱动器;
setUpAll(()异步{
//连接到应用程序
driver=等待driver.connect();
});
拆卸所有(()异步{
if(驱动程序!=null){
//断开与应用程序的连接
driver.close();
}
});
测试('measure',()异步{
//记录所发生事件的绩效时间表
Timeline Timeline=wait driver.traceAction(()异步{
//查找可滚动的用户列表
SerializableFinder incrementButton=find.byValueKey(
“增量按钮”);
//点击按钮10次
对于(int i=0;i<10;i++){
等待驱动程序。点击(递增按钮);
//模拟用户手指在轻触之间的时间
等待新的未来。延迟(新的持续时间(毫秒:250));
}
});
TimelineSummary summary=新的TimelineSummary.summary(时间线);
summary.writeSummaryToFile('increment_perf',
destinationDirectory:resultsDirectory,pretty:true);
summary.WriteTimeLineofile('increment_perf',
destinationDirectory:resultsDirectory,pretty:true);
});
});
}
这本书很适合我:
包括:
import 'package:dotenv/dotenv.dart' show load, env;
负载:
使用:
test('可以登录',()异步{
等待驱动程序。点击(emailFieldFinder);
等待驱动程序。输入文本(env['USERNAME']);
等待驱动程序。点击(passwordFieldFinder);
等待驱动程序。输入文本(env['PASSWORD']);
等待驱动程序。轻按(loginButtonFinder);
等待未来。延迟(持续时间(秒:2));
expect(等待driver.getText(mainMessageFinder),“欢迎”);
});
在颤振驾驶员测试中,我遇到了同样的需要,需要通过设备上的测试应用程序的环境变量。挑战在于测试应用程序无法直接从flatterdrive
命令读取环境变量
下面是我如何解决这个问题的。的测试名称为“field\u value\u behaviors.dart”。环境变量名为FIRESTORE\u IMPLEMENTATION
颤振驱动指令
运行flatter drive
命令时指定环境变量:
$ FIRESTORE_IMPLEMENTATION=cloud_firestore flutter drive --target=test_driver/field_value_behaviors.dart
驱动程序
驱动程序(“field_value_behaviors_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));
...
测试应用
测试应用程序(“field\u value\u behaviors.dart”)具有group()
和Test()
函数调用,并在设备(模拟器)上运行。因此,它无法直接从颤振驱动
命令中读取环境变量。幸运的是,测试应用程序可以通过EnableFlatterDriverExtension()
从驱动程序接收字符串消息:
结论:通过驱动程序中的Platform.environment
读取环境变量。通过driver.requestData
参数将其传递给测试应用程序
本请购单中规定了实施:
我让测试在“应用程序代码”中运行,而不是在测试文件中运行(在我发布此问题后,移动到此策略)。我想通过向注册的插件发送一个gequest,将值传递给设备/模拟器上运行的代码可能会起作用。我会再打电话给你,让你知道。
String firestoreImplementation =
Platform.environment['FIRESTORE_IMPLEMENTATION'];
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));
...
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);
});
firestoreFutures = {
// cloud_firestore_mocks
'cloud_firestore_mocks': firestoreImplementationQuery.future.then((value) =>
value == cloudFirestoreMocksImplementationName
? MockFirestoreInstance()
: null),