import { WebElementWrapper } from '../../../../test/functional/services/lib/web_element_wrapper' ; import { FtrService } from '../ftr_provider_context' ; interface SearchResult { label : string ; } const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; export class NavigationalSearchPageObject extends FtrService { private readonly find = this . ctx . getService ( 'find' ) ; private readonly testSubjects = this . ctx . getService ( 'testSubjects' ) ; async focus ( ) { const field = await this . testSubjects . find ( 'nav-search-input' ) ; await field . click ( ) ; } async blur ( ) { await this . testSubjects . click ( 'helpMenuButton' ) ; await this . testSubjects . click ( 'helpMenuButton' ) ; await this . find . waitForDeletedByCssSelector ( '.navSearch__panel' ) ; } async searchFor ( term , { clear = true , wait = true } = { } ) { if ( clear ) { await this . clearField ( ) ; } const field = await this . testSubjects . find ( 'nav-search-input' ) ; await field . type ( term ) ; if ( wait ) { await this . waitForResultsLoaded ( ) ; } } async getFieldValue ( ) { const field = await this . testSubjects . find ( 'nav-search-input' ) ; return field . getAttribute ( 'value' ) ; } async clearField ( ) { const field = await this . testSubjects . find ( 'nav-search-input' ) ; await field . clearValueWithKeyboard ( ) ; } async isPopoverDisplayed ( ) { return await this . find . existsByCssSelector ( '.navSearch__panel' ) ; } async clickOnOption ( index ) { const options = await this . testSubjects . findAll ( 'nav-search-option' ) ; await options [ index ] . click ( ) ; } async waitForResultsLoaded ( waitUntil = 3000 ) { await this . testSubjects . exists ( 'nav-search-option' ) ; await delay ( waitUntil ) ; } async getDisplayedResults ( ) { const resultElements = await this . testSubjects . findAll ( 'nav-search-option' ) ; return Promise . all ( resultElements . map ( ( el ) => this . convertResultElement ( el ) ) ) ; } async isNoResultsPlaceholderDisplayed ( checkAfter = 3000 ) { await delay ( checkAfter ) ; return this . testSubjects . exists ( 'nav-search-no-results' ) ; } private async convertResultElement ( resultEl ) { const labelEl = await this . find . allDescendantDisplayedByCssSelector ( '.euiSelectableTemplateSitewide__listItemTitle' , resultEl ) ; const label = await labelEl [ 0 ] . getVisibleText ( ) ; return { label , } ; } }