1
+ import { NightwatchBrowser } from 'nightwatch'
2
+ import EventEmitter from 'events'
3
+
4
+ class ExpandAllFolders extends EventEmitter {
5
+ command ( this : NightwatchBrowser , targetDirectory ?: string ) {
6
+ this . api . perform ( ( done ) => {
7
+ expandAllFolders ( this . api , targetDirectory , ( ) => {
8
+ done ( )
9
+ this . emit ( 'complete' )
10
+ } )
11
+ } )
12
+ return this
13
+ }
14
+ }
15
+
16
+ function expandAllFolders ( browser : NightwatchBrowser , targetDirectory ?: string , done ?: VoidFunction ) {
17
+ // Ensure file panel is open
18
+ browser . perform ( ( bdone : VoidFunction ) => {
19
+ browser . isVisible ( '[data-id="remixIdeSidePanel"]' , ( result ) => {
20
+ if ( result . value ) {
21
+ browser . element ( 'css selector' , '[data-id="verticalIconsKindfilePanel"] img[data-id="selected"]' , ( result ) => {
22
+ if ( result . status === 0 ) {
23
+ bdone ( )
24
+ } else browser . clickLaunchIcon ( 'filePanel' ) . perform ( ( ) => {
25
+ bdone ( )
26
+ } )
27
+ } )
28
+ } else {
29
+ browser . clickLaunchIcon ( 'filePanel' ) . perform ( ( ) => {
30
+ bdone ( )
31
+ } )
32
+ }
33
+ } )
34
+ } )
35
+ . perform ( ( ) => {
36
+ let attempts = 0
37
+ const maxAttempts = 200
38
+
39
+ const expandNextClosedFolder = ( ) => {
40
+ if ( attempts >= maxAttempts ) {
41
+ if ( done ) done ( )
42
+ return
43
+ }
44
+ attempts ++
45
+
46
+ const closedFolderSelector = targetDirectory
47
+ ? `li[data-id*="treeViewLitreeViewItem${ targetDirectory } "] .fa-folder:not(.fa-folder-open)`
48
+ : 'li[data-id*="treeViewLitreeViewItem"] .fa-folder:not(.fa-folder-open)'
49
+
50
+ browser . element ( 'css selector' , closedFolderSelector , ( result ) => {
51
+ if ( result . status === 0 && result . value ) {
52
+ // Found a closed folder icon, now find its parent li element and click it
53
+ browser . elementIdElement ( ( result . value as any ) [ 'element-6066-11e4-a52e-4f735466cecf' ] , 'xpath' , './..' , ( parentResult ) => {
54
+ if ( parentResult . status === 0 ) {
55
+ browser . elementIdClick ( ( parentResult . value as any ) [ 'element-6066-11e4-a52e-4f735466cecf' ] )
56
+ . pause ( 100 ) // Wait for folder to expand and DOM to update
57
+ . perform ( ( ) => expandNextClosedFolder ( ) ) // Look for next closed folder
58
+ } else {
59
+ // Failed to find parent, try alternative approach
60
+ browser . click ( closedFolderSelector )
61
+ . pause ( 100 )
62
+ . perform ( ( ) => expandNextClosedFolder ( ) ) // recursive call
63
+ }
64
+ } )
65
+ } else {
66
+ if ( done ) done ( )
67
+ }
68
+ } )
69
+ }
70
+
71
+ expandNextClosedFolder ( )
72
+ } )
73
+ }
74
+
75
+ module . exports = ExpandAllFolders
0 commit comments