11const puppeteer = require ( 'puppeteer' )
22import { expect } from 'chai'
3- import { directJSHandle , directPageHandle } from '../src/index'
3+ import { directJSHandle , getWindowHandle } from '../src/index'
44
55
66describe ( 'puppeteer-handle' , ( ) => {
@@ -12,94 +12,92 @@ describe('puppeteer-handle', () => {
1212
1313 after ( async ( ) => await this . browser . close ( ) )
1414
15+ const ev = func => directJSHandle ( this . page . evaluateHandle ( func ) )
16+
1517 it ( 'should seamlessly evaluate into browser-side objects' , async ( ) => {
1618 const handle = await this . page . evaluateHandle ( ( ) => ( { a : { b : ( ) => 1 } } ) )
1719 const pHandle = directJSHandle ( handle )
1820 expect ( await pHandle . a . b ( ) ) . to . equal ( 1 )
1921 } )
2022
2123 it ( 'should enable thisArg' , async ( ) => {
22- const handle = await this . page . evaluateHandle ( ( ) => ( { a : { val : 2 , b : function ( ) { return this . val ; } } } ) )
23- const pHandle = directJSHandle ( handle )
24- expect ( await pHandle . a . b ( ) ) . to . equal ( 2 )
24+ const handle = ev ( ( ) => ( { a : { val : 2 , b : function ( ) { return this . val ; } } } ) )
25+ expect ( await handle . a . b ( ) ) . to . equal ( 2 )
2526 } )
2627
2728 it ( 'should enable arguments' , async ( ) => {
28- const handle = await this . page . evaluateHandle ( ( ) => ( { add : ( a , b ) => a + b } ) )
29- const pHandle = directJSHandle ( handle )
30- expect ( await pHandle . add ( 1 , 4 ) ) . to . equal ( 5 )
29+ const handle = ev ( ( ) => ( { add : ( a , b ) => a + b } ) )
30+
31+ expect ( await handle . add ( 1 , 4 ) ) . to . equal ( 5 )
3132 } )
3233
3334
3435 it ( 'should enable async functions' , async ( ) => {
35- const handle = await this . page . evaluateHandle ( ( ) => ( { later : ( ) => Promise . resolve ( 4 ) } ) )
36- const pHandle = directJSHandle ( handle )
37- expect ( await pHandle . later ( ) ) . to . equal ( 4 )
36+ const handle = ev ( ( ) => ( { later : ( ) => Promise . resolve ( 4 ) } ) )
37+
38+ expect ( await handle . later ( ) ) . to . equal ( 4 )
3839 } )
3940
4041 it ( 'shoult evaluate strings' , async ( ) => {
41- const handle = await this . page . evaluateHandle ( ( ) => ( { str : '123' } ) )
42- const pHandle = directJSHandle ( handle )
43- expect ( await pHandle . str ) . to . equal ( '123' )
42+ const handle = ev ( ( ) => ( { str : '123' } ) )
43+
44+ expect ( await handle . str ) . to . equal ( '123' )
4445 } )
4546
4647 it ( 'shoult evaluate numbers' , async ( ) => {
47- const handle = await this . page . evaluateHandle ( ( ) => ( { num : 789 } ) )
48- const pHandle = directJSHandle ( handle )
49- expect ( await pHandle . num ) . to . equal ( 789 )
48+ const handle = ev ( ( ) => ( { num : 789 } ) )
49+
50+ expect ( await handle . num ) . to . equal ( 789 )
5051 } )
5152
5253 it ( 'shoult evaluate objects' , async ( ) => {
53- const handle = await this . page . evaluateHandle ( ( ) => ( { a : { b : 'something' } } ) )
54- const pHandle = directJSHandle ( handle )
55- expect ( await pHandle . a ) . to . deep . equal ( { b : 'something' } )
54+ const handle = ev ( ( ) => ( { a : { b : 'something' } } ) )
55+
56+ expect ( await handle . a ) . to . deep . equal ( { b : 'something' } )
5657 } )
5758
5859 it ( 'should allow for a "then" property' , async ( ) => {
59- const handle = await this . page . evaluateHandle ( ( ) => ( { then : 9 } ) )
60- const pHandle = directJSHandle ( handle )
61- expect ( await pHandle . then ) . to . equal ( 9 )
60+ const handle = ev ( ( ) => ( { then : 9 } ) )
61+
62+ expect ( await handle . then ) . to . equal ( 9 )
6263 } )
6364
6465
6566 it ( 'should allow functions in main object' , async ( ) => {
66- const handle = await this . page . evaluateHandle ( ( ) => ( { a : ( ) => 12 } ) )
67- const pHandle = directJSHandle ( handle )
68- expect ( await pHandle . a ( ) ) . to . equal ( 12 )
67+ const handle = ev ( ( ) => ( { a : ( ) => 12 } ) )
68+
69+ expect ( await handle . a ( ) ) . to . equal ( 12 )
6970 } )
7071
7172 it ( 'should work with a promise' , async ( ) => {
72- const pHandle = directJSHandle ( this . page . evaluateHandle ( ( ) => ( { a : ( ) => 32 } ) ) )
73- expect ( await pHandle . a ( ) ) . to . equal ( 32 )
73+ const handle = ev ( ( ) => ( { a : ( ) => 32 } ) )
74+ expect ( await handle . a ( ) ) . to . equal ( 32 )
7475 } )
7576
7677 it ( 'should work with window' , async ( ) => {
77- const window = directJSHandle ( this . page . evaluateHandle ( ( ) => window ) )
78+ const window = ev ( ( ) => window )
7879 expect ( await window . document . body . tagName ) . to . equal ( 'BODY' )
7980 } )
8081
8182 it ( 'should evaluate on window' , async ( ) => {
82- expect ( await directPageHandle ( this . page ) . document . body . tagName ) . to . equal ( 'BODY' )
83+ expect ( await getWindowHandle ( this . page ) . document . body . tagName ) . to . equal ( 'BODY' )
8384 } )
8485
8586 it ( 'should allow a callback' , async ( ) => {
86- const handle = this . page . evaluateHandle ( ( ) => ( { callback : cb => cb ( 'yes' ) } ) )
87- const pHandle = directJSHandle ( handle )
88- const value = await new Promise ( resolve => pHandle . callback ( resolve ) )
87+ const handle = ev ( ( ) => ( { callback : cb => cb ( 'yes' ) } ) )
88+ const value = await new Promise ( resolve => handle . callback ( resolve ) )
8989 expect ( value ) . to . equal ( 'yes' )
9090 } )
9191
9292 it ( 'should allow a callback in any arg' , async ( ) => {
93- const handle = await this . page . evaluateHandle ( ( ) => ( { add : ( num , cb ) => cb ( num + 1 ) } ) )
94- const pHandle = directJSHandle ( handle )
95- const value = await new Promise ( resolve => pHandle . add ( 1 , resolve ) )
93+ const handle = ev ( ( ) => ( { add : ( num , cb ) => cb ( num + 1 ) } ) )
94+
95+ const value = await new Promise ( resolve => handle . add ( 1 , resolve ) )
9696 expect ( value ) . to . equal ( 2 )
9797 } )
9898
9999 it ( 'should work on the main handle' , async ( ) => {
100- const handle = await this . page . evaluateHandle ( ( ) => 'root' )
101- const pHandle = directJSHandle ( handle )
102- const value = await pHandle
100+ const value = await ev ( ( ) => 'root' )
103101 expect ( value ) . to . equal ( 'root' )
104102 } )
105103} )
0 commit comments