Тестирование флаттера - формы - PullRequest
0 голосов
/ 11 ноября 2019

Я пробовал тестировать во флаттере с flutter_test и flutter_driver . Возможность выполнять тестирование пользовательского интерфейса с функциями крана. Но мое требование состоит в том, чтобы тестировать формы от начала до конца (т.е. автоматически заполнять формы). Возникла проблема с 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');
    });

1 Ответ

0 голосов
/ 14 ноября 2019

Сначала нужно указать 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

Надеюсь, что это ответ на ваш вопрос.

...