import { ElementFinder , by , browser } from 'protractor' ; import { Logger , BrowserVisibility , BrowserActions } from '@alfresco/adf-testing' ; import { Component } from '../component' ; import { Utils , isPresentAndEnabled , waitForPresence , waitForStaleness } from '../../utilities/utils' ; export class Menu extends Component { items = this . allByCss ( '.mat-menu-item' ) ; uploadFilesInput = this . byId ( 'app-upload-files' , browser ) ; submenus = browser . element . all ( by . css ( 'app-context-menu-item .mat-menu-item' ) ) ; uploadFileAction = this . byId ( 'app.create.uploadFile' ) ; uploadFolderAction = this . byId ( 'app.create.uploadFolder' ) ; createFolderAction = this . byId ( 'app.create.folder' ) ; createLibraryAction = this . byId ( 'app.create.library' ) ; createFileFromTemplateAction = this . byId ( 'app.create.fileFromTemplate' ) ; createFolderFromTemplateAction = this . byId ( 'app.create.folderFromTemplate' ) ; cancelEditingAction = this . byCss ( `.mat-menu-item[title='Cancel Editing']` ) ; cancelJoinAction = this . byCssText ( '.mat-menu-item' , 'Cancel Join' ) ; copyAction = this . byTitleAttr ( 'Copy' ) ; downloadAction = this . byCssText ( '.mat-menu-item' , 'Download' ) ; editFolderAction = this . byCss ( `.mat-menu-item[id$='editFolder']` ) ; editOfflineAction = this . byCss ( `.mat-menu-item[title='Edit Offline']` ) ; joinAction = this . byCssText ( '.mat-menu-item' , 'Join' ) ; leaveAction = this . byCssText ( '.mat-menu-item' , 'Leave' ) ; managePermissionsAction = this . byCssText ( '.mat-menu-item' , 'Permissions' ) ; restoreAction = this . byCssText ( '.mat-menu-item' , 'Restore' ) ; shareAction = this . byCssText ( '.mat-menu-item' , 'Share' ) ; shareEditAction = this . byCssText ( '.mat-menu-item' , 'Shared Link Settings' ) ; constructor ( ancestor ? ) { super ( '.mat-menu-panel' , ancestor ) ; } async waitForMenuToOpen ( ) { await waitForPresence ( browser . element ( by . css ( '.cdk-overlay-container .mat-menu-panel' ) ) ) ; await BrowserVisibility . waitUntilElementIsVisible ( this . items . get ( 0 ) ) ; } async waitForMenuToClose ( ) { await waitForStaleness ( browser . element ( by . css ( '.cdk-overlay-container .mat-menu-panel' ) ) ) ; } async closeMenu ( ) { await Utils . pressEscape ( ) ; await this . waitForMenuToClose ( ) ; } getNthItem ( nth ) { return this . items . get ( nth - 1 ) ; } private getItemByLabel ( menuItem ) { return this . byCssText ( '.mat-menu-item' , menuItem ) ; } private getSubItemByLabel ( subMenuItem ) { return this . byCssText ( 'app-context-menu-item .mat-menu-item' , subMenuItem ) ; } getItemById ( id ) { return this . byId ( id ) ; } async getItemTooltip ( menuItem ) { return this . getItemByLabel ( menuItem ) . getAttribute ( 'title' ) ; } async getItemIconText ( menuItem ) { return this . getItemByLabel ( menuItem ) . element ( by . css ( '.mat-icon' ) ) . getText ( ) ; } async getItemIdAttribute ( menuItem ) { return this . getItemByLabel ( menuItem ) . getAttribute ( 'id' ) ; } async getItemsCount ( ) { return this . items . count ( ) ; } async getMenuItems ( ) { return this . items . map ( async ( elem ) => { const span = elem . element ( by . css ( 'span' ) ) ; return span . getText ( ) ; } ) ; } async clickNthItem ( nth ) { try { const elem = this . getNthItem ( nth ) ; await BrowserVisibility . waitUntilElementIsClickable ( elem ) ; await browser . actions ( ) . mouseMove ( elem ) . perform ( ) ; await browser . actions ( ) . click ( ) . perform ( ) ; await this . waitForMenuToClose ( ) ; } catch ( e ) { Logger . error ( '____ click nth menu item catch ___' , e ) ; } } async clickMenuItem ( menuItem ) { try { const elem = this . getItemByLabel ( menuItem ) ; await BrowserActions . click ( elem ) ; } catch ( e ) { Logger . error ( `___click menu item catch : failed to click on ${ menuItem }___` , e ) ; } } async mouseOverMenuItem ( menuItem ) { try { const elem = this . getItemByLabel ( menuItem ) ; await BrowserVisibility . waitUntilElementIsClickable ( elem ) ; await browser . actions ( ) . mouseMove ( elem ) . perform ( ) ; await browser . sleep ( 500 ) ; } catch ( error ) { Logger . error ( `----- mouse over error : failed to mouse over ${ menuItem } : ` , error ) ; } } async hasSubMenu ( menuItem ) { try { const elem = this . getItemByLabel ( menuItem ) ; await BrowserVisibility . waitUntilElementIsClickable ( elem ) ; const elemClass = await elem . getAttribute ( 'class' ) ; return elemClass . includes ( 'mat-menu-item-submenu-trigger' ) ; } catch ( error ) { Logger . error ( '---- has submenu error: ' , error ) ; return false ; } } async clickSubMenuItem ( subMenuItem ) { try { const elem = this . getSubItemByLabel ( subMenuItem ) ; await BrowserActions . click ( elem ) ; } catch ( e ) { Logger . error ( '___click submenu item catch___' , e ) ; } } async isMenuItemPresent ( title ) { return browser . element ( by . cssContainingText ( '.mat-menu-item' , title ) ) . isPresent ( ) ; } async isSubMenuItemPresent ( title ) { return browser . element ( by . cssContainingText ( 'app-context-menu-item .mat-menu-item' , title ) ) . isPresent ( ) ; } async getSubmenuItemsCount ( ) { return this . submenus . count ( ) ; } async isMenuItemDisabled ( title ) { try { const item = this . getItemByLabel ( title ) ; return await item . getAttribute ( 'disabled' ) ; } catch ( error ) { Logger . error ( '----- isMenuItemDisabled catch: ' , error ) ; return null ; } } async isCreateFolderEnabled ( ) { return isPresentAndEnabled ( this . createFolderAction ) ; } async isCreateLibraryEnabled ( ) { return isPresentAndEnabled ( this . createLibraryAction ) ; } async isUploadFileEnabled ( ) { return isPresentAndEnabled ( this . uploadFileAction ) ; } async isUploadFolderEnabled ( ) { return isPresentAndEnabled ( this . uploadFolderAction ) ; } async isCreateFileFromTemplateEnabled ( ) { return isPresentAndEnabled ( this . createFileFromTemplateAction ) ; } async isCreateFolderFromTemplateEnabled ( ) { return isPresentAndEnabled ( this . createFolderFromTemplateAction ) ; } }