Skip to content

[webview_flutter] 4.0.0 fails under test #117422

Closed

Description

@domesticmouse

Building a simple Flutter app based on simple_example.dart with a test fails. Test code:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:testing_webview_flutter/main.dart';

void main() {
  testWidgets('Smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(const MaterialApp(home: WebViewExample()));
  });
}

Stack trace:

$ flutter test
00:06 +0: Smoke test                                                                                                             
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building Builder:
Null check operator used on a null value

The relevant error-causing widget was:
  MaterialApp
  MaterialApp:file:///Users/brettmorgan/Documents/flutter-projects/testing_webview_flutter/test/widget_test.dart:16:35

When the exception was thrown, this was the stack:
#0      new PlatformWebViewController (package:webview_flutter_platform_interface/src/platform_webview_controller.dart:25:33)
#1      new WebViewController.fromPlatformCreationParams (package:webview_flutter/src/webview_controller.dart:28:25)
#2      new WebViewController (package:webview_flutter/src/webview_controller.dart:20:14)
#3      _WebViewExampleState.initState (package:testing_webview_flutter/main.dart:27:18)
#4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5015:57)
#5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
...     Normal element mounting (275 frames)
#280    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#281    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6435:36)
#282    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6447:32)
...     Normal element mounting (416 frames)
#698    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#699    Element.updateChild (package:flutter/src/widgets/framework.dart:3586:20)
#700    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16)
#701    RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1172:5)
#702    RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1186:7)
#703    Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#704    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:19)
#705    AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1191:19)
#706    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:378:5)
#707    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
#708    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#709    AutomatedTestWidgetsFlutterBinding.pump.<anonymous closure> (package:flutter_test/src/binding.dart:1057:9)
#712    TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#713    AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:1043:27)
#714    WidgetTester.pumpWidget.<anonymous closure> (package:flutter_test/src/widget_tester.dart:554:22)
#717    TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#718    WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:551:27)
#719    main.<anonymous closure> (file:///Users/brettmorgan/Documents/flutter-projects/testing_webview_flutter/test/widget_test.dart:16:18)
#720    testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:171:29)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from dart:async and package:stack_trace)

════════════════════════════════════════════════════════════════════════════════════════════════════
00:06 +0 -1: Smoke test [E]                                                                                                      
  Test failed. See exception logs above.
  The test description was: Smoke test
  

To run this test again: /Users/brettmorgan/flutter/bin/cache/dart-sdk/bin/dart test /Users/brettmorgan/Documents/flutter-projects/testing_webview_flutter/test/widget_test.dart -p vm --plain-name 'Smoke test'
00:06 +0 -1: Some tests failed.

Flutter doctor

$ flutter doctor -v
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.2 22D7750270d darwin-arm, locale en)
    • Flutter version 3.3.10 on channel stable at /Users/brettmorgan/flutter
    • Upstream repository https://github.com/flutter/flutter
    • Framework revision 135454af32 (5 days ago), 2022-12-15 07:36:55 -0800
    • Engine revision 3316dd8728
    • Dart version 2.18.6
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/brettmorgan/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2022.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 70.0.5
    • Dart plugin version 222.4167.21

[✓] VS Code (version 1.74.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.54.0

[✓] Connected device (3 available)
    • iPhone 14 Pro (mobile) • BBCB0062-6656-4250-BDE9-564287448DA4 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-2
      (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 13.2 22D7750270d darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.124

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Activity

  1. darshankawar commented on Dec 21, 2022

    @darshankawar
    Member

    @domesticmouse Hi,
    I get same behavior after running the test example you provided above, but if we add below to the file, then the smoke test runs successfully:

    
    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    import 'package:webview_flutter_example/main.dart';
    import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
    
    
    void main() {
    
        setUp(() {
        WebViewPlatform.instance = FakeWebViewPlatform();
      });
      testWidgets('Smoke test', (WidgetTester tester) async {
        // Build our app and trigger a frame.
        await tester.pumpWidget(const MaterialApp(home: WebViewExample()));
      });
    }
    
    class FakeWebViewPlatform extends WebViewPlatform {
      @override
      PlatformWebViewController createPlatformWebViewController(
          PlatformWebViewControllerCreationParams params,
          ) {
        return FakeWebViewController(params);
      }
    
      @override
      PlatformWebViewWidget createPlatformWebViewWidget(
          PlatformWebViewWidgetCreationParams params,
          ) {
        return FakeWebViewWidget(params);
      }
    
      @override
      PlatformWebViewCookieManager createPlatformCookieManager(
          PlatformWebViewCookieManagerCreationParams params,
          ) {
        return FakeCookieManager(params);
      }
    
      @override
      PlatformNavigationDelegate createPlatformNavigationDelegate(
          PlatformNavigationDelegateCreationParams params,
          ) {
        return FakeNavigationDelegate(params);
      }
    }
    
    class FakeWebViewController extends PlatformWebViewController {
      FakeWebViewController(super.params) : super.implementation();
    
      @override
      Future<void> setJavaScriptMode(JavaScriptMode javaScriptMode) async {}
    
      @override
      Future<void> setBackgroundColor(Color color) async {}
    
      @override
      Future<void> setPlatformNavigationDelegate(
          PlatformNavigationDelegate handler,
          ) async {}
    
      @override
      Future<void> addJavaScriptChannel(
          JavaScriptChannelParams javaScriptChannelParams) async {}
    
      @override
      Future<void> loadRequest(LoadRequestParams params) async {}
    
      @override
      Future<String?> currentUrl() async {
        return 'https://www.google.com';
      }
    }
    
    class FakeCookieManager extends PlatformWebViewCookieManager {
      FakeCookieManager(super.params) : super.implementation();
    }
    
    class FakeWebViewWidget extends PlatformWebViewWidget {
      FakeWebViewWidget(super.params) : super.implementation();
    
      @override
      Widget build(BuildContext context) {
        return Container();
      }
    }
    
    class FakeNavigationDelegate extends PlatformNavigationDelegate {
      FakeNavigationDelegate(super.params) : super.implementation();
    
      @override
      Future<void> setOnNavigationRequest(
          NavigationRequestCallback onNavigationRequest,
          ) async {}
    
      @override
      Future<void> setOnPageFinished(PageEventCallback onPageFinished) async {}
    
      @override
      Future<void> setOnPageStarted(PageEventCallback onPageStarted) async {}
    
      @override
      Future<void> setOnProgress(ProgressCallback onProgress) async {}
    
      @override
      Future<void> setOnWebResourceError(
          WebResourceErrorCallback onWebResourceError,
          ) async {}
    }
    

    What do you think ?

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 21, 2022
  3. domesticmouse commented on Dec 21, 2022

    @domesticmouse
    ContributorAuthor

    It's a bug. This behaviour is breaking the codelab written by the original authors of the previous version of the plugin. The v3 plugin doesn't break this way. The provided additional code is a workaround.

    For what it's worth, I'll be adding a note to the codelab saying the android version of the plugin is currently broken with a pointer to this bug report.

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 21, 2022
  5. darshankawar commented on Dec 21, 2022

    @darshankawar
    Member

    Thanks for the feedback.

    stable, master flutter doctor -v
    [✓] Flutter (Channel stable, 3.3.10, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.3.10 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 135454af32 (15 hours ago), 2022-12-15 07:36:55 -0800
        • Engine revision 3316dd8728
        • Dart version 2.18.6
        • DevTools version 2.15.0
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.7.0-13.0.pre.20, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.7.0-13.0.pre.20 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 169935168f (32 minutes ago), 2022-12-20 23:19:24 -0500
        • Engine revision 4724a91afd
        • Dart version 3.0.0 (build 3.0.0-45.0.dev)
        • DevTools version 2.20.0
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  6. added
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    a: tests"flutter test", flutter_test, or one of our tests
    a: error messageError messages from the Flutter framework
    and removed
    in triagePresently being triaged by the triage team
    on Dec 21, 2022
  7. stuartmorgan-g commented on Dec 21, 2022

    @stuartmorgan-g
    Contributor
  8. bparrishMines commented on Dec 21, 2022

    @bparrishMines
    Contributor

    This test is failing because in pre-4.0, the platform implementation was set to Android by default: https://github.com/flutter/plugins/blob/5f62d21ebe38a45ed03e206eb1456b33bdb0b22d/packages/webview_flutter/webview_flutter/lib/src/webview.dart#L120.

    This plugin now uses auto platform registration and no longer sets a default platform implementation: https://docs.flutter.dev/development/packages-and-plugins/developing-packages#dart-platform-implementations

    I was able to get this test to pass:

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    import 'package:webview_flutter_android/webview_flutter_android.dart';
    import 'package:webview_flutter_example/simple_example.dart';
    
    void main() {
      testWidgets('Smoke test', (WidgetTester tester) async {
        print(defaultTargetPlatform); // prints out TargetPlatform.android
        WebViewPlatform.instance = AndroidWebViewPlatform();
        // Build our app and trigger a frame.
        await tester.pumpWidget(const MaterialApp(home: WebViewExample()));
      });
    }

    Now that auto platform registration is available and we want this to be the standard, I don't think we want to set the platform implementation in webview_flutter anymore.

    @domesticmouse Could we update the codelab to use the FakePlatform as explained by @darshankawar?

    Or we could consider providing a TestPlatform in webview_flutter that a user could set to WebViewPlatform.instance manually.

  9. stuartmorgan-g commented on Dec 21, 2022

    @stuartmorgan-g
    Contributor

    Or we could consider providing a TestPlatform in webview_flutter that a user could set to WebViewPlatform.instance manually.

    I'm generally not a fan of us maintaining test implementations of things as part of the public API, since it creates a whole new things to maintain and have "should this be included in the main package" discussions about as people want increasingly complex test objects. I'd rather people set up their own tests the way they want them.

  10. stuartmorgan-g commented on Jan 3, 2023

    @stuartmorgan-g
    Contributor

    It's a bug.

    It's a bug in the codelab though; we should update the codelab. (Which codelab is this?)

    This behaviour is breaking the codelab written by the original authors of the previous version of the plugin.

    The nature of major version updates to packages is that code written against older versions may not work without updates.

    The v3 plugin doesn't break this way.

    Because the internal implementation details that such a test implicitly relied on happened to work with v3 and earlier versions. Our plugin structure has evolved over the years, and writing non-integration tests that assume plugins will work is now something we explicitly tell people not to do because it will work even less often than it used to.

    The provided additional code is a workaround.

    It's not a workaround, it's following our current documented best practices. (Even better would be not including the plugin at all in any app-level tests, as described in the page above.)

  11. domesticmouse commented on Jan 5, 2023

    @domesticmouse
    ContributorAuthor

    It's a bug.

    It's a bug in the codelab though; we should update the codelab. (Which codelab is this?)

    The codelab that I was maintaining when I crafted this bug report was Adding WebView to your Flutter app . I have an updated version sitting in cl/496865324 waiting on an LGTM from @craiglabenz

    The codelab itself doesn't contain any references to testing, but I had tests alongside the the code to make sure it worked to some level.

    This behaviour is breaking the codelab written by the original authors of the previous version of the plugin.

    The nature of major version updates to packages is that code written against older versions may not work without updates.

    The v3 plugin doesn't break this way.

    Because the internal implementation details that such a test implicitly relied on happened to work with v3 and earlier versions. Our plugin structure has evolved over the years, and writing non-integration tests that assume plugins will work is now something we explicitly tell people not to do because it will work even less often than it used to.

    The provided additional code is a workaround.

    It's not a workaround, it's following our current documented best practices. (Even better would be not including the plugin at all in any app-level tests, as described in the page above.)

    Okiday.

  12. github-actions commented on Mar 4, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  13. locked as resolved and limited conversation to collaborators on Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: error messageError messages from the Flutter frameworka: tests"flutter test", flutter_test, or one of our testsfound in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: webviewThe WebView pluginpackageflutter/packages repository. See also p: labels.r: invalidIssue is closed as not valid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions