import expect from '@kbn/expect' ; import { WebElementWrapper } from 'test/functional/services/lib/web_element_wrapper' ; import { FtrProviderContext } from '../../ftr_provider_context' ; import type { CanvasElementColorStats } from '../canvas_element' ; import type { MlCommonUI } from './common_ui' ; export function MachineLearningDataFrameAnalyticsResultsProvider ( { getService } , mlCommonUI ) { const retry = getService ( 'retry' ) ; const testSubjects = getService ( 'testSubjects' ) ; return { async assertRegressionEvaluatePanelElementsExists ( ) { await testSubjects . existOrFail ( 'mlDFExpandableSection-RegressionEvaluation' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsRegressionGenMSEstat' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsRegressionGenRSquaredStat' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsRegressionTrainingMSEstat' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsRegressionTrainingRSquaredStat' ) ; } , async assertRegressionTablePanelExists ( ) { await testSubjects . existOrFail ( 'mlDFAnalyticsExplorationTablePanel' ) ; } , async scrollRocCurveChartIntoView ( ) { await testSubjects . scrollIntoView ( 'mlDFAnalyticsClassificationExplorationRocCurveChart' ) ; } , async assertClassificationEvaluatePanelElementsExists ( ) { await testSubjects . existOrFail ( 'mlDFExpandableSection-ClassificationEvaluation' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsClassificationExplorationConfusionMatrix' ) ; await testSubjects . existOrFail ( 'mlDFAnalyticsClassificationExplorationRocCurveChart' ) ; } , async assertClassificationTablePanelExists ( ) { await testSubjects . existOrFail ( 'mlDFAnalyticsExplorationTablePanel' ) ; } , async assertOutlierTablePanelExists ( ) { await testSubjects . existOrFail ( 'mlDFExpandableSection-results' ) ; } , async assertResultsTableExists ( ) { await testSubjects . existOrFail ( 'mlExplorationDataGrid loaded' , { timeout : 5000 } ) ; } , async assertResultsTableTrainingFiltersExist ( ) { await testSubjects . existOrFail ( 'mlDFAnalyticsExplorationQueryBarFilterButtons' , { timeout : 5000 , } ) ; } , async getResultTableRows ( ) { return ( await testSubjects . find ( 'mlExplorationDataGrid loaded' ) ) . findAllByTestSubject ( 'dataGridRowCell' ) ; } , async assertResultsTableNotEmpty ( ) { const resultTableRows = await this . getResultTableRows ( ) ; expect ( resultTableRows . length ) . to . be . greaterThan ( 0 , `DFA results table should have at least one row (got '${ resultTableRows . length }')` ) ; } , async assertTotalFeatureImportanceEvaluatePanelExists ( ) { await testSubjects . existOrFail ( 'mlDFExpandableSection-FeatureImportanceSummary' ) ; await this . scrollFeatureImportanceIntoView ( ) ; await testSubjects . existOrFail ( 'mlTotalFeatureImportanceChart' , { timeout : 30 * 1000 } ) ; } , async assertFeatureImportanceDecisionPathElementsExists ( ) { await testSubjects . existOrFail ( 'mlDFADecisionPathPopoverTab-decision_path_chart' , { timeout : 5000 , } ) ; await testSubjects . existOrFail ( 'mlDFADecisionPathPopoverTab-decision_path_json' , { timeout : 5000 , } ) ; } , async setScatterplotMatrixSampleSizeSelectValue ( selectValue ) { await testSubjects . selectValue ( 'mlScatterplotMatrixSampleSizeSelect' , selectValue ) ; const actualSelectState = await testSubjects . getAttribute ( 'mlScatterplotMatrixSampleSizeSelect' , 'value' ) ; expect ( actualSelectState ) . to . eql ( selectValue , `Sample size should be '${ selectValue }' (got '${ actualSelectState }')` ) ; } , async getScatterplotMatrixRandomizeQuerySwitchCheckState ( ) { const state = await testSubjects . getAttribute ( 'mlScatterplotMatrixRandomizeQuerySwitch' , 'aria-checked' ) ; return state === 'true' ; } , async assertScatterplotMatrixRandomizeQueryCheckState ( expectedCheckState ) { const actualCheckState = await this . getScatterplotMatrixRandomizeQuerySwitchCheckState ( ) ; expect ( actualCheckState ) . to . eql ( expectedCheckState , `Randomize query check state should be '${ expectedCheckState }' (got '${ actualCheckState }')` ) ; } , async setScatterplotMatrixRandomizeQueryCheckState ( checkState ) { await retry . tryForTime ( 30000 , async ( ) => { if ( ( await this . getScatterplotMatrixRandomizeQuerySwitchCheckState ( ) ) !== checkState ) { await testSubjects . click ( 'mlScatterplotMatrixRandomizeQuerySwitch' ) ; } await this . assertScatterplotMatrixRandomizeQueryCheckState ( checkState ) ; } ) ; } , async assertScatterplotMatrixLoaded ( ) { await testSubjects . existOrFail ( 'mlDFExpandableSection-splom > mlScatterplotMatrix loaded' , { timeout : 5000 , } ) ; } , async assertScatterplotMatrix ( expectedValue ) { await this . assertScatterplotMatrixLoaded ( ) ; await this . scrollScatterplotMatrixIntoView ( ) ; await mlCommonUI . assertColorsInCanvasElement ( 'mlDFExpandableSection-splom' , expectedValue , [ '#000000' ] , undefined , undefined , 20 ) ; } , async assertFeatureImportanceDecisionPathChartElementsExists ( ) { await testSubjects . existOrFail ( 'mlDFADecisionPathChart' , { timeout : 5000 , } ) ; } , async assertFeatureImportancePopoverContent ( ) { if ( await testSubjects . exists ( 'mlDFADecisionPathJSONViewer' , { timeout : 1000 } ) ) { const jsonContent = await testSubjects . getVisibleText ( 'mlDFADecisionPathJSONViewer' ) ; expect ( jsonContent . length ) . greaterThan ( 0 , `Feature importance JSON popover content should not be empty` ) ; } else if ( await testSubjects . exists ( 'mlDFADecisionPathPopover' , { timeout : 1000 } ) ) { await this . assertFeatureImportanceDecisionPathElementsExists ( ) ; await this . assertFeatureImportanceDecisionPathChartElementsExists ( ) ; } else { throw new Error ( 'Expected either decision path popover or JSON viewer to exist.' ) ; } } , async openFeatureImportancePopover ( ) { this . assertResultsTableNotEmpty ( ) ; await retry . tryForTime ( 30 * 1000 , async ( ) => { const featureImportanceCell = await this . getFirstFeatureImportanceCell ( ) ; await featureImportanceCell . focus ( ) ; const interactionButton = await featureImportanceCell . findByTagName ( 'button' ) ; await featureImportanceCell . moveMouseTo ( ) ; await this . waitForInteractionButtonToDisplay ( interactionButton ) ; await interactionButton . click ( ) ; await testSubjects . existOrFail ( 'mlDFAFeatureImportancePopover' , { timeout : 1000 } ) ; } ) ; } , async getFirstFeatureImportanceCell ( ) { const dataGrid = await testSubjects . find ( 'mlExplorationDataGrid loaded' ) ; const featureImportanceCell = await dataGrid . findByCssSelector ( '[data-test-subj="dataGridRowCell"][class*="featureImportance"]' ) ; return featureImportanceCell ; } , async assertFeatureInfluenceCellNotEmpty ( ) { const dataGrid = await testSubjects . find ( 'mlExplorationDataGrid loaded' ) ; const featureInfluenceCell = await dataGrid . findByCssSelector ( '[data-test-subj="dataGridRowCell"][class*="featureInfluence"]' ) ; const contentString = await featureInfluenceCell . getVisibleText ( ) ; expect ( contentString . length ) . to . be . greaterThan ( 0 , 'Expected feature influence cell to have content but it is empty.' ) ; } , async waitForInteractionButtonToDisplay ( interactionButton ) { await retry . tryForTime ( 5000 , async ( ) => { const buttonVisible = await interactionButton . isDisplayed ( ) ; expect ( buttonVisible ) . to . equal ( true , 'Expected data grid cell button to be visible' ) ; } ) ; } , async scrollContentSectionIntoView ( sectionId ) { await testSubjects . scrollIntoView ( `mlDFExpandableSection-${ sectionId }` ) ; } , async scrollAnalysisIntoView ( ) { await this . scrollContentSectionIntoView ( 'analysis' ) ; } , async scrollRegressionEvaluationIntoView ( ) { await this . scrollContentSectionIntoView ( 'RegressionEvaluation' ) ; } , async scrollClassificationEvaluationIntoView ( ) { await this . scrollContentSectionIntoView ( 'ClassificationEvaluation' ) ; } , async scrollFeatureImportanceIntoView ( ) { await this . scrollContentSectionIntoView ( 'FeatureImportanceSummary' ) ; } , async scrollScatterplotMatrixIntoView ( ) { await this . scrollContentSectionIntoView ( 'splom' ) ; } , async scrollResultsIntoView ( ) { await this . scrollContentSectionIntoView ( 'results' ) ; } , async expandContentSection ( sectionId , shouldExpand ) { const contentSubj = `mlDFExpandableSection-${ sectionId }-content` ; const expandableContentExists = await testSubjects . exists ( contentSubj , { timeout : 1000 } ) ; if ( expandableContentExists !== shouldExpand ) { await retry . tryForTime ( 5 * 1000 , async ( ) => { await testSubjects . clickWhenNotDisabled ( `mlDFExpandableSection-${ sectionId }-toggle-button` ) ; if ( shouldExpand ) { await testSubjects . existOrFail ( contentSubj , { timeout : 1000 } ) ; } else { await testSubjects . missingOrFail ( contentSubj , { timeout : 1000 } ) ; } } ) ; } } , async expandAnalysisSection ( shouldExpand ) { await this . expandContentSection ( 'analysis' , shouldExpand ) ; } , async expandRegressionEvaluationSection ( shouldExpand ) { await this . expandContentSection ( 'RegressionEvaluation' , shouldExpand ) ; } , async expandClassificationEvaluationSection ( shouldExpand ) { await this . expandContentSection ( 'ClassificationEvaluation' , shouldExpand ) ; } , async expandFeatureImportanceSection ( shouldExpand ) { await this . expandContentSection ( 'FeatureImportanceSummary' , shouldExpand ) ; } , async expandScatterplotMatrixSection ( shouldExpand ) { await this . expandContentSection ( 'splom' , shouldExpand ) ; } , async expandResultsSection ( shouldExpand ) { await this . expandContentSection ( 'results' , shouldExpand ) ; } , } ; }