import { Application } from "../../../automation" ; import { SmokeTestsConstants } from "./smokeTestsConstants" ; export interface IStackFrame { name : string ; lineNumber : number ; } const DISCONNECT = `.debug-toolbar .action-label[title*="Disconnect"]` ; const TOOLBAR_HIDDEN = `.debug-toolbar[aria-hidden="true"]` ; const NOT_DEBUG_STATUS_BAR = `.statusbar:not(debugging)` ; const STOP = `.debug-toolbar .action-label[title*="Stop"]` ; export default class AutomationHelper { constructor ( private app ) { } private async retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount = 2000 , pollRetryInterval = 100 , catchFunc ? ) { let tryes = retryCount ; while ( tryes > 0 ) { try { return await this . app . workbench . code . executeWithSpecifiedPollRetryParameters ( func , pollRetryCount , pollRetryInterval ) ; } catch ( e ) { if ( catchFunc ) { await catchFunc ( ) ; } tryes -- ; if ( tryes === 0 ) { throw e ; } } } } public async openFileWithRetry ( fileName , retryCount = 3 , pollRetryCount = 3 , pollRetryInterval = 1000 ) { const func = async ( ) => await this . app . workbench . quickaccess . openFile ( fileName ) ; const catchFunc = async ( ) => await this . app . workbench . code . dispatchKeybinding ( "escape" ) ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async runCommandWithRetry ( commandId , retryCount = 3 , pollRetryCount = 3 , pollRetryInterval = 1000 ) { const func = async ( ) => await this . app . workbench . quickaccess . runCommand ( commandId ) ; const catchFunc = async ( ) => await this . app . workbench . code . dispatchKeybinding ( "escape" ) ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async waitForOutputWithRetry ( string , retryCount = 2 , pollRetryCount = 100 , pollRetryInterval = 100 ) { const func = async ( ) => await this . app . workbench . debug . waitForOutput ( output => output . some ( line => line . indexOf ( string ) >= 0 ) ) ; const catchFunc = async ( ) => await this . app . workbench . debug . stepOver ( ) ; return this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async runDebugScenarioWithRetry ( scenario , index = 0 , retryCount = 3 , pollRetryCount = 30 , pollRetryInterval = 1000 ) { const func = async ( ) => { await this . app . workbench . quickaccess . runDebugScenario ( scenario , index ) ; await this . app . workbench . debug . waitForDebugToolbarExist ( ) ; } ; const catchFunc = async ( ) => await this . app . workbench . code . dispatchKeybinding ( "escape" ) ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async waitForStackFrameWithRetry ( func , message , retryCount = 3 , pollRetryCount = 30 , pollRetryInterval = 1000 , beforeWaitForStackFrame ? ) { const retryFunc = async ( ) => { if ( beforeWaitForStackFrame ) { await beforeWaitForStackFrame ( ) ; } await this . app . workbench . debug . openDebugViewlet ( ) ; let stackFrame = undefined ; try { await this . app . workbench . debug . waitForStackFrame ( ( sf ) => { stackFrame = sf ; return func ( sf ) ; } , message ) ; } catch ( error ) { if ( stackFrame && ! func ( stackFrame ) ) { await this . app . workbench . debug . continue ( ) ; } await this . app . workbench . debug . waitForStackFrame ( ( sf ) => { return func ( sf ) ; } , message ) ; } } ; const catchFunc = async ( ) => { await this . runCommandWithRetry ( SmokeTestsConstants . reloadAppCommand ) ; } ; await this . retryWithSpecifiedPollRetryParameters ( retryFunc , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async disconnectFromDebuggerWithRetry ( retryCount = 3 , pollRetryCount = 10 , pollRetryInterval = 1000 ) { const func = async ( ) => { try { await Promise . race ( [ this . app . workbench . code . waitAndClick ( DISCONNECT ) , this . app . workbench . code . waitAndClick ( STOP ) , ] ) ; } catch ( e ) { } await this . app . workbench . code . waitForElement ( TOOLBAR_HIDDEN ) ; await this . app . workbench . code . waitForElement ( NOT_DEBUG_STATUS_BAR ) ; } ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval ) ; } public async stopDebuggingWithRetry ( retryCount = 3 , pollRetryCount = 10 , pollRetryInterval = 1000 ) { const func = async ( ) => { await this . app . workbench . code . waitAndClick ( STOP ) ; await this . app . workbench . code . waitForElement ( TOOLBAR_HIDDEN ) ; await this . app . workbench . code . waitForElement ( NOT_DEBUG_STATUS_BAR ) ; } ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval ) ; } public async prepareForDebugScenarioCreactionTestWithRetry ( retryCount = 3 , pollRetryCount = 10 , pollRetryInterval = 1000 ) { const func = async ( ) => { await this . app . workbench . debug . openDebugViewlet ( ) ; await this . app . workbench . debug . configure ( ) ; await this . app . workbench . terminal . showTerminalWithoutNecessaryFocus ( ) ; } ; const catchFunc = async ( ) => await this . app . workbench . code . dispatchKeybinding ( "escape" ) ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async openDynamicDebugScenariosWithRetry ( retryCount = 3 , pollRetryCount = 10 , pollRetryInterval = 1000 ) { const func = async ( ) => { await this . app . workbench . quickaccess . openDynamicDebugScenarios ( "React Native..." , 1 ) ; await this . app . workbench . quickinput . waitForQuickInputOpened ( ) ; } ; const catchFunc = async ( ) => await this . app . workbench . code . dispatchKeybinding ( "escape" ) ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval , catchFunc ) ; } public async addConfigurationWithRetry ( retryCount = 3 , pollRetryCount = 10 , pollRetryInterval = 1000 ) { const func = async ( ) => { await this . app . workbench . debug . addConfiguration ( ) ; await this . app . workbench . quickinput . waitForQuickInputOpened ( pollRetryCount ) ; } ; await this . retryWithSpecifiedPollRetryParameters ( func , retryCount , pollRetryCount , pollRetryInterval ) ; } }