mirror of
https://github.com/flutter/samples.git
synced 2026-03-28 23:32:05 +00:00
Testing sample (#500)
This commit is contained in:
15
testing_app/test_driver/app.dart
Normal file
15
testing_app/test_driver/app.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
import 'package:testing_app/main.dart' as app;
|
||||
|
||||
void main() {
|
||||
// This line enables the extension.
|
||||
enableFlutterDriverExtension();
|
||||
|
||||
// Call the `main()` function of the app, or call `runApp` with
|
||||
// any widget you are interested in testing.
|
||||
app.main();
|
||||
}
|
||||
86
testing_app/test_driver/app_test.dart
Normal file
86
testing_app/test_driver/app_test.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing App Driver Tests', () {
|
||||
FlutterDriver driver;
|
||||
|
||||
// Connect to the Flutter driver before running any tests.
|
||||
setUpAll(() async {
|
||||
driver = await FlutterDriver.connect();
|
||||
});
|
||||
|
||||
// Close the connection to the driver after the tests have completed.
|
||||
tearDownAll(() async {
|
||||
if (driver != null) {
|
||||
await driver.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Finding an item in the list', () async {
|
||||
// Create Finders that are used multiple times.
|
||||
final listFinder = find.byType('ListView');
|
||||
final itemFinder = find.byValueKey('text_25');
|
||||
|
||||
// Scroll until the item to be found appears.
|
||||
// Use dxScroll to scroll horizontally and dyScroll
|
||||
// to scroll vertically.
|
||||
await driver.scrollUntilVisible(
|
||||
listFinder,
|
||||
itemFinder,
|
||||
dyScroll: -500.0,
|
||||
);
|
||||
|
||||
// Check if the item contains the correct text.
|
||||
expect(await driver.getText(itemFinder), 'Item 25');
|
||||
});
|
||||
|
||||
test('Testing IconButtons', () async {
|
||||
// Create a finder for the icon.
|
||||
final iconFinder = find.byValueKey('icon_25');
|
||||
|
||||
// Tap onto the icon.
|
||||
await driver.tap(iconFinder);
|
||||
|
||||
// Verify if appropriate message appears.
|
||||
await driver.waitFor(find.text('Added to favorites.'));
|
||||
|
||||
// Wait for the first message to disappear.
|
||||
await Future<void>.delayed(Duration(seconds: 1));
|
||||
|
||||
// Tap the icon again.
|
||||
await driver.tap(iconFinder);
|
||||
|
||||
// Verify if appropriate message appears.
|
||||
await driver.waitFor(find.text('Removed from favorites.'));
|
||||
});
|
||||
|
||||
test('Verifying whether item gets added to favorites', () async {
|
||||
// Tap onto the icon.
|
||||
await driver.tap(find.byValueKey('icon_30'));
|
||||
|
||||
// Tap onto the favorites button on the AppBar.
|
||||
// The Favorites List should appear.
|
||||
await driver.tap(find.text('Favorites'));
|
||||
|
||||
// Check if the added item has appeared in the list.
|
||||
expect(await driver.getText(find.byValueKey('favorites_text_30')),
|
||||
equals('Item 30'));
|
||||
});
|
||||
|
||||
test('Verifying remove button', () async {
|
||||
// Tap onto the remove icon.
|
||||
await driver.tap(find.byValueKey('remove_icon_30'));
|
||||
|
||||
// Verify if it disappears.
|
||||
await driver.waitForAbsent(find.text('Item 30'));
|
||||
|
||||
// Verify if appropriate message appears.
|
||||
await driver.waitFor(find.text('Removed from favorites.'));
|
||||
});
|
||||
});
|
||||
}
|
||||
114
testing_app/test_driver/perf_test.dart
Normal file
114
testing_app/test_driver/perf_test.dart
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing App Performance Tests', () {
|
||||
FlutterDriver driver;
|
||||
|
||||
// Connect to the Flutter driver before running any tests.
|
||||
setUpAll(() async {
|
||||
driver = await FlutterDriver.connect();
|
||||
});
|
||||
|
||||
// Close the connection to the driver after the tests have completed.
|
||||
tearDownAll(() async {
|
||||
if (driver != null) {
|
||||
await driver.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Scrolling test', () async {
|
||||
// Create Finders that are used multiple times.
|
||||
final listFinder = find.byType('ListView');
|
||||
|
||||
// Record a performance profile as the app scrolls through
|
||||
// the list of items.
|
||||
final scrollingTimeline = await driver.traceAction(() async {
|
||||
// Quickly scroll all the way down.
|
||||
// Use dxScroll to scroll horizontally and dyScroll
|
||||
// to scroll vertically.
|
||||
await driver.scroll(listFinder, 0, -7000, Duration(seconds: 1));
|
||||
|
||||
// Quickly scroll back up all the way.
|
||||
await driver.scroll(listFinder, 0, 7000, Duration(seconds: 1));
|
||||
});
|
||||
|
||||
// Convert the Timeline into a TimelineSummary that's easier to
|
||||
// read and understand.
|
||||
final scrollingSummary = TimelineSummary.summarize(scrollingTimeline);
|
||||
|
||||
// Then, save the summary to disk.
|
||||
// Results will be stored in the file 'build/scrolling.timeline.json'.
|
||||
await scrollingSummary.writeSummaryToFile('scrolling', pretty: true);
|
||||
|
||||
// Write the entire timeline to disk in a json format.
|
||||
// Results will be stored in
|
||||
// the file 'build/scrolling.timeline_summary.json'.
|
||||
// This file can be opened in the Chrome browser's tracing tools
|
||||
// found by navigating to chrome://tracing.
|
||||
await scrollingSummary.writeTimelineToFile('scrolling', pretty: true);
|
||||
});
|
||||
|
||||
test('Favorites operations test', () async {
|
||||
// Record a performance profile as operations are performed
|
||||
// on the favorites list.
|
||||
final operationsTimeline = await driver.traceAction(() async {
|
||||
// Create a list of icon keys.
|
||||
final iconKeys = [
|
||||
'icon_0',
|
||||
'icon_1',
|
||||
'icon_2',
|
||||
];
|
||||
|
||||
// Add first three items to favorites.
|
||||
for (var icon in iconKeys) {
|
||||
// Tap onto the icon.
|
||||
await driver.tap(find.byValueKey(icon));
|
||||
|
||||
// Verify if appropriate message appears.
|
||||
await driver.waitFor(find.text('Added to favorites.'));
|
||||
}
|
||||
|
||||
// Tap onto the favorites button on the AppBar.
|
||||
// The Favorites List should appear.
|
||||
await driver.tap(find.text('Favorites'));
|
||||
|
||||
final removeIconKeys = [
|
||||
'remove_icon_0',
|
||||
'remove_icon_1',
|
||||
'remove_icon_2',
|
||||
];
|
||||
|
||||
// Remove all the items from favorites.
|
||||
for (final iconKey in removeIconKeys) {
|
||||
// Tap onto the remove icon.
|
||||
await driver.tap(find.byValueKey(iconKey));
|
||||
|
||||
// Verify if appropriate message appears.
|
||||
await driver.waitFor(find.text('Removed from favorites.'));
|
||||
}
|
||||
});
|
||||
|
||||
// Convert the Timeline into a TimelineSummary.
|
||||
final operationsSummary = TimelineSummary.summarize(operationsTimeline);
|
||||
|
||||
// Then, save the summary to disk.
|
||||
// Results will be stored in
|
||||
// the file 'build/favorites_operations.timeline.json'.
|
||||
await operationsSummary.writeSummaryToFile('favorites_operations',
|
||||
pretty: true);
|
||||
|
||||
// Write the entire timeline to disk in a json format.
|
||||
// Results will be stored in
|
||||
// the file 'build/favorites_operations.timeline_summary.json'.
|
||||
// This file can be opened in the Chrome browser's tracing tools
|
||||
// found by navigating to chrome://tracing.
|
||||
await operationsSummary.writeTimelineToFile('favorites_operations',
|
||||
pretty: true);
|
||||
});
|
||||
});
|
||||
}
|
||||
42
testing_app/test_driver/state_mgmt.dart
Normal file
42
testing_app/test_driver/state_mgmt.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
import 'package:testing_app/screens/favorites.dart';
|
||||
|
||||
void main() {
|
||||
// Create a reference to the state
|
||||
// so that we can perform operations on it manually.
|
||||
Favorites favoritesList;
|
||||
|
||||
// This line enables the extension.
|
||||
enableFlutterDriverExtension(
|
||||
// This handler gets data from the test file
|
||||
// and then we use it to perform operations on the state object.
|
||||
handler: (command) {
|
||||
if (command == 'ADD') {
|
||||
favoritesList.add(30);
|
||||
} else if (command == 'REMOVE') {
|
||||
favoritesList.remove(30);
|
||||
}
|
||||
return Future.delayed(Duration(seconds: 1), () => 'DONE');
|
||||
});
|
||||
|
||||
// Call the `main()` function of the app, or call `runApp` with
|
||||
// any widget you are interested in testing.
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) {
|
||||
favoritesList = Favorites();
|
||||
return favoritesList;
|
||||
},
|
||||
child: MaterialApp(
|
||||
home: FavoritesPage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
45
testing_app/test_driver/state_mgmt_test.dart
Normal file
45
testing_app/test_driver/state_mgmt_test.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing App State Management Tests', () {
|
||||
FlutterDriver driver;
|
||||
|
||||
// Connect to the Flutter driver before running any tests.
|
||||
setUpAll(() async {
|
||||
driver = await FlutterDriver.connect();
|
||||
});
|
||||
|
||||
// Close the connection to the driver after the tests have completed.
|
||||
tearDownAll(() async {
|
||||
if (driver != null) {
|
||||
await driver.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Verifying add method', () async {
|
||||
/// This function is used for communication between
|
||||
/// the driver and the application.
|
||||
/// The [enableFlutterDriverExtension] call must have a [DataHandler]
|
||||
/// callback to handle these requests.
|
||||
/// We are currently using it to perform operations on the provider.
|
||||
/// Specifically, send 'ADD' command to the handler in this case.
|
||||
await driver.requestData('ADD');
|
||||
|
||||
// Check if the new item apppears in the list.
|
||||
await driver.waitFor(find.text('Item 30'));
|
||||
});
|
||||
|
||||
test('Verifying remove method', () async {
|
||||
/// Peform remove operation on the provider using the [DataHandler].
|
||||
await driver.requestData('REMOVE');
|
||||
|
||||
// Verify if it disappears.
|
||||
await driver.waitForAbsent(find.text('Item 30'));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user