Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Functional tests - fix typing issue #52167

Merged
merged 14 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions x-pack/test/functional/services/machine_learning/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FtrProviderContext } from '../../ftr_provider_context';

interface SetValueOptions {
clearWithKeyboard?: boolean;
typeCharByChar?: boolean;
}

export function MachineLearningCommonProvider({ getService }: FtrProviderContext) {
const log = getService('log');
const retry = getService('retry');
const testSubjects = getService('testSubjects');
const find = getService('find');

return {
async setValueWithChecks(
selector: string,
text: string,
options: SetValueOptions = {}
): Promise<void> {
return await retry.try(async () => {
const { clearWithKeyboard = false, typeCharByChar = false } = options;
log.debug(`TestSubjects.setValueWithChecks(${selector}, ${text})`);
await testSubjects.click(selector);
// in case the input element is actually a child of the testSubject, we
// call clearValue() and type() on the element that is focused after
// clicking on the testSubject
const input = await find.activeElement();

await retry.tryForTime(5000, async () => {
let currentValue = await input.getAttribute('value');
if (currentValue !== '') {
if (clearWithKeyboard === true) {
await input.clearValueWithKeyboard();
} else {
await input.clearValue();
}
currentValue = await input.getAttribute('value');
}

if (currentValue === '') {
return true;
} else {
throw new Error(`Expected input to be empty, but got value '${currentValue}'`);
}
});

for (const chr of text) {
await retry.tryForTime(5000, async () => {
const oldValue = await input.getAttribute('value');
await input.type(chr, { charByChar: typeCharByChar });

await retry.tryForTime(1000, async () => {
const newValue = await input.getAttribute('value');
if (newValue === `${oldValue}${chr}`) {
return true;
} else {
throw new Error(
`After typing character '${chr}', the new value in the input should be '${oldValue}${chr}' (got ${newValue})`
);
}
});
});
}
});
},
};
}
1 change: 1 addition & 0 deletions x-pack/test/functional/services/machine_learning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

export { MachineLearningAnomalyExplorerProvider } from './anomaly_explorer';
export { MachineLearningAPIProvider } from './api';
export { MachineLearningCommonProvider } from './common';
export { MachineLearningCustomUrlsProvider } from './custom_urls';
export { MachineLearningDataFrameAnalyticsProvider } from './data_frame_analytics';
export { MachineLearningDataFrameAnalyticsCreationProvider } from './data_frame_analytics_creation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/
import expect from '@kbn/expect';
import { ProvidedType } from '@kbn/test/types/ftr';

import { FtrProviderContext } from '../../ftr_provider_context';
import { MachineLearningCommonProvider } from './common';

export function MachineLearningJobWizardAdvancedProvider({ getService }: FtrProviderContext) {
export function MachineLearningJobWizardAdvancedProvider(
{ getService }: FtrProviderContext,
mlCommon: ProvidedType<typeof MachineLearningCommonProvider>
) {
const comboBox = getService('comboBox');
const testSubjects = getService('testSubjects');
const retry = getService('retry');
Expand Down Expand Up @@ -44,7 +49,7 @@ export function MachineLearningJobWizardAdvancedProvider({ getService }: FtrProv
},

async setQueryDelay(queryDelay: string) {
await testSubjects.setValue('mlJobWizardInputQueryDelay', queryDelay, {
await mlCommon.setValueWithChecks('mlJobWizardInputQueryDelay', queryDelay, {
clearWithKeyboard: true,
typeCharByChar: true,
});
Expand All @@ -61,7 +66,7 @@ export function MachineLearningJobWizardAdvancedProvider({ getService }: FtrProv
},

async setFrequency(frequency: string) {
await testSubjects.setValue('mlJobWizardInputFrequency', frequency, {
await mlCommon.setValueWithChecks('mlJobWizardInputFrequency', frequency, {
clearWithKeyboard: true,
typeCharByChar: true,
});
Expand All @@ -78,7 +83,7 @@ export function MachineLearningJobWizardAdvancedProvider({ getService }: FtrProv
},

async setScrollSize(scrollSize: string) {
await testSubjects.setValue('mlJobWizardInputScrollSize', scrollSize, {
await mlCommon.setValueWithChecks('mlJobWizardInputScrollSize', scrollSize, {
clearWithKeyboard: true,
typeCharByChar: true,
});
Expand Down Expand Up @@ -257,7 +262,7 @@ export function MachineLearningJobWizardAdvancedProvider({ getService }: FtrProv
},

async setDetectorDescription(description: string) {
await testSubjects.setValue('mlAdvancedDetectorDescriptionInput', description, {
await mlCommon.setValueWithChecks('mlAdvancedDetectorDescriptionInput', description, {
clearWithKeyboard: true,
});
await this.assertDetectorDescriptionValue(description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import expect from '@kbn/expect';
import { ProvidedType } from '@kbn/test/types/ftr';

import { FtrProviderContext } from '../../ftr_provider_context';
import { MachineLearningCommonProvider } from './common';
import { MachineLearningCustomUrlsProvider } from './custom_urls';

export function MachineLearningJobWizardCommonProvider(
{ getService }: FtrProviderContext,
mlCommon: ProvidedType<typeof MachineLearningCommonProvider>,
Copy link
Contributor

@spalger spalger Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, I usually like to export this type from the module containing the provider so I can just import the MlCommon type without having to use these long types everywhere.

in common.ts

export type MlCommon = ProvidedType<typeof MachineLearningCommonProvider>

in here

import { MlCommon } from './common'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I've created a PR for this: #52612

customUrls: ProvidedType<typeof MachineLearningCustomUrlsProvider>
) {
const comboBox = getService('comboBox');
Expand Down Expand Up @@ -113,7 +115,7 @@ export function MachineLearningJobWizardCommonProvider(
},

async setBucketSpan(bucketSpan: string) {
await testSubjects.setValue('mlJobWizardInputBucketSpan', bucketSpan, {
await mlCommon.setValueWithChecks('mlJobWizardInputBucketSpan', bucketSpan, {
clearWithKeyboard: true,
typeCharByChar: true,
});
Expand All @@ -130,7 +132,9 @@ export function MachineLearningJobWizardCommonProvider(
},

async setJobId(jobId: string) {
await testSubjects.setValue('mlJobWizardInputJobId', jobId, { clearWithKeyboard: true });
await mlCommon.setValueWithChecks('mlJobWizardInputJobId', jobId, {
clearWithKeyboard: true,
});
await this.assertJobIdValue(jobId);
},

Expand All @@ -146,7 +150,7 @@ export function MachineLearningJobWizardCommonProvider(
},

async setJobDescription(jobDescription: string) {
await testSubjects.setValue('mlJobWizardInputJobDescription', jobDescription, {
await mlCommon.setValueWithChecks('mlJobWizardInputJobDescription', jobDescription, {
clearWithKeyboard: true,
});
await this.assertJobDescriptionValue(jobDescription);
Expand Down Expand Up @@ -307,7 +311,7 @@ export function MachineLearningJobWizardCommonProvider(
await this.ensureAdvancedSectionOpen();
subj = advancedSectionSelector(subj);
}
await testSubjects.setValue(subj, modelMemoryLimit, { clearWithKeyboard: true });
await mlCommon.setValueWithChecks(subj, modelMemoryLimit, { clearWithKeyboard: true });
await this.assertModelMemoryLimitValue(modelMemoryLimit, {
withAdvancedSection: sectionOptions.withAdvancedSection,
});
Expand Down
7 changes: 5 additions & 2 deletions x-pack/test/functional/services/ml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FtrProviderContext } from '../ftr_provider_context';
import {
MachineLearningAnomalyExplorerProvider,
MachineLearningAPIProvider,
MachineLearningCommonProvider,
MachineLearningCustomUrlsProvider,
MachineLearningDataFrameAnalyticsProvider,
MachineLearningDataFrameAnalyticsCreationProvider,
Expand All @@ -29,6 +30,8 @@ import {
} from './machine_learning';

export function MachineLearningProvider(context: FtrProviderContext) {
const common = MachineLearningCommonProvider(context);

const anomalyExplorer = MachineLearningAnomalyExplorerProvider(context);
const api = MachineLearningAPIProvider(context);
const customUrls = MachineLearningCustomUrlsProvider(context);
Expand All @@ -41,8 +44,8 @@ export function MachineLearningProvider(context: FtrProviderContext) {
const jobSourceSelection = MachineLearningJobSourceSelectionProvider(context);
const jobTable = MachineLearningJobTableProvider(context);
const jobTypeSelection = MachineLearningJobTypeSelectionProvider(context);
const jobWizardAdvanced = MachineLearningJobWizardAdvancedProvider(context);
const jobWizardCommon = MachineLearningJobWizardCommonProvider(context, customUrls);
const jobWizardAdvanced = MachineLearningJobWizardAdvancedProvider(context, common);
const jobWizardCommon = MachineLearningJobWizardCommonProvider(context, common, customUrls);
const jobWizardMultiMetric = MachineLearningJobWizardMultiMetricProvider(context);
const jobWizardPopulation = MachineLearningJobWizardPopulationProvider(context);
const navigation = MachineLearningNavigationProvider(context);
Expand Down