From 8ec090ca5771bdfe666b0c5eba523ff79ead3651 Mon Sep 17 00:00:00 2001 From: Simeon Widdis Date: Tue, 26 Mar 2024 19:41:25 -0700 Subject: [PATCH] Refactor 6_notebooks.spec.js (#1174) * Refactor 6_notebooks.spec.js Signed-off-by: Simeon Widdis * Simplify reporting integration before-each Signed-off-by: Simeon Widdis * Avoid using this on notebook name access Signed-off-by: Simeon Widdis --------- Signed-off-by: Simeon Widdis --- .../6_notebooks.spec.js | 166 ++++++++++-------- .../observability-dashboards/constants.js | 22 +-- 2 files changed, 103 insertions(+), 85 deletions(-) diff --git a/cypress/integration/plugins/observability-dashboards/6_notebooks.spec.js b/cypress/integration/plugins/observability-dashboards/6_notebooks.spec.js index 8c6ebd3d8..e3b4afde5 100644 --- a/cypress/integration/plugins/observability-dashboards/6_notebooks.spec.js +++ b/cypress/integration/plugins/observability-dashboards/6_notebooks.spec.js @@ -6,7 +6,6 @@ /// import { - TEST_NOTEBOOK, SAMPLE_URL, BASE_PATH, delayTime, @@ -16,67 +15,111 @@ import { import { skipOn } from '@cypress/skip-test'; -let loadedOnce = 0; +const testNotebookName = () => { + let code = (Math.random() + 1).toString(36).substring(7); + return `Test Notebook ${code}`; +}; const moveToNotebookHome = () => { cy.visit(`${BASE_PATH}/app/observability-notebooks#/`); }; -const moveToTestNotebook = () => { +const moveToTestNotebook = (notebookName) => { cy.visit(`${BASE_PATH}/app/observability-notebooks#/`, { timeout: delayTime * 3, }); // Force refresh the observablity index and reload page to load notebooks. - if (loadedOnce === 0) { - cy.request({ + cy.request({ + method: 'POST', + failOnStatusCode: false, + form: false, + url: 'api/console/proxy', + headers: { + 'content-type': 'application/json;charset=UTF-8', + 'osd-xsrf': true, + }, + qs: { + path: `${OBSERVABILITY_INDEX_NAME}/_refresh`, method: 'POST', - failOnStatusCode: false, - form: false, - url: 'api/console/proxy', - headers: { - 'content-type': 'application/json;charset=UTF-8', - 'osd-xsrf': true, - }, - qs: { - path: `${OBSERVABILITY_INDEX_NAME}/_refresh`, - method: 'POST', - }, - }); - cy.reload(); - loadedOnce = 1; - } + }, + }); + cy.reload(); cy.get('.euiTableCellContent') - .contains(TEST_NOTEBOOK, { + .contains(notebookName, { timeout: delayTime * 3, }) .click(); }; +const makeTestNotebook = () => { + let notebookName = testNotebookName(); + + moveToNotebookHome(); + cy.get('a[data-test-subj="createNotebookPrimaryBtn"]').click(); + cy.get('input[data-test-subj="custom-input-modal-input"]').focus(); + cy.get('input[data-test-subj="custom-input-modal-input"]').type(notebookName); + cy.get('button[data-test-subj="custom-input-modal-confirm-button"]').click(); + cy.get('h1[data-test-subj="notebookTitle"]') + .contains(notebookName) + .should('exist'); + + return notebookName; +}; + +const makeParagraph = () => { + cy.get('button[data-test-subj="emptyNotebookAddCodeBlockBtn"]').click(); + cy.get('textarea[data-test-subj="editorArea-0"]').should('exist'); +}; + +const makePopulatedParagraph = () => { + makeParagraph(); + cy.get('textarea[data-test-subj="editorArea-0"]').clear(); + cy.get('textarea[data-test-subj="editorArea-0"]').focus(); + cy.get('textarea[data-test-subj="editorArea-0"]').type(MARKDOWN_TEXT); + cy.get('button[data-test-subj="runRefreshBtn-0"]').click(); +}; + +const deleteNotebook = (notebookName) => { + moveToNotebookHome(); + + cy.contains('.euiTableRow', notebookName) + .find('input[type="checkbox"]') + .check(); + + cy.get('button[data-test-subj="notebookTableActionBtn"]').click(); + cy.get('button[data-test-subj="deleteNotebookBtn"]').click(); + + cy.get('input[data-test-subj="delete-notebook-modal-input"]').focus(); + cy.get('input[data-test-subj="delete-notebook-modal-input"]').type('delete'); + cy.get('button[data-test-subj="delete-notebook-modal-delete-button"]').should( + 'not.be.disabled' + ); + cy.get( + 'button[data-test-subj="delete-notebook-modal-delete-button"]' + ).click(); + moveToNotebookHome(); + + cy.contains('.euiTableRow', notebookName).should('not.exist'); +}; + describe('Testing notebook actions', () => { - before(() => { - moveToNotebookHome(); - cy.get('a[data-test-subj="createNotebookPrimaryBtn"]').click(); - cy.get('input[data-test-subj="custom-input-modal-input"]').focus(); - cy.get('input[data-test-subj="custom-input-modal-input"]').type( - TEST_NOTEBOOK - ); - cy.get( - 'button[data-test-subj="custom-input-modal-confirm-button"]' - ).click(); - cy.get('h1[data-test-subj="notebookTitle"]') - .contains(TEST_NOTEBOOK) - .should('exist'); + beforeEach(() => { + let notebookName = makeTestNotebook(); + moveToTestNotebook(notebookName); + cy.wrap({ name: notebookName }).as('notebook'); }); - beforeEach(() => { - moveToTestNotebook(); + afterEach(() => { + cy.get('@notebook').then((notebook) => { + deleteNotebook(notebook.name); + }); }); it('Creates a code paragraph', () => { - cy.get('button[data-test-subj="emptyNotebookAddCodeBlockBtn"]').click(); - cy.get('textarea[data-test-subj="editorArea-0"]').should('exist'); + makeParagraph(); + cy.get('button[data-test-subj="runRefreshBtn-0"]').contains('Run').click(); cy.get('div[data-test-subj="paragraphInputErrorText"]') .contains('Input is required.') @@ -84,13 +127,7 @@ describe('Testing notebook actions', () => { }); it('Renders markdown', () => { - cy.get('button[data-test-subj="paragraphToggleInputBtn"]').click(); - cy.get('.euiCodeBlock').click(); - cy.get('textarea[data-test-subj="editorArea-0"]').clear(); - cy.get('textarea[data-test-subj="editorArea-0"]').focus(); - cy.get('textarea[data-test-subj="editorArea-0"]').type(MARKDOWN_TEXT); - - cy.get('button[data-test-subj="runRefreshBtn-0"]').click(); + makePopulatedParagraph(); cy.get('textarea[data-test-subj="editorArea-0"]').should('not.exist'); cy.get(`a[href="${SAMPLE_URL}"]`).should('exist'); cy.get('code').contains('POST').should('exist'); @@ -100,14 +137,18 @@ describe('Testing notebook actions', () => { describe('Test reporting integration if plugin installed', () => { beforeEach(() => { - moveToNotebookHome(); - cy.get('.euiTableCellContent').contains(TEST_NOTEBOOK).click(); - cy.get('h1[data-test-subj="notebookTitle"]') - .contains(TEST_NOTEBOOK) - .should('exist'); + let notebookName = makeTestNotebook(); cy.get('body').then(($body) => { skipOn($body.find('#reportingActionsButton').length <= 0); }); + makePopulatedParagraph(); + cy.wrap({ name: notebookName }).as('notebook'); + }); + + afterEach(() => { + cy.get('@notebook').then((notebook) => { + deleteNotebook(notebook.name); + }); }); it('Create in-context PDF report from notebook', () => { @@ -150,28 +191,3 @@ describe('Test reporting integration if plugin installed', () => { ); }); }); - -describe('clean up all test data', () => { - it('Cleans up test notebooks', () => { - moveToNotebookHome(); - cy.get('input[data-test-subj="checkboxSelectAll"]').click(); - cy.get('button[data-test-subj="notebookTableActionBtn"]').click(); - cy.get('button[data-test-subj="deleteNotebookBtn"]').click(); - cy.get( - 'button[data-test-subj="delete-notebook-modal-delete-button"]' - ).should('be.disabled'); - - cy.get('input[data-test-subj="delete-notebook-modal-input"]').focus(); - cy.get('input[data-test-subj="delete-notebook-modal-input"]').type( - 'delete' - ); - cy.get( - 'button[data-test-subj="delete-notebook-modal-delete-button"]' - ).should('not.be.disabled'); - cy.get( - 'button[data-test-subj="delete-notebook-modal-delete-button"]' - ).click(); - moveToNotebookHome(); - cy.get('div[data-test-subj="notebookEmptyTableText"]').should('exist'); - }); -}); diff --git a/cypress/utils/plugins/observability-dashboards/constants.js b/cypress/utils/plugins/observability-dashboards/constants.js index 0e9edd4f7..99e2b5c90 100644 --- a/cypress/utils/plugins/observability-dashboards/constants.js +++ b/cypress/utils/plugins/observability-dashboards/constants.js @@ -62,9 +62,10 @@ export const setTimeFilter = (setEndTime = false, refresh = true) => { cy.get('.euiTab__content').contains('Absolute').click(); cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', { timeout: TIMEOUT_DELAY, - }) - .focus() - .type('{selectall}' + startTime, { force: true }); + }).focus(); + cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', { + timeout: TIMEOUT_DELAY, + }).type('{selectall}' + startTime, { force: true }); if (setEndTime) { cy.wait(delayTime); cy.get( @@ -74,16 +75,16 @@ export const setTimeFilter = (setEndTime = false, refresh = true) => { cy.get('.euiTab__content').contains('Absolute').click(); cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', { timeout: TIMEOUT_DELAY, - }) - .focus() - .type('{selectall}' + endTime, { force: true }); + }).focus(); + cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', { + timeout: TIMEOUT_DELAY, + }).type('{selectall}' + endTime, { force: true }); } if (refresh) cy.get('.euiButton__text').contains('Refresh').click(); cy.wait(delayTime); }; // notebooks -export const TEST_NOTEBOOK = 'Test Notebook'; export const SAMPLE_URL = 'https://github.com/opensearch-project/sql/tree/main/sql-jdbc'; export const MARKDOWN_TEXT = `%md @@ -301,9 +302,10 @@ export const moveToEditPage = () => { export const changeTimeTo24 = (timeUnit) => { cy.get('[data-test-subj="superDatePickerToggleQuickMenuButton"]', { timeout: TIMEOUT_DELAY, - }) - .trigger('mouseover') - .click(); + }).trigger('mouseover'); + cy.get('[data-test-subj="superDatePickerToggleQuickMenuButton"]', { + timeout: TIMEOUT_DELAY, + }).click(); cy.wait(delayTime); cy.get('[aria-label="Time unit"]', { timeout: TIMEOUT_DELAY }).select( timeUnit