Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[pageObjects/dashboard] check that save is complete before resolving (e…
Browse files Browse the repository at this point in the history
…lastic#21892)

While debugging failures I saw in elastic#21772 I found myself encountering failure messages like `Error: expected undefined to sort of equal true`, and other more cryptic errors caused by methods like `PageObjects.dashboard.saveDashboard()` not ensuring that the dashboard was actually saved before resolving. As part of the debugging effort I noticed that the `saveDashboard()` method does have some awareness of the success condition, but rather than asserting success within the method it returns a success boolean for the caller to check, which was only being done in a handful of tests in `test/functional/apps/dashboard/_dashboard_time.js` but was ignored the vast majority of the time.

I think that most of the time we are calling `PageObjects.dashboard.saveDashboard()` we correctly assume that if the dashboard couldn't be saved for some reason the promise will be rejected and the test would fail. If the method was called `maybeSaveDashboard()` or `tryToSaveDashboard()` there might be a signal to consumers that they should check for success conditions, but that would also lead to the same checks all over the place. Instead, this PR reverses the responsibility of checking for success so that code calling `PageObjects.dashboard.saveDashboard()` can continue to assume that if something went wrong their test will fail. It also improves the error message by not using `expect(boolean).to.equal(boolean)`, instead implementing a basic `if()` statement and throwing an error with a meaningful message when something goes wrong.

```js
const isDashboardSaved = await testSubjects.exists('saveDashboardSuccess');
expect(isDashboardSaved).to.eql(true);
```

is now

```js
if (!await testSubjects.exists('saveDashboardSuccess')) {
  throw new Error('Expected to find "saveDashboardSuccess" toast after saving dashboard');
}
```

---

I think this type of change could be made to a lot of methods, and would make failures a lot easier to debug and possibly a lot less flaky if we were checking for success conditions in nearly every method we put in our PageObjects. I think it's safe to say that most of the methods we have in PageObjects do not check for actual success criteria, and sometimes that's okay: a method called `clickButton()` can safely resolve once the click method has been called, but a method like `addSampleDataSet()` should be verifying that the sample data set it set out to add was actually added.
  • Loading branch information
Spencer committed Aug 13, 2018
1 parent e5d110c commit ed16f9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 3 additions & 6 deletions test/functional/apps/dashboard/_dashboard_time.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export default function ({ getPageObjects, getService }) {
it('is saved', async () => {
await PageObjects.dashboard.clickNewDashboard();
await PageObjects.dashboard.addVisualizations([PageObjects.dashboard.getTestVisualizationNames()[0]]);
const isDashboardSaved = await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: false });
expect(isDashboardSaved).to.eql(true);
await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: false });
});

it('Does not set the time picker on open', async () => {
Expand All @@ -62,8 +61,7 @@ export default function ({ getPageObjects, getService }) {
it('is saved with quick time', async function () {
await PageObjects.dashboard.clickEdit();
await PageObjects.header.setQuickTime('Today');
const isDashboardSaved = await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: true });
expect(isDashboardSaved).to.eql(true);
await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: true });
});

it('sets quick time on open', async function () {
Expand All @@ -78,8 +76,7 @@ export default function ({ getPageObjects, getService }) {
it('is saved with absolute time', async function () {
await PageObjects.dashboard.clickEdit();
await PageObjects.header.setAbsoluteRange(fromTime, toTime);
const isDashboardSaved = await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: true });
expect(isDashboardSaved).to.eql(true);
await PageObjects.dashboard.saveDashboard(dashboardName, { storeTimeWithDashboard: true });
});

it('sets absolute time on open', async function () {
Expand Down
19 changes: 17 additions & 2 deletions test/functional/page_objects/dashboard_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ export function DashboardPageProvider({ getService, getPageObjects }) {
}

/**
* Save the current dashboard with the specified name and options and
* verify that the save was successful
*
* @param dashName {String}
* @param saveOptions {{storeTimeWithDashboard: boolean, saveAsNew: boolean, needsConfirm: false}}
Expand All @@ -301,8 +303,21 @@ export function DashboardPageProvider({ getService, getPageObjects }) {

await PageObjects.header.waitUntilLoadingHasFinished();

// Confirm that the Dashboard has been saved.
return await testSubjects.exists('saveDashboardSuccess');
// Confirm that the Dashboard has actually been saved
if (!await testSubjects.exists('saveDashboardSuccess')) {
throw new Error('Expected to find "saveDashboardSuccess" toast after saving dashboard');
}

await this.waitForSaveModalToClose();
}

async waitForSaveModalToClose() {
log.debug('Waiting for dashboard save modal to close');
await retry.try(async () => {
if (await testSubjects.exists('dashboardSaveModal')) {
throw new Error('dashboard save still open');
}
});
}

async deleteDashboard(dashboardName, dashboardId) {
Expand Down

0 comments on commit ed16f9b

Please sign in to comment.