import { Application } from 'spectron' ; import { Screenshot } from '../helpers/screenshot' ; /**
 * Abstracts the Spectron's WebdriverIO managed client property on the created Application instances.
 */ export class SpectronClient { constructor ( private spectron , private shot ) { } public windowByIndex ( index ) { return this . spectron . client . windowByIndex ( index ) ; } public async keys ( keys , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . keys ( keys ) ; } public async getText ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . getText ( selector ) ; } public async getHTML ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . getHTML ( selector ) ; } public async click ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . click ( selector ) ; } public async doubleClick ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . doubleClick ( selector ) ; } public async leftClick ( selector , xoffset , yoffset , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . leftClick ( selector , xoffset , yoffset ) ; } public async rightClick ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . rightClick ( selector ) ; } public async moveToObject ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . moveToObject ( selector ) ; } public async setValue ( selector , text , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . setValue ( selector , text ) ; } public async elements ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . elements ( selector ) ; } public async element ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . element ( selector ) ; } public async dragAndDrop ( sourceElem , destinationElem , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . dragAndDrop ( sourceElem , destinationElem ) ; } public async selectByValue ( selector , value , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . selectByValue ( selector , value ) ; } public async getValue ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . getValue ( selector ) ; } public async getAttribute ( selector , attribute , capture = true ) { await this . screenshot ( capture ) ; return Promise . resolve ( this . spectron . client . getAttribute ( selector , attribute ) ) ; } public clearElement ( selector ) { return this . spectron . client . clearElement ( selector ) ; } public buttonDown ( ) { return this . spectron . client . buttonDown ( ) ; } public buttonUp ( ) { return this . spectron . client . buttonUp ( ) ; } public async isVisible ( selector , capture = true ) { await this . screenshot ( capture ) ; return this . spectron . client . isVisible ( selector ) ; } public getTitle ( ) { return this . spectron . client . getTitle ( ) ; } private async screenshot ( capture ) { if ( capture ) { try { await this . shot . capture ( ) ; } catch ( e ) { throw new Error ( `Screenshot could not be captured: ${ e }` ) ; } } } public async getTerminalText ( ) { const PANEL_SELECTOR = 'div[id="workbench.panel.terminal"]' ; const XTERM_SELECTOR = `${ PANEL_SELECTOR } .terminal-wrapper` ; return await this . spectron . client . selectorExecute ( XTERM_SELECTOR , div => { const xterm = ( ( Array . isArray ( div ) ? div [ 0 ] : div ) as any ) . xterm ; const buffer = xterm . _core . buffer ; const lines = [ ] ; for ( let i = 0 ; i < buffer . lines . length ; i ++ ) { lines . push ( buffer . translateBufferLineToString ( i , true ) ) ; } return lines ; } ) ; } }