Home > Net >  Error: The argument type 'FlutterDriver?' can't be assigned to the parameter type �
Error: The argument type 'FlutterDriver?' can't be assigned to the parameter type �

Time:11-29

I am pretty new to Flutter and Gherkin and I am trying to follow this tutorial to implement testing with Gherkin in Flutter for a simple counter test.

The problem is when I try to run the test on my macOS terminal using:

dart test_driver/app_test.dart --feature="counter_button.feature"

I keep getting the error:

Error: The argument type 'FlutterDriver?' can't be assigned to the parameter type 'FlutterDriver' because 'FlutterDriver?' is nullable and 'FlutterDriver' isn't.
 - 'FlutterDriver' is from 'package:flutter_driver/src/driver/driver.dart' ('../../Downloads/flutter/packages/flutter_driver/lib/src/driver/driver.dart').
    var counterVal  = await FlutterDriverUtils.getText(world.driver, locator);

This is my counter_button.feature file:

Feature: Counter Button

    As a user
    I want to tap the plus button
    So that I can see the counter increment

    Scenario: User taps on counter button
        Given the user is at the counter dashboard
        And the counter value is at 0
        When the user taps on the plus button
        Then the counter value is at 1

And this is my counter_button_steps.dart code:

import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_gherkin/flutter_gherkin.dart';
import 'package:gherkin/gherkin.dart';



class UserIsInDashboardStep extends GivenWithWorld<FlutterWorld>  {
  @override
  Future<void> executeStep() async {
    final locator = find.text('You have pushed the button this many times:');
    var locatorExists = await FlutterDriverUtils.isPresent(world.driver, locator);
    expectMatch(true, locatorExists);
  }

  @override
  RegExp get pattern => RegExp(r"the user is at the counter dashboard");
}

class CounterValueStep extends And1WithWorld<int, FlutterWorld> {
  @override
  Future<void> executeStep(int expectedVal) async {
    final locator = find.byValueKey('counter-val-key');
    var counterVal  = await FlutterDriverUtils.getText(world.driver, locator);

    expectMatch(expectedVal, int.parse(counterVal));
  }

  @override
  RegExp get pattern => RegExp(r"the counter value is at {int}");
}

class UserTapsIncrementButton extends WhenWithWorld<FlutterWorld> {
  @override
  Future<void> executeStep() async {
    final locator = find.byTooltip('Increment');
    await FlutterDriverUtils.tap(world.driver, locator);
  }

  @override
  RegExp get pattern => RegExp(r"the user taps on the plus button");
}

And I have this key for widget in my main.dart file:

children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              key: Key('counter-val-key'),
              style: Theme.of(context).textTheme.displaySmall,
            ),
          ],

I have the flutter_gherkin: ^2.0.0 dependency. Finally this is my app_test.dart code:

import 'dart:async';
import 'dart:io';
import 'package:flutter_gherkin/flutter_gherkin.dart';
import 'package:gherkin/gherkin.dart';
import 'package:glob/glob.dart';

import 'steps/counter_button_steps.dart';

Future<void> main() {
  final config = FlutterTestConfiguration()
    ..features = [Glob(r"test_driver/features/**.feature")]
    ..reporters = [
      ProgressReporter(),
      TestRunSummaryReporter(),
      JsonReporter(path: './report.json')
    ] 
    ..hooks = []
    ..stepDefinitions = [
      UserIsInDashboardStep(),
      CounterValueStep(),
      UserTapsIncrementButton()
    ]
    ..customStepParameterDefinitions = [

    ]
    ..restartAppBetweenScenarios = true
    ..targetAppPath = "test_driver/app.dart"
    ..targetDeviceId = 'macos'
    ..keepAppRunningAfterTests = false; 
  return GherkinRunner().execute(config);
}

The problem only seems to occur with the getText method. I did not face with any issues with isPresent(world.driver, locator) or tap(world.driver, locator).

CodePudding user response:

Try changing

world.driver

with

world.driver!

Just add ! at the end of it.

  • Related