import expect from '@kbn/expect' ; import { FtrProviderContext } from '../../ftr_provider_context' ; export function MachineLearningAnomaliesTableProvider ( { getService } ) { const retry = getService ( 'retry' ) ; const testSubjects = getService ( 'testSubjects' ) ; return { async assertTableExists ( ) { await testSubjects . existOrFail ( 'mlAnomaliesTable' ) ; } , async getTableRows ( ) { return await testSubjects . findAll ( 'mlAnomaliesTable > ~mlAnomaliesListRow' ) ; } , param expectedCount async assertTableRowsCount ( expectedCount ) { const actualCount = ( await this . getTableRows ( ) ) . length ; expect ( actualCount ) . to . eql ( expectedCount , `Expect anomaly table rows count to be ${ expectedCount }, got ${ actualCount }` ) ; } , async getRowSubjByRowIndex ( rowIndex ) { const tableRows = await this . getTableRows ( ) ; expect ( tableRows . length ) . to . be . greaterThan ( rowIndex , `Expected anomalies table to have at least ${ rowIndex + 1 } rows (got ${ tableRows . length } rows)` ) ; const row = tableRows [ rowIndex ] ; const rowSubj = await row . getAttribute ( 'data-test-subj' ) ; return rowSubj ; } , async assertTableNotEmpty ( ) { const tableRows = await this . getTableRows ( ) ; expect ( tableRows . length ) . to . be . greaterThan ( 0 , `Anomalies table should have at least one row (got '${ tableRows . length }')` ) ; } , async assertAnomalyActionsMenuButtonExists ( rowIndex ) { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; await testSubjects . existOrFail ( `${ rowSubj } > mlAnomaliesListRowActionsButton` ) ; } , async assertAnomalyActionsMenuButtonNotExists ( rowIndex ) { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; await testSubjects . missingOrFail ( `${ rowSubj } > mlAnomaliesListRowActionsButton` ) ; } , async assertAnomalyActionsMenuButtonEnabled ( rowIndex , expectedValue ) { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; const isEnabled = await testSubjects . isEnabled ( `${ rowSubj } > mlAnomaliesListRowActionsButton` ) ; expect ( isEnabled ) . to . eql ( expectedValue , `Expected actions menu button for anomalies list entry  #${ rowIndex } to be '${ expectedValue ? 'enabled' : 'disabled' }' (got '${ isEnabled ? 'enabled' : 'disabled' }')` ) ; } , async ensureAnomalyActionsMenuOpen ( rowIndex ) { await retry . tryForTime ( 30 * 1000 , async ( ) => { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; if ( ! ( await testSubjects . exists ( 'mlAnomaliesListRowActionsMenu' ) ) ) { await testSubjects . click ( `${ rowSubj } > mlAnomaliesListRowActionsButton` ) ; await testSubjects . existOrFail ( 'mlAnomaliesListRowActionsMenu' , { timeout : 5000 } ) ; } } ) ; } , async assertAnomalyActionConfigureRulesButtonExists ( rowIndex ) { await this . ensureAnomalyActionsMenuOpen ( rowIndex ) ; await testSubjects . existOrFail ( 'mlAnomaliesListRowActionConfigureRulesButton' ) ; } , async assertAnomalyActionConfigureRulesButtonNotExists ( rowIndex ) { await this . ensureAnomalyActionsMenuOpen ( rowIndex ) ; await testSubjects . missingOrFail ( 'mlAnomaliesListRowActionConfigureRulesButton' ) ; } , async assertAnomalyActionConfigureRulesButtonEnabled ( rowIndex , expectedValue ) { await this . ensureAnomalyActionsMenuOpen ( rowIndex ) ; const isEnabled = await testSubjects . isEnabled ( 'mlAnomaliesListRowActionConfigureRulesButton' ) ; expect ( isEnabled ) . to . eql ( expectedValue , `Expected "configure rules" action button for anomalies list entry  #${ rowIndex } to be '${ expectedValue ? 'enabled' : 'disabled' }' (got '${ isEnabled ? 'enabled' : 'disabled' }')` ) ; } , async assertAnomalyActionViewSeriesButtonEnabled ( rowIndex , expectedValue ) { await this . ensureAnomalyActionsMenuOpen ( rowIndex ) ; const isEnabled = await testSubjects . isEnabled ( 'mlAnomaliesListRowActionViewSeriesButton' ) ; expect ( isEnabled ) . to . eql ( expectedValue , `Expected "view series" action button for anomalies list entry  #${ rowIndex } to be '${ expectedValue ? 'enabled' : 'disabled' }' (got '${ isEnabled ? 'enabled' : 'disabled' }')` ) ; } , param rowsNumber async assertRowsNumberPerPage ( rowsNumber ) { const textContent = await testSubjects . getVisibleText ( 'mlAnomaliesTable > tablePaginationPopoverButton' ) ; expect ( textContent ) . to . be ( `Rows per page: ${ rowsNumber }` ) ; } , async ensurePagePopupOpen ( ) { await retry . tryForTime ( 5000 , async ( ) => { const isOpen = await testSubjects . exists ( 'tablePagination-10-rows' ) ; if ( ! isOpen ) { await testSubjects . click ( 'mlAnomaliesTable > tablePaginationPopoverButton' ) ; await testSubjects . existOrFail ( 'tablePagination-10-rows' ) ; } } ) ; } , async setRowsNumberPerPage ( rowsNumber ) { await this . ensurePagePopupOpen ( ) ; await testSubjects . click ( `tablePagination-${ rowsNumber }-rows` ) ; await this . assertRowsNumberPerPage ( rowsNumber ) ; } , async ensureDetailsOpen ( rowIndex ) { await retry . tryForTime ( 10 * 1000 , async ( ) => { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; if ( ! ( await testSubjects . exists ( 'mlAnomaliesListRowDetails' ) ) ) { await testSubjects . click ( `${ rowSubj } > mlAnomaliesListRowDetailsToggle` ) ; await testSubjects . existOrFail ( 'mlAnomaliesListRowDetails' , { timeout : 1000 } ) ; } } ) ; } , async ensureDetailsClosed ( rowIndex ) { await retry . tryForTime ( 10 * 1000 , async ( ) => { const rowSubj = await this . getRowSubjByRowIndex ( rowIndex ) ; if ( await testSubjects . exists ( 'mlAnomaliesListRowDetails' ) ) { await testSubjects . click ( `${ rowSubj } > mlAnomaliesListRowDetailsToggle` ) ; await testSubjects . missingOrFail ( 'mlAnomaliesListRowDetails' , { timeout : 1000 } ) ; } } ) ; } , async scrollTableIntoView ( ) { await testSubjects . scrollIntoView ( 'mlAnomaliesTable' ) ; } , } ; }