driver_test.dart 3.75 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter_driver/flutter_driver.dart';
8

9
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
10 11 12 13

void main() {
  group('FlutterDriver', () {
    final SerializableFinder presentText = find.text('present');
14
    late FlutterDriver driver;
15 16 17 18 19 20 21 22 23 24 25

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    tearDownAll(() async {
      await driver.close();
    });

    test('waitFor should find text "present"', () async {
      await driver.waitFor(presentText);
26
    }, timeout: Timeout.none);
27 28

    test('waitForAbsent should time out waiting for text "present" to disappear', () async {
29 30 31 32 33 34 35 36
      await expectLater(
        () => driver.waitForAbsent(presentText, timeout: const Duration(seconds: 1)),
        throwsA(isA<DriverError>().having(
          (DriverError error) => error.message,
          'message',
          contains('Timeout while executing waitForAbsent'),
        )),
      );
37
    }, timeout: Timeout.none);
38 39 40

    test('waitForAbsent should resolve when text "present" disappears', () async {
      // Begin waiting for it to disappear
41
      final Completer<void> whenWaitForAbsentResolves = Completer<void>();
42
      driver.waitForAbsent(presentText).then(
43 44 45 46 47
        whenWaitForAbsentResolves.complete,
        onError: whenWaitForAbsentResolves.completeError,
      );

      // Wait 1 second then make it disappear
48
      await Future<void>.delayed(const Duration(seconds: 1));
49 50 51 52
      await driver.tap(find.byValueKey('togglePresent'));

      // Ensure waitForAbsent resolves
      await whenWaitForAbsentResolves.future;
53
    }, timeout: Timeout.none);
54 55

    test('waitFor times out waiting for "present" to reappear', () async {
56 57 58 59 60 61 62 63
      await expectLater(
        () => driver.waitFor(presentText, timeout: const Duration(seconds: 1)),
        throwsA(isA<DriverError>().having(
          (DriverError error) => error.message,
          'message',
          contains('Timeout while executing waitFor'),
        )),
      );
64
    }, timeout: Timeout.none);
65 66 67

    test('waitFor should resolve when text "present" reappears', () async {
      // Begin waiting for it to reappear
68
      final Completer<void> whenWaitForResolves = Completer<void>();
69
      driver.waitFor(presentText).then(
70 71 72 73 74
        whenWaitForResolves.complete,
        onError: whenWaitForResolves.completeError,
      );

      // Wait 1 second then make it appear
75
      await Future<void>.delayed(const Duration(seconds: 1));
76 77 78 79
      await driver.tap(find.byValueKey('togglePresent'));

      // Ensure waitFor resolves
      await whenWaitForResolves.future;
80
    }, timeout: Timeout.none);
81 82 83

    test('waitForAbsent resolves immediately when the element does not exist', () async {
      await driver.waitForAbsent(find.text('that does not exist'));
84
    }, timeout: Timeout.none);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    test('uses hit test to determine tappable elements', () async {
      final SerializableFinder a = find.byValueKey('a');
      final SerializableFinder menu = find.byType('_DropdownMenu<Letter>');

      // Dropdown is closed
      await driver.waitForAbsent(menu);

      // Open dropdown
      await driver.tap(a);
      await driver.waitFor(menu);

      // Close it again
      await driver.tap(a);
      await driver.waitForAbsent(menu);
100
    }, timeout: Timeout.none);
101 102 103 104 105 106 107 108

    test('enters text in a text field', () async {
      final SerializableFinder textField = find.byValueKey('enter-text-field');
      await driver.tap(textField);
      await driver.enterText('Hello!');
      await driver.waitFor(find.text('Hello!'));
      await driver.enterText('World!');
      await driver.waitFor(find.text('World!'));
109
    }, timeout: Timeout.none);
110 111
  });
}