forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulation-helpers.ts
207 lines (176 loc) · 7.21 KB
/
emulation-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2020 The Chromium 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 type * as puppeteer from 'puppeteer-core';
import {
$,
click,
clickElement,
getBrowserAndPages,
goToResource,
waitFor,
waitForFunction,
} from '../../shared/helper.js';
import {
reloadDevTools,
} from './cross-tool-helper.js';
const DEVICE_TOOLBAR_TOGGLER_SELECTOR = '[aria-label="Toggle device toolbar"]';
const DEVICE_TOOLBAR_SELECTOR = '.device-mode-toolbar';
const DEVICE_TOOLBAR_OPTIONS_SELECTOR = '.device-mode-toolbar .device-mode-toolbar-options';
const MEDIA_QUERY_INSPECTOR_SELECTOR = '.media-inspector-view';
const DEVICE_LIST_DROPDOWN_SELECTOR = '.toolbar-button';
const ZOOM_LIST_DROPDOWN_SELECTOR = '[aria-label*="Zoom"]';
const SURFACE_DUO_MENU_ITEM_SELECTOR = '[aria-label*="Surface Duo"]';
const FOLDABLE_DEVICE_MENU_ITEM_SELECTOR = '[aria-label*="Asus Zenbook Fold"]';
const EDIT_MENU_ITEM_SELECTOR = '[aria-label*="Edit"]';
const TEST_DEVICE_MENU_ITEM_SELECTOR = '[aria-label*="Test device, unchecked"]';
const DUAL_SCREEN_BUTTON_SELECTOR = '[aria-label="Toggle dual-screen mode"]';
const DEVICE_POSTURE_DROPDOWN_SELECTOR = '[aria-label="Device posture"]';
const SCREEN_DIM_INPUT_SELECTOR = '[title="Width"]';
const AUTO_AUTO_ADJUST_ZOOM_SELECTOR = '[aria-label*="Auto-adjust zoom"]';
export const reloadDockableFrontEnd = async () => {
await reloadDevTools({canDock: true});
};
export const deviceModeIsEnabled = async () => {
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
const pressed = await deviceToolbarToggler.evaluate(element => {
const button = element.shadowRoot?.querySelector('.primary-toggle') as HTMLButtonElement;
return button.getAttribute('aria-pressed');
});
return pressed === 'true';
};
export const clickDeviceModeToggler = async () => {
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
await clickElement(deviceToolbarToggler);
};
export const openDeviceToolbar = async () => {
if (await deviceModeIsEnabled()) {
return;
}
await clickDeviceModeToggler();
await waitFor(DEVICE_TOOLBAR_SELECTOR);
};
export const deviceModeButtonCanEnable = async () => {
const deviceToolbarToggler = await waitFor(DEVICE_TOOLBAR_TOGGLER_SELECTOR);
return await deviceToolbarToggler.evaluate(element => {
const button = element.shadowRoot?.querySelector('.primary-toggle') as HTMLButtonElement;
return !button.disabled;
});
};
export const showMediaQueryInspector = async () => {
const inspector = await $(MEDIA_QUERY_INSPECTOR_SELECTOR);
if (inspector) {
return;
}
await click(DEVICE_TOOLBAR_OPTIONS_SELECTOR);
const {frontend} = getBrowserAndPages();
await frontend.keyboard.press('ArrowDown');
await frontend.keyboard.press('Enter');
await waitFor(MEDIA_QUERY_INSPECTOR_SELECTOR);
};
export const startEmulationWithDualScreenPage = async () => {
await reloadDockableFrontEnd();
await goToResource('emulation/dual-screen-inspector.html');
await waitFor('.tabbed-pane-left-toolbar');
await openDeviceToolbar();
};
export const getButtonDisabled = async (spanButton: puppeteer.ElementHandle<HTMLButtonElement>) => {
return await spanButton.evaluate((e: HTMLButtonElement) => {
return e.disabled;
});
};
export const clickDevicesDropDown = async () => {
const toolbar = await waitFor(DEVICE_TOOLBAR_SELECTOR);
await click(DEVICE_LIST_DROPDOWN_SELECTOR, {root: toolbar});
};
export const clickDevicePostureDropDown = async () => {
const toolbar = await waitFor(DEVICE_TOOLBAR_SELECTOR);
await click(DEVICE_POSTURE_DROPDOWN_SELECTOR, {root: toolbar});
};
export const clickZoomDropDown = async () => {
const toolbar = await waitFor(DEVICE_TOOLBAR_SELECTOR);
await click(ZOOM_LIST_DROPDOWN_SELECTOR, {root: toolbar});
};
export const clickWidthInput = async () => {
const toolbar = await waitFor(DEVICE_TOOLBAR_SELECTOR);
await click(SCREEN_DIM_INPUT_SELECTOR, {root: toolbar});
};
export const selectToggleButton = async () => {
// button that toggles between single and double screen.
const toggleButton = await $(DUAL_SCREEN_BUTTON_SELECTOR) as puppeteer.ElementHandle<HTMLButtonElement>;
return toggleButton;
};
export const selectEdit = async () => {
await clickDevicesDropDown();
await click(EDIT_MENU_ITEM_SELECTOR);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};
export const selectDevice = async (name: string) => {
await clickDevicesDropDown();
await click(`[aria-label*="${name}, unchecked"]`);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};
export const selectTestDevice = async () => {
await clickDevicesDropDown();
await click(TEST_DEVICE_MENU_ITEM_SELECTOR);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};
// Test if span button works when emulating a dual screen device.
export const selectDualScreen = async () => {
await clickDevicesDropDown();
await click(SURFACE_DUO_MENU_ITEM_SELECTOR);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};
export const selectFoldableDevice = async () => {
await clickDevicesDropDown();
await click(FOLDABLE_DEVICE_MENU_ITEM_SELECTOR);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};
const waitForNotExpanded = async (selector: string) => {
const toolbar = await waitFor(DEVICE_TOOLBAR_SELECTOR);
const dropdown = await waitFor(selector, toolbar);
await waitForFunction(async () => {
const expanded = await dropdown.evaluate(el => el.getAttribute('aria-expanded'));
return expanded === null;
});
};
export const waitForZoomDropDownNotExpanded = async () => {
await waitForNotExpanded(ZOOM_LIST_DROPDOWN_SELECTOR);
};
export const clickDevicePosture = async (name: string) => {
await clickDevicePostureDropDown();
await click(`[aria-label*="${name}, unchecked"]`);
await waitForNotExpanded(DEVICE_POSTURE_DROPDOWN_SELECTOR);
};
export const getDevicePostureDropDown = async () => {
// dropdown menu for the posture selection.
const dropdown = await $(DEVICE_POSTURE_DROPDOWN_SELECTOR) as puppeteer.ElementHandle<HTMLButtonElement>;
return dropdown;
};
export const clickToggleButton = async () => {
// make sure the toggle button is clickable.
const toggleButton = await selectToggleButton();
await clickElement(toggleButton);
};
export const getWidthOfDevice = async () => {
// Read the width of spanned duo to make sure spanning works.
const widthInput = await waitFor(SCREEN_DIM_INPUT_SELECTOR);
return widthInput.evaluate(e => (e as HTMLInputElement).value);
};
export const getZoom = async () => {
// Read the width of spanned duo to make sure spanning works.
const widthInput = await waitFor(ZOOM_LIST_DROPDOWN_SELECTOR);
return widthInput.evaluate(e => (e as HTMLInputElement).innerText);
};
export const toggleAutoAdjustZoom = async () => {
await clickZoomDropDown();
await click(AUTO_AUTO_ADJUST_ZOOM_SELECTOR);
await waitForZoomDropDownNotExpanded();
};
const IPAD_MENU_ITEM_SELECTOR = '[aria-label*="iPad"]';
// Test if span button is clickable when emulating a non-dual-screen device.
export const selectNonDualScreenDevice = async () => {
await clickDevicesDropDown();
await click(IPAD_MENU_ITEM_SELECTOR);
await waitForNotExpanded(DEVICE_LIST_DROPDOWN_SELECTOR);
};