1

我正在使用 Flutter Driver 为我们的应用程序编写一些集成测试,并且我正在尝试使用该waitFor命令等到元素出现。

该命令运行良好并等待我的元素,或者在找不到它时失败。我的问题是,当它失败时,控制台中的日志不会告诉我哪些waitFor调用失败了。

例如,我运行以下代码来等待 myButton1 和 myButton2:

Duration timeout = new Duration(seconds: 2);
await _driver.waitFor(myButton1, timeout: timeout);
await _driver.waitFor(myButton2, timeout: timeout);

但是,当我运行测试时,它们会失败并显示以下错误消息:

00:04 +0 -1: Buttons shown when app is started [E]                                                                               
  DriverError: Error in Flutter application: Timeout while executing waitFor: TimeoutException after 0:00:02.000000: Future not completed
  
  
  package:flutter_driver/src/driver/vmservice_driver.dart 343:7  VMServiceFlutterDriver.sendCommand
  
00:04 +0 -1: Buttons (tearDownAll)                                                                                               [  +77 ms] test 0: process with pid 87459 no longer needed by test harness
[        ] test 0: cleaning up...
[        ] test 0: ensuring end-of-process for shell
[  +10 ms] test 0: deleting temporary directory
[   +4 ms] test 0: shutting down test harness socket server
[   +2 ms] test 0: finished
00:04 +0 -1: Some tests failed.                                                                                                            
[  +13 ms] Deleting /var/folders/xt/0fckrj2558746_v71h2vwqh00000gn/T/flutter_tools.WEuo76/flutter_test_compiler.u7VNn4...
[   +9 ms] killing pid 87444
[  +37 ms] Deleting /var/folders/xt/0fckrj2558746_v71h2vwqh00000gn/T/flutter_tools.WEuo76/flutter_test_fonts.FDpcXF...
[   +6 ms] test package returned with exit code 1
[  +13 ms] "flutter test" took 4,890ms.
[   +6 ms] 
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:14:3)
           #1      TestCommand.runCommand (package:flutter_tools/src/commands/test.dart:292:7)
           <asynchronous suspension>
           #2      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:860:18)
           #3      _rootRunUnary (dart:async/zone.dart:1198:47)
           #4      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
           #5      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
           #6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
           #7      Future._propagateToListeners (dart:async/future_impl.dart:725:32)
           #8      Future._completeWithValue (dart:async/future_impl.dart:529:5)
           #9      Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
           #10     _rootRun (dart:async/zone.dart:1190:13)
           #11     _CustomZone.run (dart:async/zone.dart:1093:19)
           #12     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
           #13     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
           #14     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
           #15     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
           #16     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
           #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)
           
           
[ +254 ms] ensureAnalyticsSent: 251ms
[   +1 ms] Running shutdown hooks
[        ] Shutdown hook priority 4
[        ] Shutdown hooks complete
[        ] exiting with code 1

此信息不会告诉我是 myButton1 还是 myButton2 失败了。它也没有从我的测试中给我任何代码行来指示哪些行实际上有错误。

对此有什么想法吗?

4

1 回答 1

1

我还没有弄清楚是否有内置的方法可以做到这一点,但我写了一个辅助方法来获取我需要的信息:

  Future waitForObject(SerializableFinder object, Duration timeout, {String errorMessage = "waitForObject timed out"}) async {
    var message = "ERROR ==> $errorMessage";
    return await _driver.waitFor(object, timeout: timeout).catchError((e) { throw(message);});
  }

如果找不到对象,这仍然会在超时后引发错误,但它也会将我指向导致超时的代码中的确切行。

于 2020-08-28T05:36:57.710 回答