首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:参数类型'FlutterDriver?‘不能分配给参数类型'FlutterDriver‘

错误:参数类型'FlutterDriver?‘不能分配给参数类型'FlutterDriver‘
EN

Stack Overflow用户
提问于 2022-11-28 01:13:05
回答 1查看 19关注 0票数 0

我是一个非常新的颤振和Gherkin,我试图遵循本教程,以实现测试与Gherkin在颤振为一个简单的反测试。

问题是,当我尝试在我的macOS终端上运行测试时,我使用:

代码语言:javascript
复制
dart test_driver/app_test.dart --feature="counter_button.feature"

我不断地发现错误:

代码语言:javascript
复制
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);

这是我的counter_button.feature文件:

代码语言:javascript
复制
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

这是我的counter_button_steps.dart代码:

代码语言:javascript
复制
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");
}

我在我的main.dart文件中有一个小部件的密钥:

代码语言:javascript
复制
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,
            ),
          ],

我有flutter_gherkin:^2.0.0依赖项。最后,这是我的app_test.dart代码:

代码语言:javascript
复制
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);
}

这个问题似乎只发生在getText方法中。我没有遇到isPresent(world.driver,定位器)或tap(world.driver,定位器)的任何问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-28 01:36:23

试着改变

代码语言:javascript
复制
world.driver

使用

代码语言:javascript
复制
world.driver!

只需在其末尾添加!即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74595058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档