import path = require ( "path" ) ; import fs = require ( "fs-extra" ) ; import { COMPILE_KEY_FILE , DELETE_DATA , INCLUDE_PATH_DATA , PROJECT_FOLDER , } from "./scenario" ; import { By , VSBrowser , Workbench , Notification , InputBox , WebElement , ActivityBar , SideBarView , QuickPickItem , ContextMenu , ViewItem , ContextMenuItem , ViewControl , EditorView , } from "vscode-extension-tester" ; import { expect } from "chai" ; import { IUserData } from "./page-objects/interface-po" ; import { setTimeout } from "timers/promises" ; const DEFAULT_DELAY = 1000 ; function clearServersJson ( projectFolder ) { const serversJsonFile = path . join ( projectFolder , ".vscode" , "servers.json" ) ; if ( fs . existsSync ( serversJsonFile ) ) { fs . removeSync ( serversJsonFile ) ; } const launchJsonFile = path . join ( projectFolder , ".vscode" , "launch.json" ) ; if ( fs . existsSync ( launchJsonFile ) ) { fs . removeSync ( launchJsonFile ) ; } } async function closeAllEditors ( ) { const editorView = new EditorView ( ) ; await editorView . closeAllEditors ( ) ; await delay ( ) ; } export async function openAdvplProject ( ) { clearServersJson ( PROJECT_FOLDER ) ; await VSBrowser . instance . openResources ( PROJECT_FOLDER ) ; await delay ( 2000 ) ; closeAllEditors ( ) ; } export async function readServersJsonFile ( ) { const serversJsonFile = path . join ( PROJECT_FOLDER , ".vscode" , "servers.json" ) ; let result = "< file not found >" ; if ( fs . existsSync ( serversJsonFile ) ) { const buffer = fs . readFileSync ( serversJsonFile ) ; result = buffer . toString ( ) ; } return result ; } export const delay = async ( duration = DEFAULT_DELAY ) => { await setTimeout ( duration ) ; } ; export const avoidsBacksliding = async ( ) => { await delay ( 3000 ) ; } ; export async function takeQuickPickAction ( pickBox , titleAction ) { const actionContainer = pickBox . findElement ( By . className ( "actions-container" ) ) ; const actionList = await actionContainer . findElements ( By . className ( "action-item" ) ) ; const actions = actionList . filter ( async ( action ) => { const link = await action . findElement ( By . css ( "a.action-label" ) ) ; const title = ( await link . getAttribute ( "title" ) ) . toLowerCase ( ) ; return title === titleAction . toLowerCase ( ) ; } ) ; if ( actions . length == 1 ) { await actions [ 0 ] . click ( ) ; await delay ( ) ; return true ; } return false ; } export async function fillEnvironment ( environment ) { const pickBox = new InputBox ( ) ; await delay ( ) ; let title = await pickBox . getTitle ( ) ; expect ( title ) . is . equal ( "Connection (1/1)" ) ; const quickPicks = await pickBox . getQuickPicks ( ) ; const find = quickPicks . filter ( async ( element ) => { return ( await element . getText ( ) ) == environment ; } ) . length > 0 ; if ( ! find ) { expect ( await takeQuickPickAction ( pickBox , "action" ) ) . is . true ; title = await pickBox . getMessage ( ) ; expect ( title . startsWith ( "Enter the name of the environment" ) ) . is . true ; await delay ( ) ; } await pickBox . setText ( environment ) ; await delay ( ) ; await pickBox . confirm ( ) ; await delay ( ) ; } export async function fillUserdata ( userData ) { const pickBox = new InputBox ( ) ; await delay ( ) ; pickBox . wait ( ) ; let title = await pickBox . getTitle ( ) ; title = await pickBox . getTitle ( ) ; expect ( title ) . is . equal ( "Authentication (1/2)" ) ; await pickBox . setText ( userData . username ) ; await delay ( ) ; await pickBox . confirm ( ) ; await delay ( ) ; await pickBox . wait ( ) ; title = await pickBox . getTitle ( ) ; expect ( title ) . is . equal ( "Authentication (2/2)" ) ; await pickBox . setText ( userData . password ) ; await delay ( ) ; await pickBox . confirm ( ) ; await delay ( ) ; } export async function fireContextMenuAction ( element , name ) { const menu = await element . openContextMenu ( ) ; await menu . wait ( 2000 ) ; const action = await menu . getItem ( name ) ; await action . click ( ) ; await delay ( ) ; }