Skip to content

Commit

Permalink
removed code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed Jan 15, 2021
1 parent b33de6e commit 068aab1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 62 deletions.
46 changes: 28 additions & 18 deletions test/functional/page_objects/visualize_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ interface VisualizeSaveModalArgs {
dashboardId?: string;
}

type DashboardPickerOption =
| 'add-to-library-option'
| 'existing-dashboard-option'
| 'new-dashboard-option';

export function VisualizePageProvider({ getService, getPageObjects }: FtrProviderContext) {
const testSubjects = getService('testSubjects');
const retry = getService('retry');
Expand Down Expand Up @@ -353,11 +358,27 @@ export function VisualizePageProvider({ getService, getPageObjects }: FtrProvide
}
}

public async saveVisualization(
public async saveVisualization(vizName: string, saveModalArgs: VisualizeSaveModalArgs = {}) {
await this.ensureSavePanelOpen();

await this.setSaveModalValues(vizName, saveModalArgs);
log.debug('Click Save Visualization button');

await testSubjects.click('confirmSaveSavedObjectButton');

// Confirm that the Visualization has actually been saved
await testSubjects.existOrFail('saveVisualizationSuccess');
const message = await common.closeToast();
await header.waitUntilLoadingHasFinished();
await common.waitForSaveModalToClose();

return message;
}

public async setSaveModalValues(
vizName: string,
{ saveAsNew, redirectToOrigin, addToDashboard, dashboardId }: VisualizeSaveModalArgs = {}
) {
await this.ensureSavePanelOpen();
await testSubjects.setValue('savedObjectTitle', vizName);

const saveAsNewCheckboxExists = await testSubjects.exists('saveAsNewCheckbox');
Expand All @@ -376,30 +397,19 @@ export function VisualizePageProvider({ getService, getPageObjects }: FtrProvide

const dashboardSelectorExists = await testSubjects.exists('add-to-dashboard-options');
if (dashboardSelectorExists) {
const dashboardSelector = await testSubjects.find('add-to-dashboard-options');
let optionSelector = 'add-to-library-option';
let option: DashboardPickerOption = 'add-to-library-option';
if (addToDashboard) {
optionSelector = dashboardId ? 'existing-dashboard-option' : 'new-dashboard-option';
option = dashboardId ? 'existing-dashboard-option' : 'new-dashboard-option';
}
log.debug('dashboard selector exists, choosing option:', optionSelector);
const label = await dashboardSelector.findByCssSelector(`label[for="${optionSelector}"]`);
log.debug('save modal dashboard selector, choosing option:', option);
const dashboardSelector = await testSubjects.find('add-to-dashboard-options');
const label = await dashboardSelector.findByCssSelector(`label[for="${option}"]`);
await label.click();

if (dashboardId) {
// TODO - selecting an existing dashboard
}
}
log.debug('Click Save Visualization button');

await testSubjects.click('confirmSaveSavedObjectButton');

// Confirm that the Visualization has actually been saved
await testSubjects.existOrFail('saveVisualizationSuccess');
const message = await common.closeToast();
await header.waitUntilLoadingHasFinished();
await common.waitForSaveModalToClose();

return message;
}

public async saveVisualizationExpectSuccess(
Expand Down
38 changes: 8 additions & 30 deletions x-pack/test/functional/page_objects/lens_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function LensPageProvider({ getService, getPageObjects }: FtrProviderCont
const find = getService('find');
const comboBox = getService('comboBox');
const browser = getService('browser');
const PageObjects = getPageObjects(['header', 'timePicker', 'common']);
const PageObjects = getPageObjects(['header', 'timePicker', 'common', 'visualize']);

return logWrapper('lensPage', log, {
/**
Expand Down Expand Up @@ -275,39 +275,17 @@ export function LensPageProvider({ getService, getPageObjects }: FtrProviderCont
saveAsNew?: boolean,
redirectToOrigin?: boolean,
addToDashboard?: boolean,
dashboardId?: boolean
dashboardId?: string
) {
await PageObjects.header.waitUntilLoadingHasFinished();
await testSubjects.click('lnsApp_saveButton');
await testSubjects.setValue('savedObjectTitle', title);

const saveAsNewCheckboxExists = await testSubjects.exists('saveAsNewCheckbox');
if (saveAsNewCheckboxExists) {
const state = saveAsNew ? 'check' : 'uncheck';
await testSubjects.setEuiSwitch('saveAsNewCheckbox', state);
}

const redirectToOriginCheckboxExists = await testSubjects.exists('returnToOriginModeSwitch');
if (redirectToOriginCheckboxExists) {
const state = redirectToOrigin ? 'check' : 'uncheck';
await testSubjects.setEuiSwitch('returnToOriginModeSwitch', state);
}

const dashboardSelectorExists = await testSubjects.exists('add-to-dashboard-options');
if (dashboardSelectorExists) {
const dashboardSelector = await testSubjects.find('add-to-dashboard-options');
let optionSelector = 'add-to-library-option';
if (addToDashboard) {
optionSelector = dashboardId ? 'existing-dashboard-option' : 'new-dashboard-option';
}
log.debug('dashboard selector exists, choosing option:', optionSelector);
const label = await dashboardSelector.findByCssSelector(`label[for="${optionSelector}"]`);
await label.click();

if (dashboardId) {
// TODO - selecting an existing dashboard
}
}
await PageObjects.visualize.setSaveModalValues(title, {
saveAsNew,
redirectToOrigin,
addToDashboard,
dashboardId,
});

await testSubjects.click('confirmSaveSavedObjectButton');
await retry.waitForWithTimeout('Save modal to disappear', 1000, () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
await PageObjects.visEditor.clickGo();

await PageObjects.visualize.ensureSavePanelOpen();
await testSubjects.setValue('savedObjectTitle', 'My new markdown viz');

const dashboardSelector = await testSubjects.find('add-to-dashboard-options');
const label = await dashboardSelector.findByCssSelector(
`label[for="add-to-library-option"]`
);
await label.click();
await PageObjects.visualize.setSaveModalValues('My new markdown viz');

await selectSavedObjectTags('tag-1');

Expand All @@ -125,13 +119,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
await PageObjects.visEditor.clickGo();

await PageObjects.visualize.ensureSavePanelOpen();
await testSubjects.setValue('savedObjectTitle', 'vis-with-new-tag');

const dashboardSelector = await testSubjects.find('add-to-dashboard-options');
const label = await dashboardSelector.findByCssSelector(
`label[for="add-to-library-option"]`
);
await label.click();
await PageObjects.visualize.setSaveModalValues('vis-with-new-tag');

await testSubjects.click('savedObjectTagSelector');
await testSubjects.click(`tagSelectorOption-action__create`);
Expand Down

0 comments on commit 068aab1

Please sign in to comment.