1
1
import { ProtoValue } from './value' ;
2
2
import { JSHandle } from 'puppeteer' ;
3
3
import { Flagpole } from '.' ;
4
+ import { Link } from './link' ;
5
+ import { Scenario } from './scenario' ;
6
+ import { ResponseType , iResponse } from './response' ;
4
7
5
8
export class NodeElement extends ProtoValue {
6
9
7
10
public async isFormTag ( ) : Promise < boolean > {
8
11
if ( this . isCheerioElement ( ) || this . isPuppeteerElement ( ) ) {
9
- return await this . getTagName ( ) = == 'form' ;
12
+ return ( await this . getTagName ( ) ) == 'form' ;
10
13
}
11
14
return false ;
12
15
}
@@ -154,6 +157,10 @@ export class NodeElement extends ProtoValue {
154
157
return null ;
155
158
}
156
159
160
+ public async hasAttribute ( key : string ) : Promise < boolean > {
161
+ return ( await this . getAttribute ( key ) ) !== null ;
162
+ }
163
+
157
164
public async getAttribute ( key : string ) : Promise < any > {
158
165
if ( this . isCheerioElement ( ) ) {
159
166
return ( typeof this . _input . get ( 0 ) . attribs [ key ] !== 'undefined' ) ?
@@ -184,6 +191,10 @@ export class NodeElement extends ProtoValue {
184
191
return null ;
185
192
}
186
193
194
+ public async hasData ( key : string ) : Promise < boolean > {
195
+ return ( await this . getData ( key ) ) !== null ;
196
+ }
197
+
187
198
public async getData ( key : string ) : Promise < any > {
188
199
let text : any ;
189
200
if ( this . isCheerioElement ( ) ) {
@@ -290,4 +301,78 @@ export class NodeElement extends ProtoValue {
290
301
return null ;
291
302
}
292
303
304
+ /**
305
+ * Load the URL from this NodeElement if it has something to load
306
+ * This is used to create a lambda scenario
307
+ *
308
+ * @param a
309
+ */
310
+ public async load ( a ?: string | Function , b ?: Function ) : Promise < Scenario > {
311
+ const element : NodeElement = this ;
312
+ const srcPath : string | null = await element . getUrl ( ) ;
313
+ const link : Link = new Link ( element . _context . response , srcPath || '' ) ;
314
+ const title : string = typeof a == 'string' ? a : `Load ${ srcPath } ` ;
315
+ const callback : Function = ( function ( ) {
316
+ // Handle overloading
317
+ if ( typeof b == 'function' ) {
318
+ return b ;
319
+ }
320
+ else if ( typeof a == 'function' ) {
321
+ return a ;
322
+ }
323
+ // No callback was set, so just create a blank one
324
+ else {
325
+ return function ( ) { } ;
326
+ }
327
+ } ) ( ) ;
328
+ const scenario : Scenario = element . _context . suite . Scenario ( title ) ;
329
+ // Is this link one that we can actually load?
330
+ if ( link . isNavigation ( ) ) {
331
+ const scenarioType : string = await element . getLambdaScenarioType ( ) ;
332
+ const uri : string = link . getUri ( ) ;
333
+ // Get the options or just pass the default
334
+ const opts : any = (
335
+ ( scenarioType == 'browser' && element . _context . scenario . responseType == ResponseType . browser ) ||
336
+ scenarioType != 'browser'
337
+ ) ? element . _context . scenario . getRequestOptions ( ) : { } ;
338
+ // Initialize the scenario
339
+ scenario [ scenarioType ] ( opts ) ;
340
+ // Set our callback
341
+ scenario . next ( callback ) ;
342
+ // Done. Execute it asynchronously
343
+ setTimeout ( ( ) => {
344
+ scenario . open ( uri ) ;
345
+ } , 1 ) ;
346
+ }
347
+ else {
348
+ scenario . skip ( 'Not a navigational link' ) ;
349
+ }
350
+ return scenario ;
351
+ }
352
+
353
+ public async getLambdaScenarioType ( ) : Promise < string > {
354
+ if (
355
+ ( await this . isFormTag ( ) ) || ( await this . isClickable ( ) )
356
+ ) {
357
+ // Assume if we are already in browser mode, we want to stay there
358
+ return ( this . _context . scenario . responseType == ResponseType . browser ) ?
359
+ 'browser' : 'html' ;
360
+ }
361
+ else if ( await this . isImageTag ( ) ) {
362
+ return 'image' ;
363
+ }
364
+ else if ( await this . isStylesheetTag ( ) ) {
365
+ return 'stylesheet' ;
366
+ }
367
+ else if ( await this . isScriptTag ( ) ) {
368
+ return 'script' ;
369
+ }
370
+ else if ( await this . isVideoTag ( ) ) {
371
+ return 'video'
372
+ }
373
+ else {
374
+ return 'resource' ;
375
+ }
376
+ }
377
+
293
378
}
0 commit comments