@@ -33,84 +33,93 @@ export const DEFAULT_INPUT_VALUES = {
33
33
// ... [rest of the default input values]
34
34
} ;
35
35
36
- export const fillForms = async ( page : Page , timeout = 30000 ) => {
37
- console . log ( 'Filling out forms' ) ;
38
- let isDone = false ;
36
+ export const fillForms = async ( page : Page , timeout = 6000 ) => {
37
+ let isInteracting = false ;
39
38
40
39
const timeoutPromise = new Promise ( resolve => {
41
40
setTimeout ( ( ) => {
42
- isDone = true ;
43
- console . log ( 'Timeout reached. Exiting fillForms().' ) ;
41
+ if ( isInteracting ) {
42
+ // console.log('Interaction ongoing. Waiting for safe exit.');
43
+ return ;
44
+ }
45
+ // console.log('Timeout reached. Exiting fillForms().');
44
46
resolve ( 'Timeout' ) ;
45
47
} , timeout ) ;
46
48
} ) ;
47
49
48
50
const fillPromise = async ( ) => {
51
+ console . log ( 'Entering fillPromise.' ) ;
49
52
try {
50
- console . log ( 'Checking for inputs on the page' ) ;
51
- if ( isDone ) {
52
- return ;
53
- }
54
- const elements = await page . $$ ( 'input' ) ;
55
- console . log ( `Found ${ elements . length } input elements` ) ;
56
- let count = 0 ;
57
- for ( const el of elements ) {
58
- if ( isDone ) {
59
- return ;
60
- }
61
- if ( count > 100 ) {
62
- break ;
63
- }
64
- count += 1 ;
65
- const pHandle = await el . getProperty ( 'type' ) ;
66
- const pValue = await pHandle . jsonValue ( ) ;
67
- // console.log(`Input is type ${pValue}`);
53
+ if ( ! page . isClosed ( ) ) {
54
+ console . log ( 'Checking for inputs on the page' ) ;
55
+ const elements = await page . $$ ( 'input' ) ;
56
+ console . log ( `Found ${ elements . length } input elements` ) ;
57
+ let count = 0 ;
58
+ for ( const el of elements ) {
59
+ if ( ! page . isClosed ( ) ) {
60
+ isInteracting = true ;
68
61
69
- const autoCompleteHandle = await el . getProperty ( 'autocomplete' ) ;
70
- const autoCompleteValue = ( await autoCompleteHandle . jsonValue ( ) ) as string ;
71
- // console.log(`Autocomplete attribute is: ${autoCompleteValue}`);
72
- let autoCompleteKeys = [ ] ;
62
+ // console.log(`Inspecting element ${count}`);
63
+ if ( count > 100 ) {
64
+ break ;
65
+ }
66
+ count += 1 ;
73
67
74
- // console.log('Checking autocomplete value');
75
- if ( autoCompleteValue ) {
76
- if ( autoCompleteValue . includes ( 'cc-name' ) ) {
77
- // console.log('Autocomplete includes cc-name.');
78
- autoCompleteKeys = [ 'cc-name' ] ;
68
+ const pHandle = await el . getProperty ( 'type' ) ;
69
+ const pValue = await pHandle . jsonValue ( ) ;
70
+ // console.log(`Input is type ${pValue}`);
71
+
72
+ const autoCompleteHandle = await el . getProperty ( 'autocomplete' ) ;
73
+ const autoCompleteValue = ( await autoCompleteHandle . jsonValue ( ) ) as string ;
74
+ // console.log(`Autocomplete attribute is: ${autoCompleteValue}`);
75
+ let autoCompleteKeys = [ ] ;
76
+
77
+ // console.log('Checking autocomplete value');
78
+ if ( autoCompleteValue ) {
79
+ if ( autoCompleteValue . includes ( 'cc-name' ) ) {
80
+ // console.log('Autocomplete includes cc-name.');
81
+ autoCompleteKeys = [ 'cc-name' ] ;
82
+ } else {
83
+ // console.log('Autocomplete does not include cc-name.');
84
+ autoCompleteKeys = Object . keys ( DEFAULT_INPUT_VALUES ) . filter ( k => ( autoCompleteValue as string ) . includes ( k ) ) ;
85
+ }
86
+ }
87
+
88
+ if ( pValue === 'submit' || pValue === 'hidden' ) {
89
+ // console.log('Type is either submit or hidden.');
90
+ continue ;
91
+ } else if ( autoCompleteKeys . length > 0 ) {
92
+ // console.log('Autocomplete keys > 0');
93
+ await el . focus ( ) ;
94
+ await page . keyboard . press ( 'Tab' , {
95
+ delay : 100
96
+ } ) ;
97
+ await el . press ( 'Backspace' ) ;
98
+ await page . keyboard . type ( DEFAULT_INPUT_VALUES [ autoCompleteKeys [ 0 ] as string ] ) ;
99
+ } else if ( Object . keys ( DEFAULT_INPUT_VALUES ) . includes ( pValue as string ) ) {
100
+ // console.log('Default input values includes pValue');
101
+ await el . focus ( ) ;
102
+ await page . keyboard . press ( 'Tab' , {
103
+ delay : 100
104
+ } ) ;
105
+ await el . press ( 'Backspace' ) ;
106
+ await page . keyboard . type ( DEFAULT_INPUT_VALUES [ pValue as string ] ) ;
107
+ // console.log(' ... done with test');
108
+ }
109
+ isInteracting = false ;
79
110
} else {
80
- // console.log('Autocomplete does not include cc-name .');
81
- autoCompleteKeys = Object . keys ( DEFAULT_INPUT_VALUES ) . filter ( k => ( autoCompleteValue as string ) . includes ( k ) ) ;
111
+ console . log ( 'Page is closed. Exiting loop .' ) ;
112
+ break ;
82
113
}
83
114
}
84
-
85
- if ( isDone ) {
86
- return ;
87
- } else if ( pValue === 'submit' || pValue === 'hidden' ) {
88
- // console.log('Type is either submit or hidden.');
89
- continue ;
90
- } else if ( autoCompleteKeys . length > 0 ) {
91
- // console.log('Autocomplete keys > 0');
92
- await el . focus ( ) ;
93
- await page . keyboard . press ( 'Tab' , {
94
- delay : 100
95
- } ) ;
96
- await el . press ( 'Backspace' ) ;
97
- await page . keyboard . type ( DEFAULT_INPUT_VALUES [ autoCompleteKeys [ 0 ] as string ] ) ;
98
- } else if ( Object . keys ( DEFAULT_INPUT_VALUES ) . includes ( pValue as string ) ) {
99
- // console.log('Default input values includes pValue');
100
- await el . focus ( ) ;
101
- await page . keyboard . press ( 'Tab' , {
102
- delay : 100
103
- } ) ;
104
- await el . press ( 'Backspace' ) ;
105
- await page . keyboard . type ( DEFAULT_INPUT_VALUES [ pValue as string ] ) ;
106
- // console.log(' ... done with test');
107
- }
115
+ } else {
116
+ console . log ( 'Page is closed. Exiting fillForms.' ) ;
108
117
}
109
118
} catch ( error ) {
110
119
if ( error . message . includes ( 'Execution context was destroyed' ) ) {
111
120
console . log ( 'Page navigated away while interacting. Continuing...' ) ;
112
121
} else {
113
- console . log ( `Error in fillForms: ${ error . message } ` ) ;
122
+ console . error ( `Error in fillForms: ${ error . message } ` ) ;
114
123
}
115
124
} finally {
116
125
console . log ( 'Done with fillForms' ) ;
@@ -121,7 +130,6 @@ export const fillForms = async (page: Page, timeout = 30000) => {
121
130
} ;
122
131
123
132
export const autoScroll = async page => {
124
- console . log ( 'Scrolling the page' ) ;
125
133
await page . evaluate ( async ( ) => {
126
134
return new Promise ( ( resolve , reject ) => {
127
135
try {
@@ -136,12 +144,10 @@ export const autoScroll = async page => {
136
144
count += 1 ;
137
145
if ( totalHeight >= scrollHeight || count > COUNT_MAX ) {
138
146
clearInterval ( timer ) ;
139
- console . log ( 'Done scrolling the page' ) ;
140
147
resolve ( undefined ) ;
141
148
}
142
149
} , 100 ) ;
143
150
} catch ( error ) {
144
- console . log ( `Error scrolling: ${ error . message } ` ) ;
145
151
reject ( error ) ;
146
152
}
147
153
} ) ;
0 commit comments