Skip to content

Commit

Permalink
[BUG][Fuctional Test] Make setDefaultAbsoluteRange more robust and up…
Browse files Browse the repository at this point in the history
…date doc views tests

* add a helper function setInputValueWithRetry for setAbsoluteRange to retry
time range setup
* update doc_views test to click docTableExpandToggleColumn-0
* update doc_views_link to check new tab

Issue Resolve
opensearch-project#5241

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Oct 9, 2023
1 parent 1e980fa commit 444508a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Data Explorer][Discover] Allow data grid to auto adjust size based on fetched data count ([#5191](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5191))
- [BUG] Fix wrong test due to time conversion ([#5174](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5174))
- [BUG][Data Explorer][Discover] Allow filter and query persist when refresh page or paste url to a new tab ([#5206](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5206))
- [BUG][Fuctional Test] Make setDefaultAbsoluteRange more robust and update doc views tests ([#5242](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5242))

### 🚞 Infrastructure

Expand Down
35 changes: 33 additions & 2 deletions test/functional/page_objects/time_picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function TimePickerProvider({ getService, getPageObjects }: FtrProviderCo
const { header } = getPageObjects(['header']);
const opensearchDashboardsServer = getService('opensearchDashboardsServer');
const MenuToggle = getService('MenuToggle');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const quickSelectTimeMenuToggle = new MenuToggle({
name: 'QuickSelectTime Menu',
Expand Down Expand Up @@ -130,20 +131,49 @@ 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.inputValue('superDatePickerAbsoluteDateInput', toTime);
await this.setInputValueWithRetry('superDatePickerAbsoluteDateInput', toTime);
await browser.pressKeys(browser.keys.ESCAPE); // close popover because sometimes browser can't find start input

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

const superDatePickerApplyButtonExists = await testSubjects.exists(
'superDatePickerApplyTimeButton'
Expand Down
2 changes: 1 addition & 1 deletion test/plugin_functional/test_suites/doc_views/doc_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
});

it('should show custom doc views', async () => {
await testSubjects.click('docTableExpandToggleColumn');
await testSubjects.click('docTableExpandToggleColumn-0');
const reactTab = await find.byButtonText('React doc view');
expect(await reactTab.isDisplayed()).to.be(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,81 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide
const find = getService('find');
const browser = getService('browser');
const PageObjects = getPageObjects(['common', 'discover', 'timePicker']);
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const waitFor = async (conditionFunction, timeoutMs = 10000, intervalMs = 100) => {
const start = Date.now();

let lastError;
while (Date.now() - start < timeoutMs) {
try {
if (await conditionFunction()) {
return;
}
} catch (error) {
lastError = error;
}

await sleep(intervalMs);
}

throw new Error(
`waitFor condition did not become true within ${timeoutMs}ms. Last error: ${
lastError && lastError.message
}`
);
};

describe('custom doc views links', function () {
beforeEach(async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
await testSubjects.click('docTableExpandToggleColumn');
await testSubjects.click('docTableExpandToggleColumn-0');
});

it('should show href and generateCb doc views link', async () => {
it('should show href and generateCb doc views link and not show generateCbHidden doc views link', async () => {
const hrefLink = await find.byLinkText('href doc view link');
const generateCbLink = await find.byLinkText('generateCb doc view link');

expect(await hrefLink.isDisplayed()).to.be(true);
expect(await generateCbLink.isDisplayed()).to.be(true);
});

it('should not render generateCbHidden doc views link', async () => {
expect(await find.existsByLinkText('generateCbHidden doc view link')).to.eql(false);
});

it('should render href doc view link', async () => {
const hrefLink = await find.byLinkText('href doc view link');
const originalTabCount = (await browser.getAllWindowHandles()).length;
await hrefLink.click();
expect(await browser.getCurrentUrl()).to.eql('http://some-url/');

// wait until a new tab is opened
await waitFor(async () => (await browser.getAllWindowHandles()).length > originalTabCount);

// switch to the originalTabCount in case previous tab is not closed in time
await browser.switchTab(originalTabCount);

const currentUrl = await browser.getCurrentUrl();
expect(currentUrl).to.eql('http://some-url/');

// close new tab and switch back to original tab
await browser.closeCurrentWindow();
await browser.switchTab(0);
});

it('should render generateCb doc view link', async () => {
const generateCbLink = await find.byLinkText('generateCb doc view link');
const originalTabCount = (await browser.getAllWindowHandles()).length;
await generateCbLink.click();
expect(await browser.getCurrentUrl()).to.eql('http://some-url/');

// wait until a new tab is opened
await waitFor(async () => (await browser.getAllWindowHandles()).length > originalTabCount);

// switch to the originalTabCount in case previous tab is not closed in time
await browser.switchTab(originalTabCount);

const currentUrl = await browser.getCurrentUrl();
expect(currentUrl).to.eql('http://some-url/');

// close new tab and switch back to original tab
await browser.closeCurrentWindow();
await browser.switchTab(0);
});
});
}

0 comments on commit 444508a

Please sign in to comment.