@@ -4,8 +4,7 @@ import * as os from "os";
4
4
import * as path from "path" ;
5
5
import * as puppeteer from "puppeteer" ;
6
6
import { ChildProcess , spawn } from "child_process" ;
7
- import { logger , field } from "@coder/logger" ;
8
- import { EventEmitter } from 'events' ;
7
+ import { logger , field , Level } from "@coder/logger" ;
9
8
10
9
interface IServerOptions {
11
10
host : string ;
@@ -142,11 +141,22 @@ export class TestPage {
142
141
* Screenshot path will be in the following format:
143
142
* `<screenshotDir>/[<page-tag>_]<screenshot-number>_<screenshot-name>.jpg`.
144
143
*/
145
- public screenshot ( name : string , options ?: puppeteer . ScreenshotOptions ) : Promise < string | Buffer > {
146
- options = Object . assign ( { path : path . resolve ( TestServer . puppeteerDir , `./${ this . tag ? `${ this . tag } _` : "" } ${ this . screenshotCount } _${ name } .jpg` ) , fullPage : true } , options ) ;
147
- const img = this . rootPage . screenshot ( options ) ;
144
+ public async screenshot ( name : string , options ?: puppeteer . ScreenshotOptions ) : Promise < string | Buffer > {
145
+ let debugCI = logger . level === Level . Debug && process . env . TRAVIS_BUILD_NUMBER ;
146
+ let tag = this . tag ? `${ this . tag } _` : "" ;
147
+ if ( debugCI ) {
148
+ tag = `TRAVIS-${ process . env . TRAVIS_BUILD_NUMBER } _${ tag } ` ;
149
+ }
150
+ options = Object . assign ( { path : path . resolve ( TestServer . puppeteerDir , `./${ tag } ${ this . screenshotCount } _${ name } .jpg` ) , fullPage : true } , options ) ;
151
+ const img = await this . rootPage . screenshot ( options ) ;
148
152
this . screenshotCount ++ ;
149
153
154
+ if ( debugCI ) {
155
+ // TODO: upload to imgur.
156
+ const url = "" ;
157
+ logger . debug ( "captured screenshot" , field ( "path" , options . path ) , field ( "url" , url ) ) ;
158
+ }
159
+
150
160
return img ;
151
161
}
152
162
@@ -157,9 +167,18 @@ export class TestPage {
157
167
*/
158
168
// tslint:disable-next-line:no-any
159
169
public async recordFunc ( fn : Function , ...args : any [ ] ) : Promise < any > {
160
- await this . screenshot ( `${ fn . name } _before` ) ;
170
+ let err : Error | undefined ;
171
+ if ( typeof fn !== "function" || fn . name === "" ) {
172
+ err = new Error ( JSON . stringify ( fn ) ) ;
173
+ logger . debug ( "cannot record nameless function" , field ( "trace" , err . stack ) ) ;
174
+ }
175
+ if ( ! err ) {
176
+ await this . screenshot ( `${ fn . name } _before` ) ;
177
+ }
161
178
const result = await fn . apply ( this . rootPage , args ) ;
162
- await this . screenshot ( `${ fn . name } _after` ) ;
179
+ if ( ! err ) {
180
+ await this . screenshot ( `${ fn . name } _after` ) ;
181
+ }
163
182
164
183
return result ;
165
184
}
@@ -179,10 +198,10 @@ export class TestPage {
179
198
*/
180
199
public waitFor ( durationOrSelector : number | string , options ?: puppeteer . WaitForSelectorOptionsHidden | puppeteer . WaitForSelectorOptions ) : Promise < puppeteer . ElementHandle | null | void > {
181
200
if ( typeof durationOrSelector === "number" ) {
182
- return this . recordFunc ( this . rootPage . waitFor , durationOrSelector ) ;
201
+ return this . rootPage . waitFor ( durationOrSelector ) ;
183
202
}
184
203
185
- return this . recordFunc ( this . rootPage . waitFor , durationOrSelector , options ) ;
204
+ return this . rootPage . waitFor ( durationOrSelector , options ) ;
186
205
}
187
206
188
207
/**
@@ -359,14 +378,17 @@ export class TestServer {
359
378
* runner context.
360
379
*/
361
380
// tslint:disable-next-line:no-any
362
- public async querySelectorAll ( page : TestPage , selector : string ) : Promise < Array < DeserializedNode > > {
381
+ public async querySelectorAll ( page : puppeteer . Page | TestPage , selector : string ) : Promise < Array < DeserializedNode > > {
363
382
if ( ! selector ) {
364
383
throw new Error ( "selector undefined" ) ;
365
384
}
366
385
367
386
// tslint:disable-next-line:no-any
368
387
const elements : Array < DeserializedNode > = [ ] ;
369
- const serializedElements = await page . rootPage . evaluate ( ( selector ) => {
388
+ if ( page instanceof TestPage ) {
389
+ page = page . rootPage ;
390
+ }
391
+ const serializedElements = await page . evaluate ( ( selector ) => {
370
392
// tslint:disable-next-line:no-any
371
393
return new Promise < Array < string > > ( ( res , rej ) : void => {
372
394
const elements = Array . from ( document . querySelectorAll ( selector ) ) ;
@@ -396,7 +418,7 @@ export class TestServer {
396
418
* Get an element on the page. See `TestServer.querySelectorAll`.
397
419
*/
398
420
// tslint:disable-next-line:no-any
399
- public async querySelector ( page : TestPage , selector : string ) : Promise < DeserializedNode > {
421
+ public async querySelector ( page : puppeteer . Page | TestPage , selector : string ) : Promise < DeserializedNode > {
400
422
if ( ! selector ) {
401
423
throw new Error ( "selector undefined" ) ;
402
424
}
0 commit comments