This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' show File;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
const String _kPathParent = 'test_driver/goldens/';
|
||||
|
||||
/// The utility class that helps test cases to tests screenshots with a [FlutterDriver].
|
||||
@immutable
|
||||
class DriverScreenShotTester {
|
||||
/// Constructs a [DriverScreenShotTester].
|
||||
///
|
||||
/// All the parameters are required and must not be null.
|
||||
const DriverScreenShotTester({
|
||||
@required this.testName,
|
||||
@required this.driver,
|
||||
@required this.deviceModel,
|
||||
}) : assert(testName != null),
|
||||
assert(driver != null),
|
||||
assert(deviceModel != null);
|
||||
|
||||
/// The name of the test.
|
||||
///
|
||||
/// It needs to match the folder name which the goldens resides under `test_driver/goldens`.
|
||||
final String testName;
|
||||
|
||||
/// The `FlutterDriver` used to take the screenshots.
|
||||
final FlutterDriver driver;
|
||||
|
||||
/// The device model of the device that the test is running on.
|
||||
final String deviceModel;
|
||||
|
||||
/// Compares `screenshot` to the corresponding golden image. Returns true if they match.
|
||||
///
|
||||
/// The golden image should exists at `test_driver/goldens/<testName>/<deviceModel>.png`
|
||||
/// prior to this call.
|
||||
Future<bool> compareScreenshots(List<int> screenshot) async {
|
||||
final File file = File(_getImageFilePath());
|
||||
final List<int> matcher = await file.readAsBytes();
|
||||
final Function listEquals = const ListEquality<int>().equals;
|
||||
return listEquals(screenshot, matcher);
|
||||
}
|
||||
|
||||
/// Returns a bytes representation of a screenshot on the current screen.
|
||||
Future<List<int>> getScreenshotAsBytes() async {
|
||||
return await driver.screenshot();
|
||||
}
|
||||
|
||||
/// Save the `screenshot` as a golden image.
|
||||
///
|
||||
/// The path of the image is defined as:
|
||||
/// `test_driver/goldens/<testName>/<deviceModel>.png`
|
||||
///
|
||||
/// Can be used when recording the golden for the first time.
|
||||
Future<void> saveScreenshot(List<int> screenshot) async {
|
||||
final File file = File(_getImageFilePath());
|
||||
if (!file.existsSync()) {
|
||||
await file.writeAsBytes(screenshot);
|
||||
}
|
||||
}
|
||||
|
||||
String _getImageFilePath() {
|
||||
return path.joinAll(<String>[_kPathParent, testName, deviceModel + '.png']);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
@@ -0,0 +1,36 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// 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';
|
||||
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
|
||||
import './driver_screenshot_tester.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
FlutterDriver driver;
|
||||
String deviceModel;
|
||||
|
||||
setUpAll(() async {
|
||||
driver = await FlutterDriver.connect();
|
||||
deviceModel = await driver.requestData('device_model');
|
||||
});
|
||||
|
||||
tearDownAll(() => driver.close());
|
||||
|
||||
test('A page with an image screenshot', () async {
|
||||
|
||||
final SerializableFinder imagePageListTile =
|
||||
find.byValueKey('image_page');
|
||||
await driver.waitFor(imagePageListTile);
|
||||
await driver.tap(imagePageListTile);
|
||||
await driver.waitFor(find.byValueKey('red_square_image'));
|
||||
await driver.waitUntilNoTransientCallbacks();
|
||||
|
||||
final DriverScreenShotTester tester = DriverScreenShotTester(testName: 'red_square_image', deviceModel: deviceModel, driver: driver);
|
||||
final List<int> screenShot = await tester.getScreenshotAsBytes();
|
||||
final bool compareResult = await tester.compareScreenshots(screenShot);
|
||||
expect(compareResult, true);
|
||||
await driver.tap(find.byTooltip('Back'));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user