颤振测试:从文本字段中检索值

时间:2020-05-03 08:37:49

标签: authentication flutter testing dart flutter-test

我当前正在测试自己的登录页面,我想确保输入的值是单击登录时传递的值(用户名和密码)。我想做的不仅仅是验证输入是否为某种格式。我想看看我是否写了“ Tom123”,它返回了“ Tom123”。进行此测试的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

编辑:以下是OP的完整示例。

如果这不能解决您的问题,我建议您清楚自己的意图。

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

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: LoginWidget(),
        ),
      ),
    );
  }
}

class LoginWidget extends StatelessWidget {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final _formKey = GlobalKey<FormState>();

// Simple password & email validators, returns text if password is empty, if not returns null
  String _validateemail(String value) {
    if (value.isEmpty) {
      return "email must not be empty";
    }
    return null;
  }

  String _validatepassword(String value) {
    if (value.isEmpty) {
      return "password must not be empty";
    }
    return null;
  }

  final bool validated = true;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Form(
          key: _formKey,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextFormField(
                validator: _validateemail,
                controller: _usernameController,
                decoration: InputDecoration(labelText: 'Username'),
              ),
              TextFormField(
                validator: _validatepassword,
                controller: _passwordController,
                decoration: InputDecoration(labelText: 'Password'),
              ),
            ],
          ),
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: MaterialButton(
            height: 70,
            minWidth: MediaQuery.of(context).size.width,
            onPressed: () {
              if (_formKey.currentState.validate()) {

                // checks if the username and password match the following conditions and if they do, navigate to next page.
                if (_passwordController.text == "password" &&
                    _usernameController.text == "example") {
                  SystemChannels.textInput.invokeMethod('TextInput.hide');
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SuccessPage()),
                  );
                }

                // prints the following if the controllers' text do not match the preceding condition
                print("Invalid username or password entered. You entered " + _usernameController.text + " and " + _passwordController.text);
              } else {
                print(_usernameController.text);
                print(_passwordController.text);
              }
            },
            color: Colors.white,
            child: Text(
              "LOGIN",
              style: TextStyle(color: Colors.blueGrey),
            ),
          ),
        )
      ],
    );
  }
}

class SuccessPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Working! Navigated from login page."),
            Padding(
              padding: const EdgeInsets.only(top: 40),
              child: MaterialButton(
                color: Colors.white,
                child: Text(
                  "Go back",
                  style: TextStyle(color: Colors.blueGrey),
                ),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}