Skip to content

Commit

Permalink
fix PR comment by making a special time picker method for discover
Browse files Browse the repository at this point in the history
Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Oct 9, 2023
1 parent 444508a commit 8e7834c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
111 changes: 79 additions & 32 deletions test/functional/page_objects/time_picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,49 +131,20 @@ export function TimePickerProvider({ getService, getPageObjects }: FtrProviderCo
await testSubjects.exists('superDatePickerstartDatePopoverButton');
}

// Helper function to set input value and verify
private async setInputValueWithRetry(testSubjectId: string, value: string) {
const MAX_ATTEMPTS = 3;

for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
// try to set the value
await this.inputValue(testSubjectId, value);
await sleep(500);

// verify if the value was correctly set
const actualValue = await (await testSubjects.find(testSubjectId)).getAttribute('value');
if (actualValue === value) {
return;
}

// if it's the last attempt and value wasn't set correctly, throw an error
if (attempt === MAX_ATTEMPTS - 1) {
throw new Error(
`Failed to set ${testSubjectId} to ${value} after ${MAX_ATTEMPTS} attempts.`
);
}

await sleep(500); // wait before retrying
}
}

/**
* @param {String} fromTime MMM D, YYYY @ HH:mm:ss.SSS
* @param {String} toTime MMM D, YYYY @ HH:mm:ss.SSS
*/
public async setAbsoluteRange(fromTime: string, toTime: string) {
log.debug(`Setting absolute range to ${fromTime} to ${toTime}`);
await this.showStartEndTimes();
// make sure to close this verify panel
await browser.pressKeys(browser.keys.ESCAPE);
await sleep(500);

// set to time
await testSubjects.click('superDatePickerendDatePopoverButton');
let panel = await this.getTimePickerPanel();
await testSubjects.click('superDatePickerAbsoluteTab');
await testSubjects.click('superDatePickerAbsoluteDateInput');
await this.setInputValueWithRetry('superDatePickerAbsoluteDateInput', toTime);
await this.inputValue('superDatePickerAbsoluteDateInput', toTime);
await browser.pressKeys(browser.keys.ESCAPE); // close popover because sometimes browser can't find start input

// set from time
Expand All @@ -182,8 +153,7 @@ export function TimePickerProvider({ getService, getPageObjects }: FtrProviderCo
panel = await this.getTimePickerPanel();
await testSubjects.click('superDatePickerAbsoluteTab');
await testSubjects.click('superDatePickerAbsoluteDateInput');
await this.setInputValueWithRetry('superDatePickerAbsoluteDateInput', fromTime);
await browser.pressKeys(browser.keys.ESCAPE);
await this.inputValue('superDatePickerAbsoluteDateInput', fromTime);

const superDatePickerApplyButtonExists = await testSubjects.exists(
'superDatePickerApplyTimeButton'
Expand Down Expand Up @@ -340,6 +310,83 @@ export function TimePickerProvider({ getService, getPageObjects }: FtrProviderCo
const toTime = 'Apr 13, 2018 @ 00:00:00.000';
await this.setAbsoluteRange(fromTime, toTime);
}

// Helper function to set input value and verify
private async setInputValueWithRetry(testSubjectId: string, value: string) {
const MAX_ATTEMPTS = 3;

for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
// try to set the value
await this.inputValue(testSubjectId, value);
await sleep(500);

// verify if the value was correctly set
const actualValue = await (await testSubjects.find(testSubjectId)).getAttribute('value');
if (actualValue === value) {
return;
}

// if it's the last attempt and value wasn't set correctly, throw an error
if (attempt === MAX_ATTEMPTS - 1) {
throw new Error(
`Failed to set ${testSubjectId} to ${value} after ${MAX_ATTEMPTS} attempts.`
);
}

await sleep(500); // wait before retrying
}
}

// TODO: This is a temporary method added due to observed issues with panels
// not closing in time and incorrect time settings on Discover page. Once these bugs are resolved
// and the interactions become more reliable, we should consider removing this method and related helper functions.
// Tracking issue: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5241
/**
* @param {String} fromTime MMM D, YYYY @ HH:mm:ss.SSS
* @param {String} toTime MMM D, YYYY @ HH:mm:ss.SSS
*/
public async setDefaultRangeForDiscover() {
const fromTime = this.defaultStartTime;
const toTime = this.defaultEndTime;
log.debug(`Setting absolute range to ${fromTime} to ${toTime}`);

await this.showStartEndTimes();
// make sure to close this verify panel
await browser.pressKeys(browser.keys.ESCAPE);
await sleep(500);

// set to time
await testSubjects.click('superDatePickerendDatePopoverButton');
let panel = await this.getTimePickerPanel();
await testSubjects.click('superDatePickerAbsoluteTab');
await testSubjects.click('superDatePickerAbsoluteDateInput');
await this.setInputValueWithRetry('superDatePickerAbsoluteDateInput', toTime);
await browser.pressKeys(browser.keys.ESCAPE); // close popover because sometimes browser can't find start input

// set from time
await testSubjects.click('superDatePickerstartDatePopoverButton');
await this.waitPanelIsGone(panel);
panel = await this.getTimePickerPanel();
await testSubjects.click('superDatePickerAbsoluteTab');
await testSubjects.click('superDatePickerAbsoluteDateInput');
await this.setInputValueWithRetry('superDatePickerAbsoluteDateInput', fromTime);
await browser.pressKeys(browser.keys.ESCAPE);

const superDatePickerApplyButtonExists = await testSubjects.exists(
'superDatePickerApplyTimeButton'
);
if (superDatePickerApplyButtonExists) {
// Timepicker is in top nav
// Click super date picker apply button to apply time range
await testSubjects.click('superDatePickerApplyTimeButton');
} else {
// Timepicker is embedded in query bar
// click query bar submit button to apply time range
await testSubjects.click('querySubmitButton');
}
await this.waitPanelIsGone(panel);
await header.awaitGlobalLoadingIndicatorHidden();
}
}

return new TimePicker();
Expand Down
4 changes: 3 additions & 1 deletion test/plugin_functional/test_suites/doc_views/doc_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
describe('custom doc views', function () {
before(async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
// TODO: change back to setDefaultRange() once we resolve
// https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5241
await PageObjects.timePicker.setDefaultRangeForDiscover();
});

it('should show custom doc views', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
describe('custom doc views links', function () {
beforeEach(async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
// TODO: change back to setDefaultRange() once we resolve
// https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5241
await PageObjects.timePicker.setDefaultRangeForDiscover();
await testSubjects.click('docTableExpandToggleColumn-0');
});

Expand Down

0 comments on commit 8e7834c

Please sign in to comment.