我不确定如何检查小部件当前是否使用FlutterDriver显示。
使用WidgetTester,这非常容易执行,例如使用findsOneWidget。
但是,在使用FlutterDriver进行集成测试时,无法访问WidgetTester对象。
FlutterDriver.waitFor方法没有指示小部件是否在给定的持续时间内找到。
如何使用FlutterDriver检查小部件是否在屏幕上
发布于 2019-06-19 04:30:32
颤振驱动程序没有显式的方法来检查小部件是否存在/存在,但是我们可以使用waitFor方法创建一个自定义方法来达到这个目的。例如,我在屏幕上有一个简单的text小部件,我将编写一个颤振驱动程序测试,以检查该小部件是否存在,或者没有使用自定义方法isPresent。
主要代码:
body: Center(
child:
Text('This is Test', key: Key('textKey'))以下是颤振驱动程序测试,以检查是否存在此小部件:
test('check if text widget is present', () async {
final isExists = await isPresent(find.byValueKey('textKey'), driver);
if (isExists) {
print('widget is present');
} else {
print('widget is not present');
}
});isPresent是自定义方法,其定义如下:
isPresent(SerializableFinder byValueKey, FlutterDriver driver, {Duration timeout = const Duration(seconds: 1)}) async {
try {
await driver.waitFor(byValueKey,timeout: timeout);
return true;
} catch(exception) {
return false;
}
}运行测试会检测到小部件的存在:

如果我注释掉text小部件代码,然后运行测试,它就会检测到小部件不存在:

https://stackoverflow.com/questions/56602769
复制相似问题