颤振测试-表格

时间:2019-11-11 07:08:34

标签: flutter-test

我尝试使用 flutter_test flutter_driver 在Flutter中进行测试。能够通过点击功能实现UI测试。但是我的要求是测试表单的首尾对端(即自动填写表单)。面对 driver.enterText()的问题,该问题没有得到执行,并且测试用例从此处停止执行。

下面是我尝试过的代码:

test('Going back to customers tab', () async {
      // First, tap the button.
      final customerTabFinder = find.text('Customers');
      await driver.waitFor(customerTabFinder);
      await driver.tap(customerTabFinder);
      await driver.getText(find.text("Customer"));
      await takeScreenshot(driver, 'screenshots/incremented1.png');
    });

    test("Adding customers", () async {
      final pressAddCustomerButton = find.byTooltip("Increment");

      await driver.waitFor(pressAddCustomerButton);
      await driver.tap(pressAddCustomerButton);
      print("Add customer page is opened");
    });

    test("Adding text in textfield", () async {
      await driver.tap(find.byValueKey("CustomerBusinessName"));
      await sleep(Duration(seconds: 5));
      await driver.enterText('Hello !');

      await driver.tap(find.byValueKey("CustomerPhoneNumber"));
      await driver.enterText('1234567890');
    });

2 个答案:

答案 0 :(得分:0)

您首先需要告诉driver等待直到找到元素CustomerBusinessName textField,然后点击它,然后直接输入文本。在点击和输入文本操作之间无需等待或睡眠5秒钟,因为驱动程序首先需要找到该元素,然后对其执行操作。我尝试渲染两个TextFields,并且能够在两个文本中正确输入文字。这是工作示例代码:

main.dart

body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Form(
                  key: Key('inputTextField'),
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: 'Enter name'
                    ),
                  )
              ),
              Form(
                  key: Key('inputTextField1'),
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: 'Enter phone'
                    ),
                  )
              )
            ],
          )
        )

enter image description here

driver test

test('enter text', () async {
    final formFinder = find.byValueKey('inputTextField');
    final formFinder1 = find.byValueKey('inputTextField1');

    await driver.waitFor(formFinder);
    await driver.tap(formFinder);
    await driver.enterText('Hello');
    print('entered text');

    await driver.waitFor(formFinder1);
    await driver.tap(formFinder1);
    await driver.enterText('123456');
    print('entered number');
  });

测试结果:

enter image description here

希望这能回答您的问题。

答案 1 :(得分:-1)

现在,您应该稍微不同地解决此问题。您可以使用测试器创建元素并与之交互。 Docs

await tester.pumpWidget(testWidget);
await tester.enterText(find.byKey(Key("titleInput")), "title");