/*********************************************************************
 * Copyright (c) 2019 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 **********************************************************************/ import 'reflect-metadata' ; import Axios from 'axios' ; import { CLASSES } from '../inversify.types' ; import { inject , injectable } from 'inversify' ; import { By , error , Key } from 'selenium-webdriver' ; import { Ide } from '../pageobjects/ide/Ide' ; import { Terminal } from '../pageobjects/ide/Terminal' ; import { TopMenu } from '../pageobjects/ide/TopMenu' ; import { DialogWindow } from '../pageobjects/ide/DialogWindow' ; import { DriverHelper } from '../utils/DriverHelper' ; import { PreviewWidget } from '../pageobjects/ide/PreviewWidget' ; import { RightToolBar } from '../pageobjects/ide/RightToolBar' ; import { Logger } from '../utils/Logger' ; import { QuickOpenContainer } from '../pageobjects/ide/QuickOpenContainer' ; @ injectable ( ) export class CodeExecutionTests { private static lastApplicationUrl : string = '' ; constructor ( @ inject ( CLASSES . Terminal ) private readonly terminal , @ inject ( CLASSES . TopMenu ) private readonly topMenu , @ inject ( CLASSES . Ide ) private readonly ide , @ inject ( CLASSES . DialogWindow ) private readonly dialogWindow , @ inject ( CLASSES . DriverHelper ) private readonly driverHelper , @ inject ( CLASSES . PreviewWidget ) private readonly previewWidget , @ inject ( CLASSES . RightToolBar ) private readonly rightToolBar , @ inject ( CLASSES . QuickOpenContainer ) private readonly quickOpenContainer ) { } public runTask ( taskName , timeout ) { test ( `Run command '${ taskName }'` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . terminal . waitIconSuccess ( taskName , timeout ) ; } ) ; } public runTaskInputText ( taskName , expectedQuery , inputText , timeout ) { test ( `Run command '${ taskName }' expecting dialog shell` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . terminal . waitText ( taskName , expectedQuery , timeout ) ; await this . terminal . clickOnTab ( taskName ) ; await this . terminal . type ( taskName , inputText ) ; await this . terminal . type ( taskName , Key . ENTER ) ; await this . terminal . waitIconSuccess ( taskName , timeout ) ; } ) ; } public runTaskConsoleOutput ( taskName , expectedText , timeout ) { test ( `Run command '${ taskName }' expecting console putput: ${ expectedText }` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . terminal . waitText ( taskName , expectedText , timeout ) ; } ) ; } public runTaskWithDialogShellAndOpenLink ( taskName , expectedDialogText , timeout ) { test ( `Run command '${ taskName }' expecting dialog shell` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . dialogWindow . waitDialogAndOpenLink ( expectedDialogText , timeout ) ; } ) ; } public runTaskWithDialogShellDjangoWorkaround ( taskName , expectedDialogText , urlSubPath , timeout ) { test ( `Run command '${ taskName }' expecting dialog shell` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . dialogWindow . waitDialog ( expectedDialogText , timeout ) ; const dialogRedirectUrl = await this . dialogWindow . getApplicationUrlFromDialog ( expectedDialogText ) ; const augmentedPreviewUrl = dialogRedirectUrl + urlSubPath ; await this . dialogWindow . closeDialog ( ) ; await this . dialogWindow . waitDialogDissappearance ( ) ; await this . driverHelper . getDriver ( ) . wait ( async ( ) => { try { const res = await Axios . get ( augmentedPreviewUrl ) ; if ( res . status === 200 ) { return true ; } } catch ( error ) { await this . driverHelper . wait ( 1000 ) ; } } , timeout ) ; } ) ; } public runTaskWithDialogShellAndClose ( taskName , expectedDialogText , timeout ) { test ( `Run command '${ taskName }' expecting dialog shell` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . dialogWindow . waitDialog ( expectedDialogText , timeout ) ; await this . dialogWindow . closeDialog ( ) ; await this . dialogWindow . waitDialogDissappearance ( ) ; } ) ; } public runTaskWithNotification ( taskName , notificationText , timeout ) { test ( `Run command '${ taskName }' expecting notification pops up` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . ide . waitNotification ( notificationText , timeout ) ; } ) ; } public runTaskWithNotificationAndOpenLink ( taskName , notificationText , buttonText , timeout ) { test ( `Run command '${ taskName }' expecting notification` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . ide . waitNotification ( notificationText , timeout ) ; await this . ide . clickOnNotificationButton ( notificationText , buttonText ) ; CodeExecutionTests . lastApplicationUrl = await this . previewWidget . getUrl ( ) ; } ) ; } public runTaskWithNotificationAndOpenLinkPreviewNoUrl ( taskName , notificationText , timeout ) { test ( `Run command '${ taskName }' expecting notification` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . ide . waitNotification ( notificationText , timeout ) ; await this . ide . clickOnNotificationButton ( notificationText , 'Open In Preview' ) ; CodeExecutionTests . lastApplicationUrl = await this . previewWidget . getUrl ( ) ; } ) ; } public runTaskWithNotificationAndOpenLinkUnexposedPort ( taskName , notificationText , portOpenText , timeout ) { test ( `Run command '${ taskName }' expecting notification with unexposed port` , async ( ) => { await this . runTaskUsingQuickOpenContainer ( taskName ) ; await this . ide . waitNotificationAndConfirm ( notificationText , timeout ) ; await this . ide . waitNotificationAndOpenLink ( portOpenText , timeout ) ; CodeExecutionTests . lastApplicationUrl = await this . previewWidget . getUrl ( ) ; } ) ; } public verifyRunningApplication ( locator , applicationCheckTimeout , polling ) { test ( `Verify running application by locator: '${ locator }'` , async ( ) => { await this . previewWidget . waitApplicationOpened ( CodeExecutionTests . lastApplicationUrl , applicationCheckTimeout ) ; try { await this . previewWidget . waitContentAvailable ( locator , applicationCheckTimeout , polling ) ; } catch ( err ) { if ( err instanceof error . TimeoutError ) { Logger . warn ( `CodeExecutionTests.verifyRunningApplication application not located, probably blocked by preloader or content not available. Retrying.` ) ; await this . driverHelper . getDriver ( ) . switchTo ( ) . defaultContent ( ) ; await this . ide . waitAndSwitchToIdeFrame ( ) ; await this . previewWidget . refreshPage ( ) ; await this . previewWidget . waitContentAvailable ( locator , applicationCheckTimeout , polling ) ; } } } ) ; } public getLastApplicationUrl ( ) { return CodeExecutionTests . lastApplicationUrl ; } public refreshPreviewWindow ( ) { test ( 'Refreshing preview widget' , async ( ) => { await this . previewWidget . refreshPage ( ) ; } ) ; } public closePreviewWidget ( ) { test ( 'Close preview widget' , async ( ) => { await this . rightToolBar . clickOnToolIcon ( 'Preview' ) ; await this . previewWidget . waitPreviewWidgetAbsence ( ) ; } ) ; } public waitTerminalPresent ( terminalTabName , timeout ) { test ( 'Wait terminal presence' , async ( ) => { await this . terminal . waitTab ( terminalTabName , timeout ) ; await this . driverHelper . wait ( 5000 ) ; } ) ; } public closeTerminal ( taskName ) { test ( 'Close the terminal tasks' , async ( ) => { await this . ide . closeAllNotifications ( ) ; await this . terminal . rejectTerminalProcess ( taskName ) ; await this . terminal . closeTerminalTab ( taskName ) ; await this . dialogWindow . waitAndCloseIfAppear ( ) ; } ) ; } private async runTaskUsingQuickOpenContainer ( taskName ) { await this . topMenu . selectOption ( 'Terminal' , 'Run Task...' ) ; try { await this . quickOpenContainer . waitContainer ( ) ; } catch ( err ) { Logger . warn ( `CodeExecutionTests.runTaskInputText quickOpenContainer failed to open, retrying.` ) ; await this . topMenu . selectOption ( 'Terminal' , 'Run Task...' ) ; await this . quickOpenContainer . waitContainer ( ) ; } await this . quickOpenContainer . type ( `${ taskName }${ Key . ENTER }` ) ; } }