简体   繁体   English

颤振:如何在这个颤振测试中获取按钮小部件?

[英]flutter: how to get the button widget in this flutter test?

I'm working with flutter test .我正在使用flutter test I want to test something after tap a widget but I don't know how to find the button.我想在tap a widget后测试一些东西,但我不知道如何找到按钮。 Because I custom it rather than use Icon or iconButton .因为我自定义它而不是使用IconiconButton Also, I can't find it according to its text because another widget has the same name.另外,我无法根据其text找到它,因为另一个小部件具有相同的名称。 And also two InkWell widget as you can see.如您所见,还有两个InkWell小部件。 My basic test code is here.我的基本测试代码在这里。

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

import 'package:project/page/login.dart';

void main() {
  testWidgets('login page test', (WidgetTester tester) async {
    // Build our app and trigger a frame
    await tester.pumpWidget(Login());

    expect(find.text('login'), findsNWidgets(2));
    expect(find.text('ID'), findsOneWidget);
    expect(find.text('password'), findsOneWidget);
    expect(find.text('end'), findsOneWidget);

    // how to find my custom button
    await tester.tap((find.byElementType(InkWell)));  <- my question here

    // test other things after tapping the button.
  });

}

在此处输入图像描述

Instead of find.byElementType() use find.byType() .而不是find.byElementType()使用find.byType()

So your code will be like this:所以你的代码将是这样的:

await tester.tap((find.byType(InkWell)));等待 tester.tap((find.byType(InkWell)));

Like this you have to mention key (key: const Key("btnLogin"),)像这样你必须提到 key (key: const Key("btnLogin"),)

ElevatedButton(
          key: const Key("btnLogin"),
          onPressed: () {
            _decrementCounter();
          },
          child: const Text(
            " Login ",
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.white),
          ),
        ),

in Widget test在小部件测试中

await tester.tap(find.byKey(const Key('btnLogin')));
await tester.pump();
    InkWell(
          key: const Key("btnLogin"),
          child: ElevatedButton(
            onPressed: () {
              _decrementCounter();
            },
            child: const Text(
              " Login ",
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),

Like this also we can do像这样我们也可以这样做

await tester.tap(find.byKey(const Key('btnLogin')));
await tester.pump();

give a key name to your inkwell button like this.像这样为您的墨水池按钮命名。

- in your login screen. - 在您的登录屏幕中。

     InkWell(
      key: const Key("login"),
      child: ElevatedButton(
        onPressed: () {
        },
        child: const Text(
          " Login ",
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.white),
        ),
      ),
    ),

 **- in your testing file**
    
   var loginButton = find.byKey(const Key('login'));
   await tester.tap(login);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM