forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements-helpers.ts
1039 lines (916 loc) · 44 KB
/
elements-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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 {assert} from 'chai';
import type * as puppeteer from 'puppeteer-core';
import {AsyncScope} from '../../conductor/async-scope.js';
import {
$,
$$,
click,
clickElement,
clickMoreTabsButton,
getBrowserAndPages,
getTextContent,
goToResource,
pressKey,
step,
summonSearchBox,
typeText,
waitFor,
waitForAria,
waitForFunction,
waitForNone,
waitForVisible,
} from '../../shared/helper.js';
import {openSubMenu} from './context-menu-helpers.js';
import {
expectVeEvents,
veChange,
veClick,
veImpression,
veImpressionForElementsPanel,
veImpressionsUnder,
veKeyDown,
} from './visual-logging-helpers.js';
const SELECTED_TREE_ELEMENT_SELECTOR = '.selected[role="treeitem"]';
const CSS_PROPERTY_NAME_SELECTOR = '.webkit-css-property';
const CSS_PROPERTY_VALUE_SELECTOR = '.value';
const CSS_DECLARATION_SELECTOR =
`[role="treeitem"]:has(${CSS_PROPERTY_NAME_SELECTOR}):has(${CSS_PROPERTY_VALUE_SELECTOR})`;
const COLOR_SWATCH_SELECTOR = '.color-swatch-inner';
const CSS_STYLE_RULE_SELECTOR = '[aria-label*="css selector"]';
const COMPUTED_PROPERTY_SELECTOR = 'devtools-computed-style-property';
const COMPUTED_STYLES_PANEL_SELECTOR = '[aria-label="Computed panel"]';
const COMPUTED_STYLES_SHOW_ALL_SELECTOR = '[title="Show all"]';
export const ELEMENTS_PANEL_SELECTOR = '.panel[aria-label="elements"]';
const FONT_EDITOR_SELECTOR = '[aria-label="Font Editor"]';
const HIDDEN_FONT_EDITOR_SELECTOR = '.font-toolbar-hidden';
export const SECTION_SUBTITLE_SELECTOR = '.styles-section-subtitle';
const CLS_PANE_SELECTOR = '.styles-sidebar-toolbar-pane';
const CLS_BUTTON_SELECTOR = '[aria-label="Element Classes"]';
const CLS_INPUT_SELECTOR = '[aria-placeholder="Add new class"]';
const LAYOUT_PANE_TAB_SELECTOR = '[aria-label="Layout"]';
const LAYOUT_PANE_TABPANEL_SELECTOR = '[aria-label="Layout panel"]';
const ADORNER_SELECTOR = 'devtools-adorner';
export const INACTIVE_GRID_ADORNER_SELECTOR = '[aria-label="Enable grid mode"]';
export const ACTIVE_GRID_ADORNER_SELECTOR = '[aria-label="Disable grid mode"]';
const ELEMENT_CHECKBOX_IN_LAYOUT_PANE_SELECTOR = '.elements input[type=checkbox]';
const ELEMENT_STYLE_SECTION_SELECTOR = '[aria-label="element.style, css selector"]';
const STYLE_QUERY_RULE_TEXT_SELECTOR = '.query-text';
export const STYLE_PROPERTIES_SELECTOR = '.tree-outline-disclosure [role="treeitem"]';
const CSS_AUTHORING_HINTS_ICON_SELECTOR = '.hint';
export const SEARCH_BOX_SELECTOR = '.search-bar';
const SEARCH_RESULTS_MATCHES = '.search-results-matches';
export const EMULATE_FOCUSED_PAGE = 'Emulate a focused page';
export const openLayoutPane = async () => {
await step('Open Layout pane', async () => {
await click(LAYOUT_PANE_TAB_SELECTOR);
const panel = await waitFor(LAYOUT_PANE_TABPANEL_SELECTOR);
await waitFor('.elements', panel);
});
await expectVeEvents([
veClick('Panel: elements > Toolbar: sidebar > PanelTabHeader: elements.layout'),
veImpressionsUnder('Panel: elements', [veImpression(
'Pane', 'layout',
[
veImpression('SectionHeader', 'grid-settings'),
veImpression(
'Section', 'grid-settings',
[
veImpression('DropDown', 'show-grid-line-labels'),
veImpression('Toggle', 'extend-grid-lines'),
veImpression('Toggle', 'show-grid-areas'),
veImpression('Toggle', 'show-grid-track-sizes'),
]),
veImpression('Section', 'grid-overlays'),
veImpression('SectionHeader', 'flexbox-overlays'),
veImpression('Section', 'flexbox-overlays'),
])]),
]);
};
export const waitForAdorners = async (expectedAdorners: {textContent: string, isActive: boolean}[]) => {
await waitForFunction(async () => {
const actualAdorners = await $$(ADORNER_SELECTOR);
const actualAdornersStates = await Promise.all(actualAdorners.map(n => {
return n.evaluate((node, activeSelector: string) => {
// TODO for now only the grid adorner that can be active. When the flex (or other) adorner can be activated
// too we should change the selector passed here crbug.com/1144090.
return {textContent: node.textContent, isActive: node.matches(activeSelector)};
}, ACTIVE_GRID_ADORNER_SELECTOR);
}));
if (actualAdornersStates.length !== expectedAdorners.length) {
return false;
}
for (let i = 0; i < actualAdornersStates.length; i++) {
const index = expectedAdorners.findIndex(expected => {
const actual = actualAdornersStates[i];
return expected.textContent === actual.textContent && expected.isActive === actual.isActive;
});
if (index !== -1) {
expectedAdorners.splice(index, 1);
}
}
return expectedAdorners.length === 0;
});
if (expectedAdorners.length) {
await expectVeEvents(
[veImpressionsUnder('Panel: elements > Tree: elements > TreeItem', [veImpression('Adorner', 'grid')])]);
}
};
export const toggleAdornerSetting = async (type: string) => {
await openSubMenu(SELECTED_TREE_ELEMENT_SELECTOR, 'Badge settings');
const adornerToggle =
await Promise.any([waitFor(`[aria-label="${type}, unchecked"]`), waitFor(`[aria-label="${type}, checked"]`)]);
await adornerToggle.click();
await expectVeEvents([veClick(`Menu > Toggle: ${type}`)]);
};
export const waitForSelectedNodeToBeExpanded = async () => {
await waitFor(`${SELECTED_TREE_ELEMENT_SELECTOR}[aria-expanded="true"]`);
};
export const waitForAdornerOnSelectedNode = async (expectedAdornerText: string) => {
await waitForFunction(async () => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
const adorner = await waitFor(ADORNER_SELECTOR, selectedNode);
return expectedAdornerText === await adorner.evaluate(node => node.textContent);
});
await expectVeEvents([veImpressionsUnder(
'Panel: elements > Tree: elements > TreeItem', [veImpression('Adorner', expectedAdornerText)])]);
};
export const waitForNoAdornersOnSelectedNode = async () => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
await waitForNone(ADORNER_SELECTOR, selectedNode);
};
export const toggleElementCheckboxInLayoutPane = async () => {
await step('Click element checkbox in Layout pane', async () => {
await click(ELEMENT_CHECKBOX_IN_LAYOUT_PANE_SELECTOR);
});
await expectVeEvents([veClick('Panel: elements > Pane: layout > Section: grid-overlays > Item > Toggle')]);
};
export const getGridsInLayoutPane = async () => {
const panel = await waitFor(LAYOUT_PANE_TABPANEL_SELECTOR);
return await $$('.elements .element', panel);
};
export const waitForSomeGridsInLayoutPane = async (minimumGridCount: number) => {
await waitForFunction(async () => {
const grids = await getGridsInLayoutPane();
return grids.length >= minimumGridCount;
});
await expectVeEvents(
[veImpressionsUnder('Panel: elements > Pane: layout > Section: grid-overlays', [veImpression('Item', undefined, [
veImpression('Action', 'elements.select-element'),
veImpression('ShowStyleEditor', 'color'),
veImpression('Toggle'),
])])]);
};
export const waitForContentOfSelectedElementsNode = async (expectedTextContent: string) => {
await waitForFunction(async () => {
const selectedTextContent = await getContentOfSelectedNode();
return selectedTextContent === expectedTextContent;
});
};
export const waitForPartialContentOfSelectedElementsNode = async (expectedPartialTextContent: string) => {
await waitForFunction(async () => {
const selectedTextContent = await getContentOfSelectedNode();
return selectedTextContent.includes(expectedPartialTextContent);
});
};
/**
* Gets the text content of the currently selected element.
*/
export const getContentOfSelectedNode = async () => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
return await selectedNode.evaluate(node => node.textContent as string);
};
export const waitForSelectedNodeChange = async (initialValue: string, asyncScope = new AsyncScope()) => {
await waitForFunction(async () => {
const currentContent = await getContentOfSelectedNode();
return currentContent !== initialValue;
}, asyncScope);
};
export const assertSelectedElementsNodeTextIncludes = async (expectedTextContent: string) => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
const selectedTextContent = await selectedNode.evaluate(node => node.textContent as string);
assert.include(selectedTextContent, expectedTextContent);
};
export const waitForSelectedTreeElementSelectorWithTextcontent = async (expectedTextContent: string) => {
await waitForFunction(async () => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
const selectedTextContent = await selectedNode.evaluate(node => node.textContent);
return selectedTextContent === expectedTextContent;
});
};
export const waitForSelectedTreeElementSelectorWhichIncludesText = async (expectedTextContent: string) => {
await waitForFunction(async () => {
const selectedNode = await waitFor(SELECTED_TREE_ELEMENT_SELECTOR);
const selectedTextContent = await selectedNode.evaluate(node => node.textContent);
return selectedTextContent && selectedTextContent.includes(expectedTextContent);
});
};
export const waitForChildrenOfSelectedElementNode = async () => {
await waitFor(`${SELECTED_TREE_ELEMENT_SELECTOR} + ol > li`);
};
export const waitForAndClickTreeElementWithPartialText = async (text: string) =>
waitForFunction(async () => clickTreeElementWithPartialText(text));
export const waitForElementWithPartialText = async (text: string) => {
return waitForFunction(async () => elementWithPartialText(text));
};
const elementWithPartialText = async (text: string) => {
const tree = await waitFor('Page DOM[role="tree"]', undefined, undefined, 'aria');
const elements = await $$('[role="treeitem"]', tree, 'aria');
for (const handle of elements) {
const match = await handle.evaluate((element, text) => element.textContent?.includes(text), text);
if (match) {
return handle;
}
}
return null;
};
export const clickTreeElementWithPartialText = async (text: string) => {
const handle = await elementWithPartialText(text);
if (handle) {
await clickElement(handle);
await expectVeEvents([veClick('Panel: elements > Tree: elements > TreeItem')]);
return true;
}
return false;
};
export const clickNthChildOfSelectedElementNode = async (childIndex: number) => {
assert(childIndex > 0, 'CSS :nth-child() selector indices are 1-based.');
await click(`${SELECTED_TREE_ELEMENT_SELECTOR} + ol > li:nth-child(${childIndex})`);
await expectVeEvents([veClick('Panel: elements > Tree: elements > TreeItem')]);
};
export const focusElementsTree = async () => {
await click(SELECTED_TREE_ELEMENT_SELECTOR);
await expectVeEvents([veClick('Panel: elements > Tree: elements > TreeItem')]);
};
export const navigateToSidePane = async (paneName: string) => {
if ((await $$(`[aria-label="${paneName} panel"]`)).length) {
return;
}
await click(`[aria-label="${paneName}"]`);
await waitFor(`[aria-label="${paneName} panel"]`);
const jslogContext = paneName.toLowerCase();
await expectVeEvents([
veClick(`Panel: elements > Toolbar: sidebar > PanelTabHeader: ${jslogContext}`),
veImpressionsUnder('Panel: elements', [veImpression('Pane', jslogContext)]),
]);
};
export const waitForElementsStyleSection = async () => {
// Wait for the file to be loaded and selectors to be shown
await waitFor('.styles-selector');
await expectVeEvents([veImpressionsUnder('Panel: elements', [veImpression('Pane', 'styles')])]);
};
export const waitForElementsComputedSection = async () => {
await waitFor(COMPUTED_PROPERTY_SELECTOR);
await expectVeEvents([veImpressionsUnder('Panel: elements', [veImpression('Pane', 'computed')])]);
};
export const getContentOfComputedPane = async () => {
const pane = await waitFor('Computed panel', undefined, undefined, 'aria');
const tree = await waitFor('[role="tree"]', pane, undefined, 'aria');
return await tree.evaluate(node => node.textContent as string);
};
export const waitForComputedPaneChange = async (initialValue: string) => {
await waitForFunction(async () => {
const value = await getContentOfComputedPane();
return value !== initialValue;
});
};
export const getAllPropertiesFromComputedPane = async () => {
const properties = await $$(COMPUTED_PROPERTY_SELECTOR);
return (await Promise.all(properties.map(elem => elem.evaluate(async node => {
const nameSlot = node.shadowRoot?.querySelector<HTMLSlotElement>('.property-name slot');
const valueSlot = node.shadowRoot?.querySelector<HTMLSlotElement>('.property-value slot');
const name = nameSlot?.assignedElements().at(0);
const value = valueSlot?.assignedElements().at(0);
return (!name || !value) ? null : {
name: name.textContent ? name.textContent.trim().replace(/:$/, '') : '',
value: value.textContent ? value.textContent.trim().replace(/;$/, '') : '',
};
}))))
.filter(prop => Boolean(prop));
};
export const getPropertyFromComputedPane = async (name: string) => {
const properties = await $$(COMPUTED_PROPERTY_SELECTOR);
for (const property of properties) {
const matchingProperty = await property.evaluate((node, name) => {
const nameSlot = node.shadowRoot?.querySelector<HTMLSlotElement>('.property-name slot');
const nameEl = nameSlot?.assignedElements().at(0);
return nameEl?.textContent?.trim().replace(/:$/, '') === name;
}, name);
// Note that evaluateHandle always returns a handle, even if it points to an undefined remote object, so we need to
// check it's defined here or continue iterating.
if (matchingProperty) {
return property;
}
}
return undefined;
};
export const expandSelectedNodeRecursively = async () => {
const EXPAND_RECURSIVELY = '[aria-label="Expand recursively"]';
// Find the selected node, right click.
await click(SELECTED_TREE_ELEMENT_SELECTOR, {clickOptions: {button: 'right'}});
// Wait for the 'expand recursively' option, and click it.
await click(EXPAND_RECURSIVELY);
await expectVeEvents([
veClick('Panel: elements > Tree: elements > TreeItem'),
veImpressionForSelectedNodeMenu(await getContentOfSelectedNode()),
veClick('Panel: elements > Tree: elements > TreeItem > Menu > Action: expand-recursively'),
]);
};
export const findElementById = async (id: string) => {
await pressKey('f', {control: true});
await waitFor('.search-bar:not(.hidden)');
await typeText('#' + id);
await pressKey('Enter');
await waitFor(`.highlight > .webkit-html-tag[aria-label*="\\"${id}\\"`);
await pressKey('Escape');
await waitFor('.search-bar.hidden');
};
function veImpressionForSelectedNodeMenu(content: string) {
const isPeudoElement = content.startsWith('::');
if (isPeudoElement) {
return veImpressionsUnder('Panel: elements > Tree: elements > TreeItem', [veImpression('Menu', undefined, [
veImpression('Action', 'expand-recursively'),
veImpression('Action', 'scroll-into-view'),
veImpression('Item', 'show-adorner-settings'),
veImpression('Action', 'store-as-global-variable'),
])]);
}
return veImpressionsUnder('Panel: elements > Tree: elements > TreeItem', [veImpression('Menu', undefined, [
veImpression('Action', 'add-attribute'),
veImpression('Action', 'collapse-children'),
veImpression('Action', 'cut'),
veImpression('Action', 'delete-element'),
veImpression('Action', 'elements.duplicate-element'),
veImpression('Action', 'elements.edit-as-html'),
veImpression('Action', 'emulation.capture-node-screenshot'),
veImpression('Action', 'expand-recursively'),
veImpression('Action', 'focus'),
veImpression('Action', 'paste'),
veImpression('Action', 'scroll-into-view'),
veImpression('Item', 'show-adorner-settings'),
veImpression('Action', 'store-as-global-variable'),
veImpression('Item', 'break-on'),
veImpression('Item', 'copy'),
veImpression('Item', 'force-state'),
veImpression('Toggle', 'elements.hide-element'),
])]);
}
export const showForceState = async (specificStates?: boolean) => {
// Check if it is already visible
if (!(await $(EMULATE_FOCUSED_PAGE, undefined, 'aria'))) {
await click('[aria-label="Toggle Element State"]');
await waitForAria(EMULATE_FOCUSED_PAGE);
}
if (specificStates) {
const specificStatesPane = await waitFor('.specific-pseudo-states');
if (!(await specificStatesPane.evaluate(node => node.checkVisibility()))) {
await click('.force-specific-element-header');
await waitForVisible('.specific-pseudo-states');
}
}
};
export const forcePseudoState = async (pseudoState: string, specificStates?: boolean) => {
// Open element & page state pane and wait for it to be loaded asynchronously
await showForceState(specificStates);
const stateEl = await waitForAria(pseudoState);
// FIXME(crbug/1112692): Refactor test to remove the timeout.
// await timeout(100);
await stateEl.click();
await expectVeEvents([
veClick('Panel: elements > Pane: styles > ToggleSubpane: element-states'),
veImpressionsUnder('Panel: elements > Pane: styles', [veImpression(
'Pane', 'element-states',
[
veImpression('Action: learn-more'),
veImpression('Toggle: active'),
veImpression('Toggle: focus'),
veImpression('Toggle: focus-visible'),
veImpression('Toggle: focus-within'),
veImpression('Toggle: hover'),
veImpression('Toggle: target'),
])]),
veChange(`Panel: elements > Pane: styles > Pane: element-states > Toggle: ${
pseudoState === EMULATE_FOCUSED_PAGE ? 'emulate-page-focus' : pseudoState.substr(1)}`),
]);
};
export const removePseudoState = async (pseudoState: string) => {
const stateEl = await waitForAria(pseudoState);
await stateEl.click();
await expectVeEvents([
veChange(`Panel: elements > Pane: styles > Pane: element-states > Toggle: ${
pseudoState === EMULATE_FOCUSED_PAGE ? 'emulate-page-focus' : pseudoState.substr(1)}`),
]);
};
export const getComputedStylesForDomNode =
async (elementSelector: string, styleAttribute: keyof CSSStyleDeclaration) => {
const {target} = getBrowserAndPages();
return target.evaluate((elementSelector, styleAttribute) => {
const element = document.querySelector(elementSelector);
if (!element) {
throw new Error(`${elementSelector} could not be found`);
}
return getComputedStyle(element)[styleAttribute];
}, elementSelector, styleAttribute);
};
export const waitForNumberOfComputedProperties = async (numberToWaitFor: number) => {
const computedPane = await getComputedPanel();
return waitForFunction(
async () => numberToWaitFor ===
await computedPane.$$eval('pierce/' + COMPUTED_PROPERTY_SELECTOR, properties => properties.length));
};
export const getComputedPanel = async () => waitFor(COMPUTED_STYLES_PANEL_SELECTOR);
export const filterComputedProperties = async (filterString: string) => {
const initialContent = await getContentOfComputedPane();
const computedPanel = await waitFor(COMPUTED_STYLES_PANEL_SELECTOR);
await click('[aria-label="Filter"]', {
root: computedPanel,
});
await typeText(filterString);
await waitForComputedPaneChange(initialContent);
await expectVeEvents([veChange('Panel: elements > Pane: computed > TextField: filter')]);
};
export const toggleShowAllComputedProperties = async () => {
const initialContent = await getContentOfComputedPane();
const computedPanel = await waitFor(COMPUTED_STYLES_PANEL_SELECTOR);
await click(COMPUTED_STYLES_SHOW_ALL_SELECTOR, {root: computedPanel});
await waitForComputedPaneChange(initialContent);
await expectVeEvents(
[veChange('Panel: elements > Pane: computed > Toggle: show-inherited-computed-style-properties')]);
};
export const waitForDomNodeToBeVisible = async (elementSelector: string) => {
const {target} = getBrowserAndPages();
// DevTools will force Blink to make the hover shown, so we have
// to wait for the element to be DOM-visible (e.g. no `display: none;`)
await target.waitForSelector(elementSelector, {visible: true});
};
export const waitForDomNodeToBeHidden = async (elementSelector: string) => {
const {target} = getBrowserAndPages();
await target.waitForSelector(elementSelector, {hidden: true});
};
export const assertGutterDecorationForDomNodeExists = async () => {
await waitFor('.elements-gutter-decoration');
};
export const getStyleRuleSelector = (selector: string) => `[aria-label="${selector}, css selector"]`;
export const waitForExactStyleRule = async (expectedSelector: string) => {
await waitForFunction(async () => {
const rules = await getDisplayedStyleRules();
return rules.find(rule => rule.selectorText === expectedSelector);
});
};
export const waitForStyleRule = async (expectedSelector: string) => {
await waitForFunction(async () => {
const rules = await getDisplayedStyleRules();
return rules.map(rule => rule.selectorText).includes(expectedSelector);
});
};
export const getComputedStyleProperties = async () => {
const computedPanel = await getComputedPanel();
const allProperties = await computedPanel.$$('pierce/[role="treeitem"][aria-level="1"]');
const properties = [];
for (const prop of allProperties) {
const name = await prop.$eval('pierce/' + CSS_PROPERTY_NAME_SELECTOR, element => element.textContent);
const value = await prop.$eval('pierce/' + CSS_PROPERTY_VALUE_SELECTOR, element => element.textContent);
const traceElements = await prop.$$('pierce/devtools-computed-style-trace');
const trace = await Promise.all(traceElements.map(async element => {
const value = await element.$eval('pierce/.value', element => element.textContent);
const selector = await element.$eval('pierce/.trace-selector', element => element.textContent);
const link = await element.$eval('pierce/.trace-link', element => element.textContent);
return {value, selector, link};
}));
properties.push({name, value, trace});
}
return properties;
};
export const getDisplayedCSSDeclarations = async () => {
const cssDeclarations = await $$(CSS_DECLARATION_SELECTOR);
return Promise.all(cssDeclarations.map(async node => await node.evaluate(n => n.textContent?.trim())));
};
export const getDisplayedStyleRulesCompact = async () => {
const compactRules = [];
for (const rule of await getDisplayedStyleRules()) {
compactRules.push(
{selectorText: rule.selectorText, propertyNames: rule.propertyData.map(data => data.propertyName)});
}
return compactRules;
};
export const getDisplayedStyleRules = async () => {
const allRuleSelectors = await $$(CSS_STYLE_RULE_SELECTOR);
const rules = [];
for (const ruleSelector of allRuleSelectors) {
const propertyData = await getDisplayedCSSPropertyData(ruleSelector);
const selectorText = await ruleSelector.evaluate(node => {
const attribute = node.getAttribute('aria-label') || '';
return attribute.substring(0, attribute.lastIndexOf(', css selector'));
});
rules.push({selectorText, propertyData});
}
return rules;
};
/**
* @param propertiesSection - The element containing this properties section.
* @returns an array with an entry for each property in the section. Each entry has:
* - propertyName: The name of this property.
* - isOverloaded: True if this is an inherited properties section, and this property is overloaded by a child node.
* The property will be shown as crossed out in the style pane.
* - isInherited: True if this is an inherited properties section, and this property is a non-inherited CSS property.
* The property will be shown as grayed-out in the style pane.
*/
export const getDisplayedCSSPropertyData = async (propertiesSection: puppeteer.ElementHandle<Element>) => {
const cssPropertyNames = await $$(CSS_PROPERTY_NAME_SELECTOR, propertiesSection);
const propertyNamesData =
(await Promise.all(cssPropertyNames.map(
async node => {
return {
propertyName: await node.evaluate(n => n.textContent),
isOverLoaded: await node.evaluate(n => n.parentElement && n.parentElement.matches('.overloaded')),
isInherited: await node.evaluate(n => n.parentElement && n.parentElement.matches('.inherited')),
};
},
)))
.filter(c => Boolean(c.propertyName));
return propertyNamesData;
};
export const getDisplayedCSSPropertyNames = async (propertiesSection: puppeteer.ElementHandle<Element>) => {
const cssPropertyNames = await $$(CSS_PROPERTY_NAME_SELECTOR, propertiesSection);
const propertyNamesText = (await Promise.all(cssPropertyNames.map(
node => node.evaluate(n => n.textContent),
)))
.filter(c => Boolean(c));
return propertyNamesText;
};
export const getStyleRule = (selector: string) => {
return waitFor(getStyleRuleSelector(selector));
};
export const getStyleRuleWithSourcePosition = (styleSelector: string, sourcePosition?: string) => {
if (!sourcePosition) {
return getStyleRule(styleSelector);
}
const selector = getStyleRuleSelector(styleSelector);
return waitForFunction(async () => {
const candidate = await waitFor(selector);
if (candidate) {
const sourcePositionElement = await candidate.$('.styles-section-subtitle .devtools-link');
const text = await sourcePositionElement?.evaluate(node => node.textContent);
if (text === sourcePosition) {
return candidate;
}
}
return undefined;
});
};
export const getColorSwatch = async (parent: puppeteer.ElementHandle<Element>|undefined, index: number) => {
const swatches = await $$(COLOR_SWATCH_SELECTOR, parent);
return swatches[index];
};
export const getColorSwatchColor = async (parent: puppeteer.ElementHandle<Element>, index: number) => {
const swatch = await getColorSwatch(parent, index);
return await swatch.evaluate(node => (node as HTMLElement).style.backgroundColor);
};
export const shiftClickColorSwatch =
async (parent: puppeteer.ElementHandle<Element>, index: number, parentVe: string) => {
const swatch = await getColorSwatch(parent, index);
const {frontend} = getBrowserAndPages();
await frontend.keyboard.down('Shift');
await clickElement(swatch);
await frontend.keyboard.up('Shift');
await expectVeEvents([
veClick(`${parentVe} > ShowStyleEditor: color`),
veImpressionsUnder(
`${parentVe} > ShowStyleEditor: color`,
[veImpression('Menu', undefined, [veImpression('Action', 'clipped-color'), veImpression('Item', 'color')])]),
]);
};
export const getElementStyleFontEditorButton = async () => {
const section = await waitFor(ELEMENT_STYLE_SECTION_SELECTOR);
const result = await $(FONT_EDITOR_SELECTOR, section);
await expectVeEvents([veImpressionsUnder(
'Panel: elements > Pane: styles > Section: style-properties', [veImpression('Action', 'font-editor')])]);
return result;
};
export const getFontEditorButtons = async () => {
const buttons = await $$(FONT_EDITOR_SELECTOR);
return buttons;
};
export const getHiddenFontEditorButtons = async () => {
const buttons = await $$(HIDDEN_FONT_EDITOR_SELECTOR);
return buttons;
};
export const getStyleSectionSubtitles = async () => {
const subtitles = await $$(SECTION_SUBTITLE_SELECTOR);
return Promise.all(subtitles.map(node => node.evaluate(n => n.textContent)));
};
export const getCSSPropertyInRule =
async (ruleSection: puppeteer.ElementHandle<Element>|string, name: string, sourcePosition?: string) => {
if (typeof ruleSection === 'string') {
ruleSection = await getStyleRuleWithSourcePosition(ruleSection, sourcePosition);
}
const propertyNames = await $$(CSS_PROPERTY_NAME_SELECTOR, ruleSection);
for (const node of propertyNames) {
const parent =
(await node.evaluateHandle((node, name) => (name === node.textContent) ? node.parentNode : undefined, name))
.asElement();
if (parent) {
return parent as puppeteer.ElementHandle<HTMLElement>;
}
}
return undefined;
};
export const focusCSSPropertyValue = async (selector: string, propertyName: string) => {
await waitForStyleRule(selector);
let property = await getCSSPropertyInRule(selector, propertyName);
// Clicking on the semicolon element to make sure we don't hit the swatch or other
// non-editable elements.
await click(CSS_PROPERTY_VALUE_SELECTOR + ' + .styles-semicolon', {root: property});
await waitForFunction(async () => {
property = await getCSSPropertyInRule(selector, propertyName);
const value = property ? await $(CSS_PROPERTY_VALUE_SELECTOR, property) : null;
if (!value) {
assert.fail(`Could not find property ${propertyName} in rule ${selector}`);
}
return await value.evaluate(node => {
return node.classList.contains('text-prompt') && node.hasAttribute('contenteditable');
});
});
await expectVeEvents([veClick(`Panel: elements > Pane: styles > Section: style-properties > Tree > TreeItem: ${
propertyName.startsWith('--') ? 'custom-property' : propertyName}`)]);
};
/**
* Edit a CSS property value in a given rule
* @param selector The selector of the rule to be updated. Note that because of the way the Styles populates, it is
* important to provide a rule selector that is unique here, to avoid editing a property in the wrong rule.
* @param propertyName The name of the property to be found and edited. If several properties have the same names, the
* first one is edited.
* @param newValue The new value to be used.
*/
export async function editCSSProperty(selector: string, propertyName: string, newValue: string) {
await focusCSSPropertyValue(selector, propertyName);
const {frontend} = getBrowserAndPages();
await frontend.keyboard.type(newValue, {delay: 100});
await frontend.keyboard.press('Enter');
await waitForFunction(async () => {
// Wait until the value element is not a text-prompt anymore.
const property = await getCSSPropertyInRule(selector, propertyName);
const value = property ? await $(CSS_PROPERTY_VALUE_SELECTOR, property) : null;
if (!value) {
assert.fail(`Could not find property ${propertyName} in rule ${selector}`);
}
return await value.evaluate(node => {
return !node.classList.contains('text-prompt') && !node.hasAttribute('contenteditable');
});
});
await expectVeEvents([veChange(`Panel: elements > Pane: styles > Section: style-properties > Tree > TreeItem: ${
propertyName.startsWith('--') ? 'custom-property' : propertyName} > Value`)]);
}
// Edit a media or container query rule text for the given styles section
export async function editQueryRuleText(queryStylesSections: puppeteer.ElementHandle<Element>, newQueryText: string) {
await click(STYLE_QUERY_RULE_TEXT_SELECTOR, {root: queryStylesSections});
await waitForFunction(async () => {
// Wait until the value element has been marked as a text-prompt.
const queryText = await $(STYLE_QUERY_RULE_TEXT_SELECTOR, queryStylesSections);
if (!queryText) {
assert.fail('Could not find any query in the given styles section');
}
const check = await queryText.evaluate(node => {
return node.classList.contains('being-edited') && node.hasAttribute('contenteditable');
});
return check;
});
await typeText(newQueryText);
await pressKey('Enter');
await waitForFunction(async () => {
// Wait until the value element is not a text-prompt anymore.
const queryText = await $(STYLE_QUERY_RULE_TEXT_SELECTOR, queryStylesSections);
if (!queryText) {
assert.fail('Could not find any query in the given styles section');
}
const check = await queryText.evaluate(node => {
return !node.classList.contains('being-edited') && !node.hasAttribute('contenteditable');
});
return check;
});
await expectVeEvents([
veClick('Panel: elements > Pane: styles > Section: style-properties > CSSRuleHeader: container-query'),
veChange('Panel: elements > Pane: styles > Section: style-properties > CSSRuleHeader: container-query'),
]);
}
export async function waitForCSSPropertyValue(selector: string, name: string, value: string, sourcePosition?: string) {
return await waitForFunction(async () => {
const propertyHandle = await getCSSPropertyInRule(selector, name, sourcePosition);
if (!propertyHandle) {
return undefined;
}
const valueHandle = await $(CSS_PROPERTY_VALUE_SELECTOR, propertyHandle);
if (!valueHandle) {
return undefined;
}
const matches = await valueHandle.evaluate((node, value) => node.textContent === value, value);
if (matches) {
return valueHandle;
}
return undefined;
});
}
export async function waitForPropertyToHighlight(ruleSelector: string, propertyName: string) {
await waitForFunction(async () => {
const property = await getCSSPropertyInRule(ruleSelector, propertyName);
if (!property) {
assert.fail(`Could not find property ${propertyName} in rule ${ruleSelector}`);
}
// StylePropertyHighlighter temporarily highlights the property using the Web Animations API, so the only way to
// know it's happening is by listing all animations.
const animationCount = await property.evaluate(node => (node as HTMLElement).getAnimations().length);
return animationCount > 0;
});
}
export const getBreadcrumbsTextContent = async ({expectedNodeCount}: {expectedNodeCount: number}) => {
const crumbsSelector = 'li.crumb > a > devtools-node-text';
await waitForFunction(async () => {
const crumbs = await $$(crumbsSelector);
return crumbs.length === expectedNodeCount;
});
const crumbs = await $$(crumbsSelector);
const crumbsAsText: string[] = await Promise.all(crumbs.map(node => node.evaluate((node: Element) => {
if (!node.shadowRoot) {
assert.fail('Found breadcrumbs node that unexpectedly has no shadowRoot.');
}
return Array.from(node.shadowRoot.querySelectorAll('span') || []).map(span => span.textContent).join('');
})));
return crumbsAsText;
};
export const getSelectedBreadcrumbTextContent = async () => {
const selectedCrumb = await waitFor('li.crumb.selected > a > devtools-node-text');
const text = selectedCrumb.evaluate((node: Element) => {
if (!node.shadowRoot) {
assert.fail('Found breadcrumbs node that unexpectedly has no shadowRoot.');
}
return Array.from(node.shadowRoot.querySelectorAll('span') || []).map(span => span.textContent).join('');
});
return text;
};
export const navigateToElementsTab = async () => {
if ((await $$(ELEMENTS_PANEL_SELECTOR)).length) {
return;
}
// Open Elements panel
await click('#tab-elements');
await waitFor(ELEMENTS_PANEL_SELECTOR);
await expectVeEvents([veImpressionForElementsPanel()]);
};
export const clickOnFirstLinkInStylesPanel = async () => {
const stylesPane = await waitFor('div.styles-pane');
await click('div.styles-section-subtitle button.devtools-link', {root: stylesPane});
await expectVeEvents([veClick('Panel: elements > Pane: styles > Section: style-properties > Link: css-location')]);
};
export const toggleClassesPane = async () => {
await click(CLS_BUTTON_SELECTOR);
await expectVeEvents([
veClick('Panel: elements > Pane: styles > ToggleSubpane: elements-classes'),
veImpressionsUnder(
'Panel: elements > Pane: styles', [veImpression('Pane', 'elements-classes', [veImpression('TextField')])]),
]);
};
export const typeInClassesPaneInput =
async (text: string, commitWith: puppeteer.KeyInput = 'Enter', waitForNodeChange: Boolean = true) => {
await step(`Typing in new class names ${text}`, async () => {
const clsInput = await waitFor(CLS_INPUT_SELECTOR);
await clsInput.type(text, {delay: 50});
});
if (commitWith) {
await step(`Committing the changes with ${commitWith}`, async () => {
const {frontend} = getBrowserAndPages();
await frontend.keyboard.press(commitWith);
});
}
if (waitForNodeChange) {
// Make sure the classes provided in text can be found in the selected element's content. This is important as the
// cls pane applies classes as you type, so it is not enough to wait for the selected node to change just once.
await step('Waiting for the selected node to change', async () => {
await waitForFunction(async () => {
const nodeContent = await getContentOfSelectedNode();
return text.split(' ').every(cls => nodeContent.includes(cls));
});
});
}
await expectVeEvents([veChange('Panel: elements > Pane: styles > Pane: elements-classes > TextField')]);
};
export const toggleClassesPaneCheckbox = async (checkboxLabel: string) => {
const initialValue = await getContentOfSelectedNode();
const classesPane = await waitFor(CLS_PANE_SELECTOR);
await click(`[title="${checkboxLabel}"]`, {root: classesPane});
await waitForSelectedNodeChange(initialValue);
await expectVeEvents([veChange('Panel: elements > Pane: styles > Pane: elements-classes > Toggle: element-class')]);
};
export const uncheckStylesPaneCheckbox = async (checkboxLabel: string) => {
console.error('uncheckStylesPaneCheckbox', checkboxLabel);
const initialValue = await getContentOfSelectedNode();
await click(`.enabled-button[aria-label="${checkboxLabel}"]`);
await waitForSelectedNodeChange(initialValue);
await expectVeEvents([veClick(`Panel: elements > Pane: styles > Section: style-properties > Tree > TreeItem: ${
checkboxLabel.split(' ')[0]} > Toggle`)]);
};
export const assertSelectedNodeClasses = async (expectedClasses: string[]) => {
const nodeText = await getContentOfSelectedNode();
const match = nodeText.match(/class=\u200B"([^"]*)/);
const classText = match ? match[1] : '';
const classes = classText.split(/[\s]/).map(className => className.trim()).filter(className => className.length);
assert.strictEqual(
classes.length, expectedClasses.length, 'Did not find the expected number of classes on the element');
for (const expectedClass of expectedClasses) {
assert.include(classes, expectedClass, `Could not find class ${expectedClass} on the element`);
}
};
export const toggleAccessibilityPane = async () => {
let a11yPane = await $('Accessibility', undefined, 'aria');
if (!a11yPane) {
const elementsPanel = await waitForAria('Elements panel');
await clickMoreTabsButton(elementsPanel);
a11yPane = await waitForAria('Accessibility');
await expectVeEvents([
veClick('Panel: elements > Toolbar: sidebar > DropDown: more-tabs'),
veImpressionsUnder(
'Panel: elements > Toolbar: sidebar > DropDown: more-tabs',
[veImpression('Menu', undefined, [veImpression('Action', 'accessibility.view')])]),
]);
}
await clickElement(a11yPane);
await waitFor('.source-order-checkbox');
await expectVeEvents([
veClick('Panel: elements > Toolbar: sidebar > DropDown: more-tabs > Menu > Action: accessibility.view'),
veImpressionsUnder('Panel: elements > Toolbar: sidebar', [veImpression('PanelTabHeader', 'accessibility.view')]),
veImpressionForAccessibilityPane(),
]);
};
function veImpressionForAccessibilityPane() {
return veImpressionsUnder(
'Panel: elements', [veImpression('Pane', 'sidebar', [
veImpression('SectionHeader', 'accessibility-tree'),
veImpression(
'Section', 'accessibility-tree',
[
veImpression('Toggle', 'full-accessibility-tree'),
veImpression('TreeItem', undefined, [veImpression('Expand'), veImpression('TreeItem')]),
]),
veImpression('SectionHeader', 'aria-attributes'),
veImpression('Section', 'aria-attributes'),
veImpression('SectionHeader', 'computed-properties'),
veImpression('Section', 'computed-properties', [veImpression('Tree', undefined, [veImpression('TreeItem')])]),
veImpression('SectionHeader', 'source-order-viewer'),
veImpression('Section', 'source-order-viewer', [veImpression('Toggle')]),
])]);
}
export const toggleAccessibilityTree = async () => {
await click('aria/Switch to Accessibility Tree view');
await expectVeEvents([veClick('Panel: elements > Action: toggle-accessibility-tree')]);
};
export const getPropertiesWithHints = async () => {
const allRuleSelectors = await $$(CSS_STYLE_RULE_SELECTOR);
const propertiesWithHints = [];
for (const propertiesSection of allRuleSelectors) {
const cssRuleNodes = await $$('li ', propertiesSection);
for (const cssRuleNode of cssRuleNodes) {
const propertyNode = await $(CSS_PROPERTY_NAME_SELECTOR, cssRuleNode);
const propertyName = propertyNode !== null ? await propertyNode.evaluate(n => n.textContent) : null;
if (propertyName === null) {
continue;
}
const authoringHintsIcon = await $(CSS_AUTHORING_HINTS_ICON_SELECTOR, cssRuleNode);
if (authoringHintsIcon) {
propertiesWithHints.push(propertyName);
}
}
}