Skip to content

Commit d0e023d

Browse files
committed
Refactor 6_notebooks.spec.js
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
1 parent 6e3cc01 commit d0e023d

File tree

2 files changed

+105
-83
lines changed

2 files changed

+105
-83
lines changed

cypress/integration/plugins/observability-dashboards/6_notebooks.spec.js

+93-73
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/// <reference types="cypress" />
77

88
import {
9-
TEST_NOTEBOOK,
109
SAMPLE_URL,
1110
BASE_PATH,
1211
delayTime,
@@ -16,81 +15,118 @@ import {
1615

1716
import { skipOn } from '@cypress/skip-test';
1817

19-
let loadedOnce = 0;
18+
const testNotebookName = () => {
19+
let code = (Math.random() + 1).toString(36).substring(7);
20+
return `Test Notebook ${code}`;
21+
};
2022

2123
const moveToNotebookHome = () => {
2224
cy.visit(`${BASE_PATH}/app/observability-notebooks#/`);
2325
};
2426

25-
const moveToTestNotebook = () => {
27+
const moveToTestNotebook = (notebookName) => {
2628
cy.visit(`${BASE_PATH}/app/observability-notebooks#/`, {
2729
timeout: delayTime * 3,
2830
});
2931

3032
// Force refresh the observablity index and reload page to load notebooks.
31-
if (loadedOnce === 0) {
32-
cy.request({
33+
cy.request({
34+
method: 'POST',
35+
failOnStatusCode: false,
36+
form: false,
37+
url: 'api/console/proxy',
38+
headers: {
39+
'content-type': 'application/json;charset=UTF-8',
40+
'osd-xsrf': true,
41+
},
42+
qs: {
43+
path: `${OBSERVABILITY_INDEX_NAME}/_refresh`,
3344
method: 'POST',
34-
failOnStatusCode: false,
35-
form: false,
36-
url: 'api/console/proxy',
37-
headers: {
38-
'content-type': 'application/json;charset=UTF-8',
39-
'osd-xsrf': true,
40-
},
41-
qs: {
42-
path: `${OBSERVABILITY_INDEX_NAME}/_refresh`,
43-
method: 'POST',
44-
},
45-
});
46-
cy.reload();
47-
loadedOnce = 1;
48-
}
45+
},
46+
});
47+
cy.reload();
4948

5049
cy.get('.euiTableCellContent')
51-
.contains(TEST_NOTEBOOK, {
50+
.contains(notebookName, {
5251
timeout: delayTime * 3,
5352
})
5453
.click();
5554
};
5655

56+
const makeTestNotebook = () => {
57+
let notebookName = testNotebookName();
58+
59+
moveToNotebookHome();
60+
cy.get('a[data-test-subj="createNotebookPrimaryBtn"]').click();
61+
cy.get('input[data-test-subj="custom-input-modal-input"]').focus();
62+
cy.get('input[data-test-subj="custom-input-modal-input"]').type(notebookName);
63+
cy.get('button[data-test-subj="custom-input-modal-confirm-button"]').click();
64+
cy.get('h1[data-test-subj="notebookTitle"]')
65+
.contains(notebookName)
66+
.should('exist');
67+
68+
return notebookName;
69+
};
70+
71+
const makeParagraph = () => {
72+
cy.get('button[data-test-subj="emptyNotebookAddCodeBlockBtn"]').click();
73+
cy.get('textarea[data-test-subj="editorArea-0"]').should('exist');
74+
};
75+
76+
const makePopulatedParagraph = () => {
77+
makeParagraph();
78+
cy.get('textarea[data-test-subj="editorArea-0"]').clear();
79+
cy.get('textarea[data-test-subj="editorArea-0"]').focus();
80+
cy.get('textarea[data-test-subj="editorArea-0"]').type(MARKDOWN_TEXT);
81+
cy.get('button[data-test-subj="runRefreshBtn-0"]').click();
82+
};
83+
84+
const deleteNotebook = (notebookName) => {
85+
moveToNotebookHome();
86+
87+
cy.contains('.euiTableRow', notebookName)
88+
.find('input[type="checkbox"]')
89+
.check();
90+
91+
cy.get('button[data-test-subj="notebookTableActionBtn"]').click();
92+
cy.get('button[data-test-subj="deleteNotebookBtn"]').click();
93+
94+
cy.get('input[data-test-subj="delete-notebook-modal-input"]').focus();
95+
cy.get('input[data-test-subj="delete-notebook-modal-input"]').type('delete');
96+
cy.get('button[data-test-subj="delete-notebook-modal-delete-button"]').should(
97+
'not.be.disabled'
98+
);
99+
cy.get(
100+
'button[data-test-subj="delete-notebook-modal-delete-button"]'
101+
).click();
102+
moveToNotebookHome();
103+
104+
cy.contains('.euiTableRow', notebookName).should('not.exist');
105+
};
106+
57107
describe('Testing notebook actions', () => {
58-
before(() => {
59-
moveToNotebookHome();
60-
cy.get('a[data-test-subj="createNotebookPrimaryBtn"]').click();
61-
cy.get('input[data-test-subj="custom-input-modal-input"]').focus();
62-
cy.get('input[data-test-subj="custom-input-modal-input"]').type(
63-
TEST_NOTEBOOK
64-
);
65-
cy.get(
66-
'button[data-test-subj="custom-input-modal-confirm-button"]'
67-
).click();
68-
cy.get('h1[data-test-subj="notebookTitle"]')
69-
.contains(TEST_NOTEBOOK)
70-
.should('exist');
108+
beforeEach(() => {
109+
let notebookName = makeTestNotebook();
110+
moveToTestNotebook(notebookName);
111+
cy.wrap({ name: notebookName }).as('notebook');
71112
});
72113

73-
beforeEach(() => {
74-
moveToTestNotebook();
114+
afterEach(function () {
115+
// ^^^^^^^^ Cannot use arrow callback to access beforeEach wrapper state
116+
deleteNotebook(this.notebook.name);
75117
});
76118

77119
it('Creates a code paragraph', () => {
78-
cy.get('button[data-test-subj="emptyNotebookAddCodeBlockBtn"]').click();
79-
cy.get('textarea[data-test-subj="editorArea-0"]').should('exist');
120+
makeParagraph();
121+
80122
cy.get('button[data-test-subj="runRefreshBtn-0"]').contains('Run').click();
81123
cy.get('div[data-test-subj="paragraphInputErrorText"]')
82124
.contains('Input is required.')
83125
.should('exist');
84126
});
85127

86128
it('Renders markdown', () => {
87-
cy.get('button[data-test-subj="paragraphToggleInputBtn"]').click();
88-
cy.get('.euiCodeBlock').click();
89-
cy.get('textarea[data-test-subj="editorArea-0"]').clear();
90-
cy.get('textarea[data-test-subj="editorArea-0"]').focus();
91-
cy.get('textarea[data-test-subj="editorArea-0"]').type(MARKDOWN_TEXT);
92-
93-
cy.get('button[data-test-subj="runRefreshBtn-0"]').click();
129+
makePopulatedParagraph();
94130
cy.get('textarea[data-test-subj="editorArea-0"]').should('not.exist');
95131
cy.get(`a[href="${SAMPLE_URL}"]`).should('exist');
96132
cy.get('code').contains('POST').should('exist');
@@ -100,17 +136,23 @@ describe('Testing notebook actions', () => {
100136

101137
describe('Test reporting integration if plugin installed', () => {
102138
beforeEach(() => {
103-
moveToNotebookHome();
104-
cy.get('.euiTableCellContent').contains(TEST_NOTEBOOK).click();
139+
let notebookName = makeTestNotebook();
105140
cy.get('h1[data-test-subj="notebookTitle"]')
106-
.contains(TEST_NOTEBOOK)
141+
.contains(notebookName)
107142
.should('exist');
108143
cy.get('body').then(($body) => {
109144
skipOn($body.find('#reportingActionsButton').length <= 0);
110145
});
146+
cy.wrap({ name: notebookName }).as('notebook');
147+
});
148+
149+
afterEach(function () {
150+
// ^^^^^^^^ Cannot use arrow callback to access beforeEach wrapper state
151+
deleteNotebook(this.notebook.name);
111152
});
112153

113154
it('Create in-context PDF report from notebook', () => {
155+
makePopulatedParagraph();
114156
cy.get('#reportingActionsButton').click();
115157
cy.get('button.euiContextMenuItem:nth-child(1)')
116158
.contains('Download PDF')
@@ -119,6 +161,7 @@ describe('Test reporting integration if plugin installed', () => {
119161
});
120162

121163
it('Create in-context PNG report from notebook', () => {
164+
makePopulatedParagraph();
122165
cy.get('#reportingActionsButton').click();
123166
cy.get('button.euiContextMenuItem:nth-child(2)')
124167
.contains('Download PNG')
@@ -127,6 +170,7 @@ describe('Test reporting integration if plugin installed', () => {
127170
});
128171

129172
it('Create on-demand report definition from context menu', () => {
173+
makePopulatedParagraph();
130174
cy.get('#reportingActionsButton').click();
131175
cy.get('button.euiContextMenuItem:nth-child(3)')
132176
.contains('Create report definition')
@@ -140,6 +184,7 @@ describe('Test reporting integration if plugin installed', () => {
140184
});
141185

142186
it('View reports homepage from context menu', () => {
187+
makePopulatedParagraph();
143188
cy.get('#reportingActionsButton').click();
144189
cy.get('button.euiContextMenuItem:nth-child(4)')
145190
.contains('View reports')
@@ -150,28 +195,3 @@ describe('Test reporting integration if plugin installed', () => {
150195
);
151196
});
152197
});
153-
154-
describe('clean up all test data', () => {
155-
it('Cleans up test notebooks', () => {
156-
moveToNotebookHome();
157-
cy.get('input[data-test-subj="checkboxSelectAll"]').click();
158-
cy.get('button[data-test-subj="notebookTableActionBtn"]').click();
159-
cy.get('button[data-test-subj="deleteNotebookBtn"]').click();
160-
cy.get(
161-
'button[data-test-subj="delete-notebook-modal-delete-button"]'
162-
).should('be.disabled');
163-
164-
cy.get('input[data-test-subj="delete-notebook-modal-input"]').focus();
165-
cy.get('input[data-test-subj="delete-notebook-modal-input"]').type(
166-
'delete'
167-
);
168-
cy.get(
169-
'button[data-test-subj="delete-notebook-modal-delete-button"]'
170-
).should('not.be.disabled');
171-
cy.get(
172-
'button[data-test-subj="delete-notebook-modal-delete-button"]'
173-
).click();
174-
moveToNotebookHome();
175-
cy.get('div[data-test-subj="notebookEmptyTableText"]').should('exist');
176-
});
177-
});

cypress/utils/plugins/observability-dashboards/constants.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ export const setTimeFilter = (setEndTime = false, refresh = true) => {
6262
cy.get('.euiTab__content').contains('Absolute').click();
6363
cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', {
6464
timeout: TIMEOUT_DELAY,
65-
})
66-
.focus()
67-
.type('{selectall}' + startTime, { force: true });
65+
}).focus();
66+
cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', {
67+
timeout: TIMEOUT_DELAY,
68+
}).type('{selectall}' + startTime, { force: true });
6869
if (setEndTime) {
6970
cy.wait(delayTime);
7071
cy.get(
@@ -74,16 +75,16 @@ export const setTimeFilter = (setEndTime = false, refresh = true) => {
7475
cy.get('.euiTab__content').contains('Absolute').click();
7576
cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', {
7677
timeout: TIMEOUT_DELAY,
77-
})
78-
.focus()
79-
.type('{selectall}' + endTime, { force: true });
78+
}).focus();
79+
cy.get('input[data-test-subj="superDatePickerAbsoluteDateInput"]', {
80+
timeout: TIMEOUT_DELAY,
81+
}).type('{selectall}' + endTime, { force: true });
8082
}
8183
if (refresh) cy.get('.euiButton__text').contains('Refresh').click();
8284
cy.wait(delayTime);
8385
};
8486

8587
// notebooks
86-
export const TEST_NOTEBOOK = 'Test Notebook';
8788
export const SAMPLE_URL =
8889
'https://github.com/opensearch-project/sql/tree/main/sql-jdbc';
8990
export const MARKDOWN_TEXT = `%md
@@ -301,9 +302,10 @@ export const moveToEditPage = () => {
301302
export const changeTimeTo24 = (timeUnit) => {
302303
cy.get('[data-test-subj="superDatePickerToggleQuickMenuButton"]', {
303304
timeout: TIMEOUT_DELAY,
304-
})
305-
.trigger('mouseover')
306-
.click();
305+
}).trigger('mouseover');
306+
cy.get('[data-test-subj="superDatePickerToggleQuickMenuButton"]', {
307+
timeout: TIMEOUT_DELAY,
308+
}).click();
307309
cy.wait(delayTime);
308310
cy.get('[aria-label="Time unit"]', { timeout: TIMEOUT_DELAY }).select(
309311
timeUnit

0 commit comments

Comments
 (0)