gpt4 book ai didi

Flutter 驱动程序处理最小化的应用程序

转载 作者:行者123 更新时间:2023-12-03 02:40:30 31 4
gpt4 key购买 nike

我正在为我的 flutter 应用程序制作一些测试场景。其中一项功能是“关于我们”部分。当点击“关于我们”时,用户被重定向到网页并且应用程序被最小化。这是一个问题,因为测试场景在这个地方失败了。 Flutter 无法找到任何组件,并且在此特定错误上失败

Unhandled exception:
DriverError: Failed to fulfill Tap due to remote error
Original error: Bad state: The client closed with pending request "ext.flutter.driver".
Original stack trace:
#0 new Client.withoutJson.<anonymous closure> (package:json_rpc_2/src/client.dart:70:24)
#1 _RootZone.run (dart:async/zone.dart:1445:54)
#2 _FutureListener.handleWhenComplete (dart:async/future_impl.dart:167:18)
#3 Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:666:39)
#4 Future._propagateToListeners (dart:async/future_impl.dart:722:37)
#5 Future._propagateToListeners (dart:async/future_impl.dart:621:9)
#6 Future._completeWithValue (dart:async/future_impl.dart:529:5)
#7 Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
#8 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#9 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#10 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#11 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:404:11)
#12 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
#13 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)


#0 VMServiceFlutterDriver.sendCommand (package:flutter_driver/src/driver/vmservice_driver.dart:345:7)
<asynchronous suspension>
#1 FlutterDriver.tap (package:flutter_driver/src/driver/driver.dart:207:11)
#2 LoginPage.tapOnAboutUs (file:///D:/Projects/app/test_driver/page_objects/login_page.dart:52:19)
#3 aboutUs_steps.iClickOnAboutUsOnLoginPage (file:///D:/Projects/app/test_driver/steps/aboutUs_steps.dart:16:15)
<asynchronous suspension>
#4 _InstanceMirror._invoke (dart:mirrors-patch/mirrors_impl.dart:337:37)
#5 _InstanceMirror.invoke (dart:mirrors-patch/mirrors_impl.dart:333:25)
#6 OguretsState.invokeStep.<anonymous closure> (package:ogurets/src/ogurets_internal.dart:316:29)
#7 new Future.sync (dart:async/future.dart:223:31)
#8 OguretsState.invokeStep (package:ogurets/src/ogurets_internal.dart:315:25)
#9 OguretsState.executeStep (package:ogurets/src/ogurets_internal.dart:296:13)
<asynchronous suspension>
#10 OguretsState._findClassStyleStepRunners.<anonymous closure> (package:ogurets/src/ogurets_internal.dart:270:19)
#11 _Scenario._executeSubScenario (package:ogurets/src/model/scenario.dart:163:43)
<asynchronous suspension>
#12 _Scenario.execute (package:ogurets/src/model/scenario.dart:57:17)
#13 _Feature.execute (package:ogurets/src/model/feature.dart:41:64)
#14 OguretsOpts.processFeatureFile (package:ogurets/src/ogurets_opts.dart:327:49)
<asynchronous suspension>
#15 OguretsOpts.run (package:ogurets/src/ogurets_opts.dart:303:17)
<asynchronous suspension>
#16 main (file:///D:/Projects/app/test_driver/ogurets_flutter_test.dart:30:13)
#17 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#18 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
是否有任何可能的解决方案可以从后台最小化应用程序?
我试图使用默认的 flutter 钩子(Hook),但没有正确的结果。
这些是我使用的文件和方法
Feature: AboutUsLink
As a tester I test the functionality of the About Us link


Scenario: Login - About us - Button functionality
Given I click on About us on Login Page



class aboutUs_steps {
FlutterOgurets _world;

aboutUs_steps(this._world);

@Given(r'I click on About us on Login Page')
void iClickOnAboutUsOnLoginPage() async {
LoginPage loginPage = new LoginPage(_world.driver);
await Future.delayed(Duration(seconds: FUTURE_DELAY));
loginPage.tapOnAboutUs();
}
}
和 LoginPage 中的方法
  Future<void> tapOnAboutUs() async {
await _driver.tap(aboutUs);
}

最佳答案

在这种情况下,我会选择小部件测试而不是集成测试(使用 flutter 驱动程序),因为它执行起来更快并且更容易验证正确的行为
问:如何为打开外部应用程序的功能编写测试(例如 Web 浏览器中的 URL)。
答:使用诸如 launch() 之类的方法重定向到网站或任何其他外部 URL 时,您想测试是否将正确的参数传递给 launch() - 在您的情况下,这将类似于 https://example.com/about
实现:
您需要模拟 launch() 并验证它是否已使用正确的参数调用,测试可能看起来像这样,您的 LoginPage 可以传递给启动器的模拟:

class MockLauncher extends Mock {}

void main() {
MockLauncher mockLauncher;
setUp() {
mockLauncher = MockLauncher();
when(mockLauncher.launch(any)).thenReturn(true);
}
testWidgets('click on About us', (WidgetTester tester)
async {
await tester.pumpWidget(LoginPage(launcher: mockLauncher.launch));
await tester.tap(find.byKey(ValueKey('about_us_link')));
verify(mockLauncher.launch('https://example.com/about'));
verifyNoMoreInteractions(mockLauncher);
});
}
import 'package:url_launcher/url_launcher.dart';

class LoginPage extends ...{
Function urlLauncher;

LoginPage({this.urlLauncher}) {
urlLauncher ??= launcher // use mock if it is passed in
}
}
同样的方法也可以用于集成测试,但我建议重新评估你这样做的原因,因为上面的测试策略对于大多数重定向场景应该是足够的。

关于Flutter 驱动程序处理最小化的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64498486/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com