Skip to content

Commit d3892cf

Browse files
authored
Refactor puppeteer tests to use Worker type (#1809)
1 parent e39506e commit d3892cf

File tree

4 files changed

+89
-49
lines changed

4 files changed

+89
-49
lines changed

dwds/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dev_dependencies:
5252
js: ^0.6.4
5353
lints: ^2.0.0
5454
pubspec_parse: ^1.2.0
55-
puppeteer: ^2.17.0
55+
puppeteer: ^2.18.0
5656
stream_channel: ^2.1.0
5757
test: ^1.21.1
5858
webdriver: ^3.0.0

dwds/test/puppeteer/extension_test.dart

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ import 'test_utils.dart';
2323

2424
final context = TestContext();
2525

26-
// Note: The following delay is required to reduce flakiness. It makes
27-
// sure the service worker execution context is ready.
28-
const executionContextDelay = 1;
29-
3026
void main() async {
31-
late Target serviceWorkerTarget;
27+
late Worker worker;
3228
late Browser browser;
3329
late String extensionPath;
3430

@@ -58,13 +54,13 @@ void main() async {
5854
],
5955
);
6056

61-
serviceWorkerTarget = await browser
57+
final serviceWorkerTarget = await browser
6258
.waitForTarget((target) => target.type == 'service_worker');
59+
worker = (await serviceWorkerTarget.worker)!;
6360
});
6461

6562
tearDown(() async {
66-
final worker = (await serviceWorkerTarget.worker)!;
67-
await Future.delayed(Duration(seconds: executionContextDelay));
63+
await workerEvalDelay();
6864
await worker.evaluate(_clearStorageJs());
6965
});
7066

@@ -78,17 +74,15 @@ void main() async {
7874
// Navigate to the Dart app:
7975
final appTab =
8076
await navigateToPage(browser, url: appUrl, isNew: true);
81-
final worker = (await serviceWorkerTarget.worker)!;
82-
await Future.delayed(Duration(seconds: executionContextDelay));
8377
// Verify that we have debug info for the Dart app:
84-
final tabIdForAppJs = _tabIdForTabJs(appUrl);
85-
final appTabId = (await worker.evaluate(tabIdForAppJs)) as int;
78+
await workerEvalDelay();
79+
final appTabId = await _getTabId(appUrl, worker: worker);
8680
final debugInfoKey = '$appTabId-debugInfo';
87-
final storageObj = await worker.evaluate(
88-
_fetchStorageObjJs(debugInfoKey, storageArea: 'session'));
89-
final json = storageObj[debugInfoKey];
90-
final debugInfo =
91-
serializers.deserialize(jsonDecode(json)) as DebugInfo;
81+
final debugInfo = await _fetchStorageObj<DebugInfo>(
82+
debugInfoKey,
83+
storageArea: 'session',
84+
worker: worker,
85+
);
9286
expect(debugInfo.appId, isNotNull);
9387
expect(debugInfo.appEntrypointPath, isNotNull);
9488
expect(debugInfo.appInstanceId, isNotNull);
@@ -116,12 +110,11 @@ void main() async {
116110
// Close the settings tab:
117111
await settingsTab.close();
118112
// Check that is has been saved in local storage:
119-
final worker = (await serviceWorkerTarget.worker)!;
120-
final storageObj = await worker.evaluate(
121-
_fetchStorageObjJs('devToolsOpener', storageArea: 'local'));
122-
final json = storageObj['devToolsOpener'];
123-
final devToolsOpener =
124-
serializers.deserialize(jsonDecode(json)) as DevToolsOpener;
113+
final devToolsOpener = await _fetchStorageObj<DevToolsOpener>(
114+
'devToolsOpener',
115+
storageArea: 'local',
116+
worker: worker,
117+
);
125118
expect(devToolsOpener.newWindow, isTrue);
126119
});
127120

@@ -131,22 +124,21 @@ void main() async {
131124
final appUrl = context.appUrl;
132125
final devToolsUrlFragment =
133126
useSse ? 'debugger?uri=sse' : 'debugger?uri=ws';
134-
final windowIdForAppJs = _windowIdForTabJs(appUrl);
135127
// Navigate to the Dart app:
136128
final appTab =
137129
await navigateToPage(browser, url: appUrl, isNew: true);
138130
// Click on the Dart Debug Extension icon:
139-
final worker = (await serviceWorkerTarget.worker)!;
140-
await Future.delayed(Duration(seconds: executionContextDelay));
141-
await worker.evaluate(clickIconJs);
131+
await workerEvalDelay();
132+
await clickOnExtensionIcon(worker);
142133
// Verify the extension opened the Dart docs in the same window:
143134
var devToolsTabTarget = await browser.waitForTarget(
144135
(target) => target.url.contains(devToolsUrlFragment));
145136
final devToolsPage = await devToolsTabTarget.page;
146-
final windowIdForDevToolsJs = _windowIdForTabJs(devToolsPage.url!);
147-
var devToolsWindowId =
148-
(await worker.evaluate(windowIdForDevToolsJs)) as int?;
149-
var appWindowId = (await worker.evaluate(windowIdForAppJs)) as int?;
137+
var devToolsWindowId = await _getWindowId(
138+
devToolsPage.url!,
139+
worker: worker,
140+
);
141+
var appWindowId = await _getWindowId(appUrl, worker: worker);
150142
expect(devToolsWindowId == appWindowId, isTrue);
151143
// Close the DevTools tab:
152144
var devToolsTab = await devToolsTabTarget.page;
@@ -168,13 +160,15 @@ void main() async {
168160
// Navigate to the Dart app:
169161
await navigateToPage(browser, url: appUrl);
170162
// Click on the Dart Debug Extension icon:
171-
await worker.evaluate(clickIconJs);
163+
await clickOnExtensionIcon(worker);
172164
// Verify the extension opened DevTools in a different window:
173165
devToolsTabTarget = await browser.waitForTarget(
174166
(target) => target.url.contains(devToolsUrlFragment));
175-
devToolsWindowId =
176-
(await worker.evaluate(windowIdForDevToolsJs)) as int?;
177-
appWindowId = (await worker.evaluate(windowIdForAppJs)) as int?;
167+
devToolsWindowId = await _getWindowId(
168+
devToolsPage.url!,
169+
worker: worker,
170+
);
171+
appWindowId = await _getWindowId(appUrl, worker: worker);
178172
expect(devToolsWindowId == appWindowId, isFalse);
179173
// Close the DevTools tab:
180174
devToolsTab = await devToolsTabTarget.page;
@@ -186,6 +180,35 @@ void main() async {
186180
});
187181
}
188182

183+
Future<int> _getTabId(
184+
String url, {
185+
required Worker worker,
186+
}) async {
187+
final jsExpression = _tabIdForTabJs(url);
188+
return (await worker.evaluate(jsExpression)) as int;
189+
}
190+
191+
Future<int?> _getWindowId(
192+
String url, {
193+
required Worker worker,
194+
}) async {
195+
final jsExpression = _windowIdForTabJs(url);
196+
return (await worker.evaluate(jsExpression)) as int?;
197+
}
198+
199+
Future<T> _fetchStorageObj<T>(
200+
String storageKey, {
201+
required String storageArea,
202+
required Worker worker,
203+
}) async {
204+
final storageObj = await worker.evaluate(_fetchStorageObjJs(
205+
storageKey,
206+
storageArea: storageArea,
207+
));
208+
final json = storageObj[storageKey];
209+
return serializers.deserialize(jsonDecode(json)) as T;
210+
}
211+
189212
String _tabIdForTabJs(String tabUrl) {
190213
return '''
191214
async () => {

dwds/test/puppeteer/lifeline_test.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Timeout(Duration(minutes: 10))
5+
@Timeout(Duration(minutes: 12))
66
@Skip('https://github.com/dart-lang/webdev/issues/1788')
77
import 'dart:async';
88

@@ -15,7 +15,7 @@ import 'test_utils.dart';
1515
final context = TestContext();
1616

1717
void main() async {
18-
late Target serviceWorkerTarget;
18+
late Worker worker;
1919
late Browser browser;
2020
late String extensionPath;
2121

@@ -38,8 +38,9 @@ void main() async {
3838
],
3939
);
4040

41-
serviceWorkerTarget = await browser
41+
final serviceWorkerTarget = await browser
4242
.waitForTarget((target) => target.type == 'service_worker');
43+
worker = (await serviceWorkerTarget.worker)!;
4344
});
4445

4546
tearDownAll(() async {
@@ -50,14 +51,10 @@ void main() async {
5051
// Navigate to the Dart app:
5152
final appTab =
5253
await navigateToPage(browser, url: context.appUrl, isNew: true);
53-
// Click on the Dart Debug Extension icon:
54-
final worker = (await serviceWorkerTarget.worker)!;
55-
// Note: The following delay is required to reduce flakiness (it makes
56-
// sure the execution context is ready):
57-
await Future.delayed(Duration(seconds: 1));
54+
await workerEvalDelay();
5855
// Initiate listeners for the port connection event and the subsequent
5956
// reconnection logs:
60-
final portConnectionPromise = worker.evaluate<bool>(_portConnectionJs);
57+
final portConnectionFuture = _connectToPort(worker);
6158
appTab.onConsole.listen((ConsoleMessage message) {
6259
final messageText = message.text ?? '';
6360
if (messageText
@@ -66,19 +63,28 @@ void main() async {
6663
}
6764
});
6865
// Click on the Dart Debug Extension icon to intiate a debug session:
69-
await worker.evaluate(clickIconJs);
70-
final connectedToPort = await portConnectionPromise;
66+
await clickOnExtensionIcon(worker);
67+
final connectedToPort = await portConnectionFuture;
7168
// Verify that we have connected to the port:
7269
expect(connectedToPort, isTrue);
7370
expect(connectionCount, equals(1));
7471
// Wait for a little over 5 minutes, and verify that we have reconnected
7572
// to the port again:
76-
await Future.delayed(Duration(minutes: 5) + Duration(seconds: 15));
73+
await _reconnectToPortDelay();
7774
expect(connectionCount, equals(2));
7875
});
7976
});
8077
}
8178

79+
Future<void> _reconnectToPortDelay() async {
80+
await Future.delayed(Duration(minutes: 5) + Duration(seconds: 15));
81+
return;
82+
}
83+
84+
Future<bool?> _connectToPort(Worker worker) async {
85+
return worker.evaluate<bool>(_portConnectionJs);
86+
}
87+
8288
final _portConnectionJs = '''
8389
async () => {
8490
return new Promise((resolve, reject) => {

dwds/test/puppeteer/test_utils.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ Future<String> buildDebugExtension() async {
2424
return '$extensionDir/compiled';
2525
}
2626

27+
Future<void> clickOnExtensionIcon(Worker worker) async {
28+
return worker.evaluate(_clickIconJs);
29+
}
30+
31+
// Note: The following delay is required to reduce flakiness. It makes
32+
// sure the service worker execution context is ready.
33+
Future<void> workerEvalDelay() async {
34+
await Future.delayed(Duration(seconds: 1));
35+
return;
36+
}
37+
2738
Future<Page> navigateToPage(
2839
Browser browser, {
2940
required String url,
@@ -60,7 +71,7 @@ Future<Page> _getPageForUrl(Browser browser, {required String url}) {
6071
return pageTarget.page;
6172
}
6273

63-
final clickIconJs = '''
74+
final _clickIconJs = '''
6475
async () => {
6576
const activeTabs = await chrome.tabs.query({ active: true });
6677
const tab = activeTabs[0];

0 commit comments

Comments
 (0)