Skip to content

Commit a08554f

Browse files
Added automated tests with cypress for Namespace form
1 parent 893118b commit a08554f

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/* eslint-disable no-undef */
2+
import { flashClassMap } from '../../../../../support/assertions/assertion_constants';
3+
4+
// Menu options
5+
const AUTOMATION_MENU_OPTION = 'Automation';
6+
const EMBEDDED_AUTOMATION_MENU_OPTION = 'Embedded Automate';
7+
const EXPLORER_MENU_OPTION = 'Explorer';
8+
9+
// Toolbar options
10+
const TOOLBAR_CONFIGURATION = 'Configuration';
11+
const TOOLBAR_ADD_NEW_DOMAIN = 'Add a New Domain';
12+
const TOOLBAR_ADD_NEW_NAMESPACE = 'Add a New Namespace';
13+
const TOOLBAR_EDIT_NAMESPACE = 'Edit this Namespace';
14+
const TOOLBAR_REMOVE_NAMESPACE = 'Remove this Namespace';
15+
16+
// Field values
17+
const DOMAIN_NAME = 'Test_Domain';
18+
const DESCRIPTION = 'Test description';
19+
const NAMESPACE_NAME = 'Test_Namespace';
20+
const EDITED_NAMESPACE_NAME = 'Test_Namespace_Edited';
21+
const EDITED_DESCRIPTION = 'Test description edited';
22+
const INVALID_NAMESPACE_NAME = 'Test Namespace';
23+
const NAMESPACE_FORM_HEADER = 'Automate Namespace';
24+
const NAMESPACE_FORM_SUB_HEADER = 'Info';
25+
26+
// List items
27+
const DATA_STORE_ACCORDION_ITEM = 'Datastore';
28+
29+
// Buttons
30+
const ADD_BUTTON_TEXT = 'Add';
31+
const CANCEL_BUTTON_TEXT = 'Cancel';
32+
const SAVE_BUTTON_TEXT = 'Save';
33+
const RESET_BUTTON_TEXT = 'Reset';
34+
35+
const FLASH_TYPE_ERROR = 'error';
36+
37+
// Flash message text snippets
38+
const FLASH_MESSAGE_ADD_SUCCESS = 'added';
39+
const FLASH_MESSAGE_SAVE_SUCCESS = 'saved';
40+
const FLASH_MESSAGE_CANCELLED = 'cancel';
41+
const FLASH_MESSAGE_INVALID_NAMESPACE = 'contain only alphanumeric';
42+
const FLASH_MESSAGE_NAMESPACE_REMOVED = 'delete successful';
43+
const FLASH_MESSAGE_NAME_ALREADY_EXISTS = 'taken';
44+
const FLASH_MESSAGE_RESET_NAMESPACE = 'reset';
45+
const BROWSER_CONFIRM_REMOVE_MESSAGE = 'remove';
46+
47+
function addNamespace(nameFieldValue) {
48+
// Navigating to the Add Namespace form
49+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_ADD_NEW_NAMESPACE);
50+
// Creating a new namespace
51+
cy.getFormInputFieldById('name').type(nameFieldValue);
52+
cy.getFormInputFieldById('description').type(DESCRIPTION);
53+
cy.getFormFooterButtonByType(ADD_BUTTON_TEXT, 'submit').click();
54+
cy.wait('@addNamespaceApi');
55+
}
56+
57+
function selectAccordionElement(accordionItemLabel) {
58+
const pathToTargetNode =
59+
accordionItemLabel === NAMESPACE_NAME
60+
? [DOMAIN_NAME, NAMESPACE_NAME]
61+
: [DOMAIN_NAME];
62+
cy.interceptApi({
63+
alias: 'treeSelectApi',
64+
urlPattern: /\/miq_ae_class\/tree_select\?id=.*&text=.*/,
65+
triggerFn: () =>
66+
cy.selectAccordionItem([DATA_STORE_ACCORDION_ITEM, ...pathToTargetNode]),
67+
});
68+
}
69+
70+
function validateNamespaceFormFields(isEditForm = false) {
71+
// Assert form header is visible
72+
cy.expect_explorer_title(NAMESPACE_FORM_HEADER);
73+
// Assert sub header is visible
74+
cy.get('#main-content #datastore-form-wrapper h3').contains(
75+
NAMESPACE_FORM_SUB_HEADER
76+
);
77+
// Assert name-space path field label is visible
78+
cy.getFormLabelByInputId('namespacePath').should('be.visible');
79+
// Assert name-space path field is visible and disabled
80+
cy.getFormInputFieldById('namespacePath')
81+
.should('be.visible')
82+
.and('be.disabled')
83+
.invoke('val')
84+
.should('include', DOMAIN_NAME);
85+
// Assert name field label is visible
86+
cy.getFormLabelByInputId('name').should('be.visible');
87+
// Assert name field is visible and enabled
88+
cy.getFormInputFieldById('name').should('be.visible').and('be.enabled');
89+
// Assert description field label is visible
90+
cy.getFormLabelByInputId('description').should('be.visible');
91+
// Assert description field is visible and enabled
92+
cy.getFormInputFieldById('description')
93+
.should('be.visible')
94+
.and('be.enabled');
95+
// Assert cancel button is visible and enabled
96+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT)
97+
.should('be.visible')
98+
.and('be.enabled');
99+
// Assert add/save button is visible and disabled
100+
cy.getFormFooterButtonByType(
101+
isEditForm ? SAVE_BUTTON_TEXT : ADD_BUTTON_TEXT,
102+
'submit'
103+
)
104+
.should('be.visible')
105+
.and('be.disabled');
106+
if (isEditForm) {
107+
// Assert reset button is visible and disabled
108+
cy.getFormFooterButtonByType(RESET_BUTTON_TEXT)
109+
.should('be.visible')
110+
.and('be.disabled');
111+
}
112+
}
113+
114+
function createNamespaceAndOpenEditForm() {
115+
// Adding a new namespace
116+
addNamespace(NAMESPACE_NAME);
117+
// Selecting the created namespace from the accordion list items
118+
selectAccordionElement(NAMESPACE_NAME);
119+
// Opening the edit form
120+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_EDIT_NAMESPACE);
121+
}
122+
123+
function extractDomainIdAndTokenFromResponse(interception) {
124+
const rawTreeObject = interception?.response?.body?.reloadTrees?.ae_tree;
125+
if (rawTreeObject) {
126+
const rawTreeParsed = JSON.parse(rawTreeObject);
127+
rawTreeParsed.every((treeObject) => {
128+
// Exit iteration once id is extracted from nodes array
129+
return treeObject?.nodes?.every((nodeObject) => {
130+
if (nodeObject?.text === DOMAIN_NAME) {
131+
const domainId = nodeObject?.key?.split('-')?.[1];
132+
const csrfToken = interception?.request?.headers?.['x-csrf-token'];
133+
const idAndToken = {
134+
domainId,
135+
csrfToken,
136+
};
137+
// Creating an aliased state to store id and token
138+
cy.wrap(idAndToken).as('idAndToken');
139+
140+
// Stop iterating once the domain id is found
141+
return false;
142+
}
143+
// Continue iterating
144+
return true;
145+
});
146+
});
147+
}
148+
}
149+
150+
describe('Automate operations on Namespaces: Automation -> Embedded Automate -> Explorer -> {Any-created-domain} -> Namespace form', () => {
151+
beforeEach(() => {
152+
cy.login();
153+
cy.menu(
154+
AUTOMATION_MENU_OPTION,
155+
EMBEDDED_AUTOMATION_MENU_OPTION,
156+
EXPLORER_MENU_OPTION
157+
);
158+
// Creating a domain to test namespace operations
159+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_ADD_NEW_DOMAIN);
160+
cy.getFormInputFieldById('name').type(DOMAIN_NAME);
161+
cy.getFormInputFieldById('description').type(DESCRIPTION);
162+
cy.intercept('POST', '/miq_ae_class/create_namespace/new?button=add').as(
163+
'addNamespaceApi'
164+
);
165+
cy.getFormFooterButtonByType(ADD_BUTTON_TEXT, 'submit').click();
166+
cy.wait('@addNamespaceApi').then((interception) => {
167+
extractDomainIdAndTokenFromResponse(interception);
168+
});
169+
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_ADD_SUCCESS);
170+
// Selecting the created domain from the accordion list items
171+
selectAccordionElement(DOMAIN_NAME);
172+
});
173+
174+
it('Validate Add Namespace form fields', () => {
175+
// Navigating to the Add Namespace form
176+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_ADD_NEW_NAMESPACE);
177+
178+
// Validating the form fields
179+
validateNamespaceFormFields();
180+
181+
// Cancelling the form
182+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
183+
});
184+
185+
it('Validate Cancel button', () => {
186+
// Navigating to the Add Namespace form
187+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_ADD_NEW_NAMESPACE);
188+
189+
// Cancelling the form
190+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT)
191+
.should('be.enabled')
192+
.click();
193+
cy.expect_flash(flashClassMap.warning, FLASH_MESSAGE_CANCELLED);
194+
});
195+
196+
it('Validate Name field allows only alphanumeric and _ . - $ characters', () => {
197+
// Trying to add a namespace with invalid characters
198+
addNamespace(INVALID_NAMESPACE_NAME);
199+
cy.expect_flash(flashClassMap.error, FLASH_MESSAGE_INVALID_NAMESPACE);
200+
201+
// Cancelling the form
202+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
203+
});
204+
205+
it('Validate Edit Namespace form fields', () => {
206+
// Create a namespace and open the edit form
207+
createNamespaceAndOpenEditForm();
208+
209+
// Validating the form fields
210+
validateNamespaceFormFields(true);
211+
212+
// Cancelling the form
213+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
214+
});
215+
216+
it('Checking whether add, edit & delete namespace works', () => {
217+
// Adding a new namespace
218+
addNamespace(NAMESPACE_NAME);
219+
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_ADD_SUCCESS);
220+
221+
// Selecting the created namespace from the accordion list items
222+
selectAccordionElement(NAMESPACE_NAME);
223+
// Editing the namespace
224+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_EDIT_NAMESPACE);
225+
// Checking if the Save button is disabled initially
226+
cy.getFormFooterButtonByType(SAVE_BUTTON_TEXT, 'submit').should(
227+
'be.disabled'
228+
);
229+
cy.getFormInputFieldById('description').clear().type(EDITED_DESCRIPTION);
230+
cy.getFormFooterButtonByType(SAVE_BUTTON_TEXT, 'submit')
231+
.should('be.enabled')
232+
.click();
233+
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_SAVE_SUCCESS);
234+
235+
// Deleting the namespace
236+
cy.expect_browser_confirm_with_text({
237+
confirmTriggerFn: () =>
238+
cy.toolbar(TOOLBAR_CONFIGURATION, TOOLBAR_REMOVE_NAMESPACE),
239+
containsText: BROWSER_CONFIRM_REMOVE_MESSAGE,
240+
});
241+
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_NAMESPACE_REMOVED);
242+
});
243+
244+
it('Checking whether creating a duplicate namespace is restricted', () => {
245+
// Adding a new namespace
246+
addNamespace(NAMESPACE_NAME);
247+
// Trying to add duplicate namespace
248+
addNamespace(NAMESPACE_NAME);
249+
cy.expect_flash(flashClassMap.error, FLASH_MESSAGE_NAME_ALREADY_EXISTS);
250+
251+
// Cancelling the form
252+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
253+
});
254+
255+
it('Checking whether Cancel & Reset buttons work fine in the Edit form', () => {
256+
// Create a namespace and open the edit form
257+
createNamespaceAndOpenEditForm();
258+
259+
/* Validating Reset button */
260+
// Checking if the Reset button is disabled initially
261+
cy.getFormFooterButtonByType(RESET_BUTTON_TEXT).should('be.disabled');
262+
// Editing name and description fields
263+
cy.getFormInputFieldById('name').clear().type(EDITED_NAMESPACE_NAME);
264+
cy.getFormInputFieldById('description').clear().type(EDITED_DESCRIPTION);
265+
// Resetting
266+
cy.getFormFooterButtonByType(RESET_BUTTON_TEXT)
267+
.should('be.enabled')
268+
.click();
269+
cy.expect_flash(flashClassMap.warning, FLASH_MESSAGE_RESET_NAMESPACE);
270+
// Confirming the edited fields contain the old values after resetting
271+
cy.getFormInputFieldById('name').should('have.value', NAMESPACE_NAME);
272+
cy.getFormInputFieldById('description').should('have.value', DESCRIPTION);
273+
274+
/* Validating Cancel button */
275+
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
276+
cy.expect_flash(flashClassMap.warning, FLASH_MESSAGE_CANCELLED);
277+
});
278+
279+
afterEach(() => {
280+
// Selecting the created domain(Test_Domain) from the accordion list items
281+
selectAccordionTree(dataStoreAccordionItem);
282+
cy.accordionItem(domainName);
283+
cy.wait('@getCreatedDomainInfo');
284+
// Removing the domain
285+
cy.expect_browser_confirm_with_text({
286+
confirmTriggerFn: () =>
287+
cy.toolbar(toolbarConfiguration, toolbarRemoveDomain),
288+
containsText: browserConfirmRemoveMessage,
289+
});
290+
});
291+
});

0 commit comments

Comments
 (0)