11import  { readFileSync }  from  'fs' 
22import  *  as  path  from  'path' 
3- import  { ElementHandle ,  EvaluateFn ,  Page }  from  'puppeteer' 
3+ import  { ElementHandle ,  EvaluateFn ,  JSHandle ,   Page }  from  'puppeteer' 
44import  { ITestUtils }  from  './typedefs' 
55
66const  domLibraryAsString  =  readFileSync ( 
@@ -25,13 +25,36 @@ type DOMReturnType = ElementHandle | ElementHandle[] | null
2525
2626type  ContextFn  =  ( ...args : any [ ] )  =>  ElementHandle 
2727
28+ async  function  createElementHandleArray ( handle : JSHandle ) : Promise < ElementHandle [ ] >  { 
29+   const  lengthHandle  =  await  handle . getProperty ( 'length' ) 
30+   const  length  =  await  lengthHandle . jsonValue ( ) 
31+ 
32+   const  elements : ElementHandle [ ]  =  [ ] 
33+   for  ( let  i  =  0 ;  i  <  length ;  i ++ )  { 
34+     const  jsElement  =  await  handle . getProperty ( i . toString ( ) ) 
35+     const  element  =  await  createElementHandle ( jsElement ) 
36+     if  ( element )  elements . push ( element ) 
37+   } 
38+ 
39+   return  elements 
40+ } 
41+ 
42+ async  function  createElementHandle ( handle : JSHandle ) : Promise < ElementHandle  |  null >  { 
43+   const  element  =  handle . asElement ( ) 
44+   if  ( element )  return  element 
45+   await  handle . dispose ( ) 
46+   return  null  // tslint:disable-line 
47+ } 
48+ 
49+ async  function  covertToElementHandle ( handle : JSHandle ,  asArray : boolean ) : Promise < DOMReturnType >  { 
50+   return  asArray  ? createElementHandleArray ( handle )  : createElementHandle ( handle ) 
51+ } 
52+ 
2853function  createDelegateFor ( 
2954  fnName : keyof  ITestUtils , 
3055  contextFn ?: ContextFn , 
3156) : ( ...args : any [ ] )  =>  Promise < DOMReturnType >  { 
3257  return  async  function ( ...args : any [ ] ) : Promise < DOMReturnType >  { 
33-     if  ( fnName . includes ( 'All' ) )  throw  new  Error ( '*All methods not yet supported' ) 
34- 
3558    // @ts -ignore 
3659    const  containerHandle : ElementHandle  =  contextFn  ? contextFn ( ...args )  : this 
3760    // @ts -ignore 
@@ -45,16 +68,13 @@ function createDelegateFor(
4568    const  handle  =  await  containerHandle 
4669      . executionContext ( ) 
4770      . evaluateHandle ( evaluateFn ,  containerHandle ,  fnName ,  ...argsToForward ) 
48-     const  element  =  handle . asElement ( ) 
49-     if  ( element )  return  element 
50-     await  handle . dispose ( ) 
51-     return  null  // tslint:disable-line 
71+     return  covertToElementHandle ( handle ,  fnName . includes ( 'All' ) ) 
5272  } 
5373} 
5474
55- export  async  function  getDocument ( context ?: Page ) : Promise < ElementHandle >  { 
75+ export  async  function  getDocument ( _page ?: Page ) : Promise < ElementHandle >  { 
5676  // @ts -ignore 
57-   const  page : Page  =  context  ||  this 
77+   const  page : Page  =  _page  ||  this 
5878  const  documentHandle  =  await  page . mainFrame ( ) . evaluateHandle ( 'document' ) 
5979  const  document  =  documentHandle . asElement ( ) 
6080  if  ( ! document )  throw  new  Error ( 'Could not find document' ) 
0 commit comments