Skip to content

Commit edb66f0

Browse files
authored
Create cypress command namespacing util (#9150)
1 parent b04af6b commit edb66f0

File tree

14 files changed

+96
-39
lines changed

14 files changed

+96
-39
lines changed

changelogs/fragments/9150.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chore:
2+
- Create cypress command namespacing util ([#9150](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9150))

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/a_check.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ describe('No Index Pattern Check Test', () => {
3131
authType: 'no_auth',
3232
});
3333
// Create workspace
34-
cy.deleteWorkspaceByName(`${workspace}`);
34+
cy.deleteWorkspaceByName(workspace);
3535
cy.visit('/app/home');
36-
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
36+
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
3737
cy.wait(2000);
3838
});
3939

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/dataset_selector.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ describe('dataset selector', { scrollBehavior: false }, () => {
3030
});
3131
beforeEach(() => {
3232
// Create workspace
33-
cy.deleteWorkspaceByName(`${workspace}`);
33+
cy.deleteWorkspaceByName(workspace);
3434
cy.visit('/app/home');
35-
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
35+
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
3636
cy.navigateToWorkSpaceSpecificPage({
3737
workspaceName: workspace,
3838
page: 'discover',

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/field_display_filtering.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('filter for value spec', () => {
3838
// Create workspace
3939
cy.deleteWorkspaceByName(workspace);
4040
cy.visit('/app/home');
41-
cy.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
41+
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
4242
cy.wait(2000);
4343
cy.createWorkspaceIndexPatterns({
4444
workspaceName: workspace,

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/queries.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ describe('query enhancement queries', { scrollBehavior: false }, () => {
2626
});
2727

2828
// Create workspace and set up index pattern
29-
cy.deleteWorkspaceByName(`${workspace}`);
29+
cy.deleteWorkspaceByName(workspace);
3030
cy.visit('/app/home');
31-
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
31+
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
3232

3333
// Create and select index pattern for data_logs_small_time_1*
3434
cy.createWorkspaceIndexPatterns({

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/s3_dataset.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const definedS3Variables = !S3_CLUSTER.url;
8787
// Create workspace
8888
cy.deleteWorkspaceByName(workspace);
8989
cy.visit('/app/home');
90-
cy.createInitialWorkspaceWithDataSource(S3_CLUSTER.name, workspace);
90+
cy.osd.createInitialWorkspaceWithDataSource(S3_CLUSTER.name, WORKSPACE_NAME);
9191
});
9292
afterEach(() => {
9393
cy.deleteWorkspaceByName(workspace);

cypress/integration/core_opensearch_dashboards/opensearch_dashboards/apps/query_enhancements/saved_search.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const runSavedSearchTests = () => {
5353
// Create workspace
5454
cy.deleteWorkspaceByName(workspaceName);
5555
cy.visit('/app/home');
56-
cy.createInitialWorkspaceWithDataSource(datasourceName, workspaceName);
56+
cy.osd.createInitialWorkspaceWithDataSource(datasourceName, workspaceName);
5757
cy.createWorkspaceIndexPatterns({
5858
workspaceName: workspaceName,
5959
indexPattern: INDEX_PATTERN_WITH_TIME.replace('*', ''),

cypress/utils/apps/workspace/commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Cypress.Commands.add(
99
(workspaceName) => {
1010
// Selecting the correct workspace
1111
cy.visit('/app/workspace_list#');
12-
cy.openWorkspaceDashboard(workspaceName);
12+
cy.osd.openWorkspaceDashboard(workspaceName);
1313
// wait until page loads
1414
cy.getElementByTestId('headerAppActionMenu').should('be.visible');
1515
}

cypress/utils/command_namespace.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* Initializes a command namespace on the cy object
8+
*
9+
* Ex usage:
10+
* initCommandNamespace(cy, 'osd'); // initializes osd namespace (only needed once per ns)
11+
* cy.osd.add('myNewCommand', (myArg) => ...); // register command to namespace
12+
* cy.osd.myNewCommand('someArg'); // executes command
13+
*
14+
*/
15+
export default function initCommandNamespace(cy, namespace) {
16+
/**
17+
* this proxy is responsible for intercepting access to properties
18+
* it's what allows us to dynamically define properties at runtime like cy.osd.myNewCommand
19+
*/
20+
cy[namespace] = new Proxy(
21+
{
22+
// register a new namespaced command with cypress (ex. osd:myNewCommand)
23+
add(commandName, callback) {
24+
Cypress.Commands.add(namespaceCommand(commandName), callback);
25+
},
26+
},
27+
{
28+
get(target, property) {
29+
if (target[property]) {
30+
// return reserved property (add method)
31+
return target[property];
32+
}
33+
34+
// return the mapped namespace command (ex. myNewCommand returns the osd:myNewCommand command)
35+
return cy[namespaceCommand(property)];
36+
},
37+
}
38+
);
39+
40+
function namespaceCommand(commandName) {
41+
return `${namespace}:${commandName}`;
42+
}
43+
}

cypress/utils/commands.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import { BASE_PATH } from './constants';
1111
import { TestFixtureHandler } from '../lib/test_fixture_handler';
12+
import initCommandNamespace from './command_namespace';
13+
14+
initCommandNamespace(cy, 'osd');
1215

1316
// This function does not delete all indices
1417
Cypress.Commands.add('deleteAllIndices', () => {
@@ -322,9 +325,8 @@ Cypress.Commands.add('deleteWorkspace', (workspaceName) => {
322325
cy.contains(/successfully/);
323326
});
324327

325-
Cypress.Commands.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, workspaceName) => {
328+
cy.osd.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, workspaceName) => {
326329
cy.intercept('POST', '/api/workspaces').as('createWorkspaceInterception');
327-
328330
cy.getElementByTestId('workspace-initial-card-createWorkspace-button')
329331
.should('be.visible')
330332
.click();
@@ -351,7 +353,7 @@ Cypress.Commands.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, w
351353
cy.contains(/successfully/);
352354
});
353355

354-
Cypress.Commands.add('openWorkspaceDashboard', (workspaceName) => {
356+
cy.osd.add('openWorkspaceDashboard', (workspaceName) => {
355357
cy.getElementByTestId('workspace-select-button').should('exist').click();
356358
cy.getElementByTestId('workspace-menu-manage-button').should('exist').click();
357359
cy.get('.euiBasicTable')

cypress/utils/dashboards/remove_workspace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function removeSampleDataAndWorkspace(url, workspaceName) {
77
describe('removing workspace/sampledata', () => {
88
it('remove workspace', () => {
99
cy.visit(`${url}/app/workspace_list`);
10-
cy.openWorkspaceDashboard(workspaceName);
10+
cy.osd.openWorkspaceDashboard(workspaceName);
1111
cy.getElementByTestId('toggleNavButton').eq(0).should('exist').click();
1212
cy.wait(3000);
1313
cy.getElementByTestId('collapsibleNavAppLink-workspace_detail').should('exist').click();

cypress/utils/dashboards/setup_workspace.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@
66
const dataSourceTitle = Cypress.env('dataSourceTitle');
77

88
export function createWorkspaceAndSampleData(url, workspaceName) {
9-
describe('checking home page', () => {
10-
it('checking workspace initial page', () => {
11-
cy.visit(`${url}/app/home`);
12-
cy.getElementByTestId('workspace-initial-card-createWorkspace-button').should('be.visible');
9+
describe('setup workspace', () => {
10+
describe('checking home page', () => {
11+
it('checking workspace initial page', () => {
12+
cy.visit(`${url}/app/home`);
13+
cy.getElementByTestId('workspace-initial-card-createWorkspace-button').should('be.visible');
14+
});
1315
});
14-
});
1516

16-
describe('creating workspace', () => {
17-
it('creating workspace with data source', () => {
18-
cy.visit(`${url}/app/home`);
19-
cy.createInitialWorkspaceWithDataSource(dataSourceTitle, workspaceName);
20-
cy.wait(2000);
17+
describe('creating workspace', () => {
18+
it('creating workspace with data source', () => {
19+
cy.visit(`${url}/app/home`);
20+
cy.osd.createInitialWorkspaceWithDataSource(dataSourceTitle, workspaceName);
21+
cy.wait(2000);
22+
});
2123
});
22-
});
2324

24-
describe('adding sample data to workspace', () => {
25-
it('add sample data to data source', () => {
26-
cy.visit(`${url}/app/workspace_list`);
27-
cy.openWorkspaceDashboard(workspaceName);
28-
cy.openSampleDataPage();
29-
cy.addSampleDataToDataSource(dataSourceTitle);
25+
describe('adding sample data to workspace', () => {
26+
it('add sample data to data source', () => {
27+
cy.visit(`${url}/app/workspace_list`);
28+
cy.osd.openWorkspaceDashboard(workspaceName);
29+
cy.openSampleDataPage();
30+
cy.addSampleDataToDataSource(dataSourceTitle);
31+
});
3032
});
3133
});
3234
}

cypress/utils/index.d.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,21 @@ declare namespace Cypress {
182182
*/
183183
drag<S = any>(targetSelector: string): Chainable<S>;
184184

185-
/**
186-
* Creates workspace and attaches it to the provided data source
187-
* It also saves the created workspace id as the alias @WORKSPACE_ID
188-
*/
189-
createInitialWorkspaceWithDataSource<S = any>(
190-
dataSourceTitle: string,
191-
workspaceName: string
192-
): Chainable<S>;
185+
// osd namespace
186+
osd: {
187+
/**
188+
* Creates workspace and attaches it to the provided data source
189+
* It also saves the created workspace id as the alias @WORKSPACE_ID
190+
*/
191+
createInitialWorkspaceWithDataSource<S = any>(
192+
dataSourceTitle: string,
193+
workspaceName: string
194+
): Chainable<S>;
195+
196+
/**
197+
* Opens workspace dashboard
198+
*/
199+
openWorkspaceDashboard<S = any>(workspaceName: string): Chainable<S>;
200+
};
193201
}
194202
}

0 commit comments

Comments
 (0)