From 4cba002a825bfb3ce28e08020a12201c2d08fb2a Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Tue, 14 Feb 2023 14:51:46 -0800 Subject: [PATCH 1/2] Add logging when tests fail --- dwds/debug_extension_mv3/web/chrome_api.dart | 2 +- dwds/debug_extension_mv3/web/utils.dart | 16 ++-- dwds/test/puppeteer/extension_test.dart | 42 +++-------- dwds/test/puppeteer/test_utils.dart | 79 +++++++++++++++++++- 4 files changed, 94 insertions(+), 45 deletions(-) diff --git a/dwds/debug_extension_mv3/web/chrome_api.dart b/dwds/debug_extension_mv3/web/chrome_api.dart index 5039db583..9d1a913f0 100644 --- a/dwds/debug_extension_mv3/web/chrome_api.dart +++ b/dwds/debug_extension_mv3/web/chrome_api.dart @@ -230,7 +230,7 @@ class MessageSender { @JS() @anonymous class Scripting { - external executeScript(InjectDetails details, Function? callback); + external Object executeScript(InjectDetails details); } @JS() diff --git a/dwds/debug_extension_mv3/web/utils.dart b/dwds/debug_extension_mv3/web/utils.dart index d122140ae..eff1920cb 100644 --- a/dwds/debug_extension_mv3/web/utils.dart +++ b/dwds/debug_extension_mv3/web/utils.dart @@ -68,16 +68,12 @@ Future removeTab(int tabId) { return completer.future; } -Future injectScript(String scriptName, {required int tabId}) { - final completer = Completer(); - chrome.scripting.executeScript( - InjectDetails( - target: Target(tabId: tabId), - files: [scriptName], - ), allowInterop(() { - completer.complete(true); - })); - return completer.future; +Future injectScript(String scriptName, {required int tabId}) async { + await promiseToFuture(chrome.scripting.executeScript(InjectDetails( + target: Target(tabId: tabId), + files: [scriptName], + ))); + return true; } void onExtensionIconClicked(void Function(Tab) callback) { diff --git a/dwds/test/puppeteer/extension_test.dart b/dwds/test/puppeteer/extension_test.dart index f1cc393f7..4f5416d62 100644 --- a/dwds/test/puppeteer/extension_test.dart +++ b/dwds/test/puppeteer/extension_test.dart @@ -58,9 +58,7 @@ void main() async { }); tearDown(() async { - await workerEvalDelay(); - await worker.evaluate(_clearStorageJs()); - await workerEvalDelay(); + await tearDownHelper(worker: worker); }); tearDownAll(() async { @@ -205,7 +203,6 @@ void main() async { // Click on the Dart Debug Extension icon: await workerEvalDelay(); await clickOnExtensionIcon(worker); - print('clicked, waiting for devtools'); // Wait for DevTools to open: final devToolsTabTarget = await browser.waitForTarget( (target) => target.url.contains(devToolsUrlFragment)); @@ -344,8 +341,7 @@ void main() async { }); tearDown(() async { - await workerEvalDelay(); - await worker.evaluate(_clearStorageJs()); + await tearDownHelper(worker: worker); }); tearDownAll(() async { @@ -424,7 +420,7 @@ void main() async { setUp(() async { for (final page in await browser.pages) { - await page.close(); + await page.close().catchError((_) {}); } appTab = await navigateToPage( browser, @@ -434,9 +430,8 @@ void main() async { }); tearDown(() async { - await workerEvalDelay(); - await worker.evaluate(_clearStorageJs()); - await workerEvalDelay(); + await tearDownHelper(worker: worker); + }); tearDownAll(() async { @@ -461,7 +456,7 @@ void main() async { test('the correct extension panels are added to Chrome DevTools', () async { - final chromeDevToolsPage = await _getChromeDevToolsPage(browser); + final chromeDevToolsPage = await getChromeDevToolsPage(browser); // There are no hooks for when a panel is added to Chrome DevTools, // therefore we rely on a slight delay: await Future.delayed(Duration(seconds: 1)); @@ -494,7 +489,7 @@ void main() async { test('Dart DevTools is embedded for debug session lifetime', () async { - final chromeDevToolsPage = await _getChromeDevToolsPage(browser); + final chromeDevToolsPage = await getChromeDevToolsPage(browser); // There are no hooks for when a panel is added to Chrome DevTools, // therefore we rely on a slight delay: await Future.delayed(Duration(seconds: 1)); @@ -541,7 +536,7 @@ void main() async { test('The Dart DevTools IFRAME has the correct query parameters', () async { - final chromeDevToolsPage = await _getChromeDevToolsPage(browser); + final chromeDevToolsPage = await getChromeDevToolsPage(browser); // There are no hooks for when a panel is added to Chrome DevTools, // therefore we rely on a slight delay: await Future.delayed(Duration(seconds: 1)); @@ -618,9 +613,7 @@ void main() async { }); tearDown(() async { - await workerEvalDelay(); - await worker.evaluate(_clearStorageJs()); - await workerEvalDelay(); + await tearDownHelper(worker: worker); }); tearDownAll(() async { @@ -672,13 +665,6 @@ Future _clickLaunchButton( } } -Future _getChromeDevToolsPage(Browser browser) async { - final chromeDevToolsTarget = browser.targets - .firstWhere((target) => target.url.startsWith('devtools://devtools')); - chromeDevToolsTarget.type = 'page'; - return await chromeDevToolsTarget.page; -} - Future _getPanelPage( Browser browser, { required Panel panel, @@ -787,16 +773,6 @@ String _fetchStorageObjJs( '''; } -String _clearStorageJs() { - return ''' - async () => { - await chrome.storage.local.clear(); - await chrome.storage.session.clear(); - return true; - } -'''; -} - String _getNotifications() { return ''' async () => { diff --git a/dwds/test/puppeteer/test_utils.dart b/dwds/test/puppeteer/test_utils.dart index 0c8c93e98..579ca6043 100644 --- a/dwds/test/puppeteer/test_utils.dart +++ b/dwds/test/puppeteer/test_utils.dart @@ -7,10 +7,19 @@ import 'dart:io'; import 'package:path/path.dart' as p; import 'package:puppeteer/puppeteer.dart'; +import 'package:test/test.dart'; import '../fixtures/context.dart'; import '../fixtures/utilities.dart'; +enum ConsoleSource { + worker, + devTools, +} + +final _workerLogs = []; +final _devToolsLogs = []; + Future buildDebugExtension() async { final extensionDir = absolutePath(pathFromDwds: 'debug_extension_mv3'); await Process.run( @@ -52,10 +61,61 @@ Future setUpExtensionTest( ); } +Future tearDownHelper({required Worker worker}) async { + _logConsoleMsgsOnFailure(); + _workerLogs.clear(); + _devToolsLogs.clear(); + await worker.evaluate(_clearStorageJs).catchError((_) {}); +} + Future getServiceWorker(Browser browser) async { final serviceWorkerTarget = await browser.waitForTarget((target) => target.type == 'service_worker'); - return (await serviceWorkerTarget.worker)!; + final worker = (await serviceWorkerTarget.worker)!; + return Worker( + worker.client, + worker.url, + onConsoleApiCalled: (type, jsHandles, _) { + for (var handle in jsHandles) { + saveConsoleMsg( + source: ConsoleSource.worker, type: '$type', msg: '$handle'); + } + }, + onExceptionThrown: null, + ); +} + +Future getChromeDevToolsPage(Browser browser) async { + final chromeDevToolsTarget = browser.targets + .firstWhere((target) => target.url.startsWith('devtools://devtools')); + chromeDevToolsTarget.type = 'page'; + final chromeDevToolsPage = await chromeDevToolsTarget.page; + chromeDevToolsPage.onConsole.listen((msg) { + saveConsoleMsg( + source: ConsoleSource.devTools, + type: '${msg.type}', + msg: msg.text ?? '', + ); + }); + return chromeDevToolsPage; +} + +void saveConsoleMsg({ + required ConsoleSource source, + required String type, + required String msg, +}) { + if (msg.isEmpty) return; + final consiseMsg = msg.startsWith('JSHandle:') ? msg.substring(9) : msg; + final formatted = 'console.$type: $consiseMsg'; + switch (source) { + case ConsoleSource.worker: + _workerLogs.add(formatted); + break; + case ConsoleSource.devTools: + _devToolsLogs.add(formatted); + break; + } } Future clickOnExtensionIcon(Worker worker) async { @@ -96,6 +156,15 @@ String getExtensionOrigin(Browser browser) { return '$chromeExtension//$extensionId'; } +void _logConsoleMsgsOnFailure() { + if (_workerLogs.isNotEmpty) { + printOnFailure(['Service Worker logs:', ..._workerLogs].join('\n')); + } + if (_devToolsLogs.isNotEmpty) { + printOnFailure(['Chrome DevTools logs:', ..._devToolsLogs].join('\n')); + } +} + Iterable _getUrlsInBrowser(Browser browser) { return browser.targets.map((target) => target.url); } @@ -112,3 +181,11 @@ final _clickIconJs = ''' chrome.action.onClicked.dispatch(tab); } '''; + +final _clearStorageJs = ''' + async () => { + await chrome.storage.local.clear(); + await chrome.storage.session.clear(); + return true; + } +'''; From b8859c902202e8b33b4a642b6d2cbaaa81d00edf Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Tue, 14 Feb 2023 14:58:14 -0800 Subject: [PATCH 2/2] Format --- dwds/test/puppeteer/extension_test.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/dwds/test/puppeteer/extension_test.dart b/dwds/test/puppeteer/extension_test.dart index 4f5416d62..1c74c92c5 100644 --- a/dwds/test/puppeteer/extension_test.dart +++ b/dwds/test/puppeteer/extension_test.dart @@ -431,7 +431,6 @@ void main() async { tearDown(() async { await tearDownHelper(worker: worker); - }); tearDownAll(() async {