|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | + |
| 9 | +import 'common.dart'; |
| 10 | + |
| 11 | +/// Manager for sending [WebDriverAction]s to driver side. |
| 12 | +/// |
| 13 | +/// See: [WebDriverCommandManager]. |
| 14 | +DriverCommandManager get driverCommandManager => |
| 15 | + _singletonWebDriverCommandManager; |
| 16 | + |
| 17 | +/// WebDriverCommandManager singleton. |
| 18 | +final WebDriverCommandManager _singletonWebDriverCommandManager = |
| 19 | + WebDriverCommandManager(); |
| 20 | + |
| 21 | +/// Enables Web Driver commands for `integration_tests`. |
| 22 | +/// |
| 23 | +/// Manages communication between `integration_tests` and the `driver_tests`. |
| 24 | +/// |
| 25 | +/// Tests can execute an Web Driver actions such as `screenshot` using browsers' |
| 26 | +/// WebDriver APIs. |
| 27 | +/// |
| 28 | +/// See: https://www.w3.org/TR/webdriver/ |
| 29 | +class WebDriverCommandManager extends DriverCommandManager { |
| 30 | + /// Tests will put the action requests from WebDriver to this pipe. |
| 31 | + Completer<WebDriverAction> webDriverActionPipe = Completer<WebDriverAction>(); |
| 32 | + |
| 33 | + /// Updated when WebDriver completes the request by the test method. |
| 34 | + /// |
| 35 | + /// For example, a test method will ask for a screenshot by calling |
| 36 | + /// `takeScreenshot`. When this screenshot is taken [driverActionComplete] |
| 37 | + /// will complete. |
| 38 | + Completer<bool> driverActionComplete = Completer<bool>(); |
| 39 | + |
| 40 | + /// Takes screenshot using WebDriver screenshot command. |
| 41 | + /// |
| 42 | + /// Only works on Web when tests are run via `flutter driver` command. |
| 43 | + /// |
| 44 | + /// See: https://www.w3.org/TR/webdriver/#screen-capture |
| 45 | + Future<void> takeScreenshot(String screenshot_name) async { |
| 46 | + await _webDriverCommand(WebDriverAction.screenshot(screenshot_name)); |
| 47 | + } |
| 48 | + |
| 49 | + Future<void> _webDriverCommand(WebDriverAction command) async { |
| 50 | + try { |
| 51 | + webDriverActionPipe.complete(Future.value(command)); |
| 52 | + try { |
| 53 | + final bool awaitCommand = await driverActionComplete.future; |
| 54 | + if (!awaitCommand) { |
| 55 | + throw Exception('Web Driver Command failed: ${command.type}'); |
| 56 | + } |
| 57 | + } catch (e) { |
| 58 | + throw Exception( |
| 59 | + 'Web Driver Command failed: ${command.type} with ' 'exception $e'); |
| 60 | + } |
| 61 | + } finally { |
| 62 | + // Reset the completer and release the lock. |
| 63 | + driverActionComplete = Completer<bool>(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// The callback function to response the driver side input. |
| 68 | + /// |
| 69 | + /// Provides a handshake mechanism for executing [WebDriverAction]s on the |
| 70 | + /// driver side. |
| 71 | + Future<Map<String, dynamic>> callbackWithDriverCommands( |
| 72 | + Map<String, String> params, IntegrationTestResults testRunner) async { |
| 73 | + final String command = params['command']; |
| 74 | + // Test status is added as an exta message. |
| 75 | + final String extraMessage = params['message'] ?? ''; |
| 76 | + Map<String, String> response; |
| 77 | + switch (command) { |
| 78 | + case 'request_data': |
| 79 | + // If not extra message is added continue as normal. |
| 80 | + if (extraMessage.isEmpty) { |
| 81 | + final bool allTestsPassed = await testRunner.allTestsPassed.future; |
| 82 | + response = <String, String>{ |
| 83 | + 'message': allTestsPassed |
| 84 | + ? Response.allTestsPassed(data: testRunner.reportData).toJson() |
| 85 | + : Response.someTestsFailed( |
| 86 | + testRunner.failureMethodsDetails, |
| 87 | + data: testRunner.reportData, |
| 88 | + ).toJson(), |
| 89 | + }; |
| 90 | + } else { |
| 91 | + // If Test status is `wait_on_webdriver_command` send the first |
| 92 | + // command in the `commandPipe` to the tests. |
| 93 | + if (extraMessage == '${TestStatus.waitOnWebdriverCommand}') { |
| 94 | + final WebDriverAction action = await webDriverActionPipe.future; |
| 95 | + switch (action.type) { |
| 96 | + case WebDriverActionTypes.screenshot: |
| 97 | + final Map<String, dynamic> data = Map.from(action.values); |
| 98 | + data.addAll( |
| 99 | + WebDriverAction.typeToMap(WebDriverActionTypes.screenshot)); |
| 100 | + response = <String, String>{ |
| 101 | + 'message': Response.webDriverCommand(data: data).toJson(), |
| 102 | + }; |
| 103 | + break; |
| 104 | + case WebDriverActionTypes.noop: |
| 105 | + final Map<String, dynamic> data = Map(); |
| 106 | + data.addAll( |
| 107 | + WebDriverAction.typeToMap(WebDriverActionTypes.noop)); |
| 108 | + response = <String, String>{ |
| 109 | + 'message': Response.webDriverCommand(data: data).toJson(), |
| 110 | + }; |
| 111 | + break; |
| 112 | + default: |
| 113 | + throw UnimplementedError('$command is not implemented'); |
| 114 | + } |
| 115 | + } |
| 116 | + // Tests will send `webdriver_command_complete` status after |
| 117 | + // WebDriver completes an action. |
| 118 | + else if (extraMessage == '${TestStatus.webdriverCommandComplete}') { |
| 119 | + final Map<String, dynamic> data = Map(); |
| 120 | + data.addAll(WebDriverAction.typeToMap(WebDriverActionTypes.ack)); |
| 121 | + response = <String, String>{ |
| 122 | + 'message': Response.webDriverCommand(data: data).toJson(), |
| 123 | + }; |
| 124 | + driverActionComplete.complete(Future.value(true)); |
| 125 | + webDriverActionPipe = Completer<WebDriverAction>(); |
| 126 | + } else { |
| 127 | + throw UnimplementedError('$command is not implemented'); |
| 128 | + } |
| 129 | + } |
| 130 | + break; |
| 131 | + case 'get_health': |
| 132 | + response = <String, String>{'status': 'ok'}; |
| 133 | + break; |
| 134 | + default: |
| 135 | + throw UnimplementedError('$command is not implemented'); |
| 136 | + } |
| 137 | + return <String, dynamic>{ |
| 138 | + 'isError': false, |
| 139 | + 'response': response, |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + @override |
| 144 | + void cleanup() { |
| 145 | + if (!webDriverActionPipe.isCompleted) { |
| 146 | + webDriverActionPipe |
| 147 | + .complete(Future<WebDriverAction>.value(WebDriverAction.noop())); |
| 148 | + } |
| 149 | + |
| 150 | + if (!driverActionComplete.isCompleted) { |
| 151 | + driverActionComplete.complete(Future<bool>.value(false)); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments