Skip to content

Commit

Permalink
Create cypress command namespacing util (#9150)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-buckner authored and FriedhelmWS committed Jan 22, 2025
1 parent 6c62785 commit 78956d0
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 39 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9150.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chore:
- Create cypress command namespacing util ([#9150](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9150))
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('No Index Pattern Check Test', () => {
authType: 'no_auth',
});
// Create workspace
cy.deleteWorkspaceByName(`${workspace}`);
cy.deleteWorkspaceByName(workspace);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
cy.wait(2000);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe('dataset selector', { scrollBehavior: false }, () => {
});
beforeEach(() => {
// Create workspace
cy.deleteWorkspaceByName(`${workspace}`);
cy.deleteWorkspaceByName(workspace);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
cy.navigateToWorkSpaceSpecificPage({
workspaceName: workspace,
page: 'discover',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('filter for value spec', () => {
// Create workspace
cy.deleteWorkspaceByName(workspace);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);
cy.wait(2000);
cy.createWorkspaceIndexPatterns({
workspaceName: workspace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('query enhancement queries', { scrollBehavior: false }, () => {
});

// Create workspace and set up index pattern
cy.deleteWorkspaceByName(`${workspace}`);
cy.deleteWorkspaceByName(workspace);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
cy.osd.createInitialWorkspaceWithDataSource(DATASOURCE_NAME, workspace);

// Create and select index pattern for data_logs_small_time_1*
cy.createWorkspaceIndexPatterns({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const definedS3Variables = !S3_CLUSTER.url;
// Create workspace
cy.deleteWorkspaceByName(workspace);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(S3_CLUSTER.name, workspace);
cy.osd.createInitialWorkspaceWithDataSource(S3_CLUSTER.name, WORKSPACE_NAME);
});
afterEach(() => {
cy.deleteWorkspaceByName(workspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const runSavedSearchTests = () => {
// Create workspace
cy.deleteWorkspaceByName(workspaceName);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(datasourceName, workspaceName);
cy.osd.createInitialWorkspaceWithDataSource(datasourceName, workspaceName);
cy.createWorkspaceIndexPatterns({
workspaceName: workspaceName,
indexPattern: INDEX_PATTERN_WITH_TIME.replace('*', ''),
Expand Down
2 changes: 1 addition & 1 deletion cypress/utils/apps/workspace/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Cypress.Commands.add(
(workspaceName) => {
// Selecting the correct workspace
cy.visit('/app/workspace_list#');
cy.openWorkspaceDashboard(workspaceName);
cy.osd.openWorkspaceDashboard(workspaceName);
// wait until page loads
cy.getElementByTestId('headerAppActionMenu').should('be.visible');
}
Expand Down
43 changes: 43 additions & 0 deletions cypress/utils/command_namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

/**
* Initializes a command namespace on the cy object
*
* Ex usage:
* initCommandNamespace(cy, 'osd'); // initializes osd namespace (only needed once per ns)
* cy.osd.add('myNewCommand', (myArg) => ...); // register command to namespace
* cy.osd.myNewCommand('someArg'); // executes command
*
*/
export default function initCommandNamespace(cy, namespace) {
/**
* this proxy is responsible for intercepting access to properties
* it's what allows us to dynamically define properties at runtime like cy.osd.myNewCommand
*/
cy[namespace] = new Proxy(
{
// register a new namespaced command with cypress (ex. osd:myNewCommand)
add(commandName, callback) {
Cypress.Commands.add(namespaceCommand(commandName), callback);
},
},
{
get(target, property) {
if (target[property]) {
// return reserved property (add method)
return target[property];
}

// return the mapped namespace command (ex. myNewCommand returns the osd:myNewCommand command)
return cy[namespaceCommand(property)];
},
}
);

function namespaceCommand(commandName) {
return `${namespace}:${commandName}`;
}
}
8 changes: 5 additions & 3 deletions cypress/utils/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import { BASE_PATH } from './constants';
import { TestFixtureHandler } from '../lib/test_fixture_handler';
import initCommandNamespace from './command_namespace';

initCommandNamespace(cy, 'osd');

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

Cypress.Commands.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, workspaceName) => {
cy.osd.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, workspaceName) => {
cy.intercept('POST', '/api/workspaces').as('createWorkspaceInterception');

cy.getElementByTestId('workspace-initial-card-createWorkspace-button')
.should('be.visible')
.click();
Expand All @@ -351,7 +353,7 @@ Cypress.Commands.add('createInitialWorkspaceWithDataSource', (dataSourceTitle, w
cy.contains(/successfully/);
});

Cypress.Commands.add('openWorkspaceDashboard', (workspaceName) => {
cy.osd.add('openWorkspaceDashboard', (workspaceName) => {
cy.getElementByTestId('workspace-select-button').should('exist').click();
cy.getElementByTestId('workspace-menu-manage-button').should('exist').click();
cy.get('.euiBasicTable')
Expand Down
2 changes: 1 addition & 1 deletion cypress/utils/dashboards/remove_workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function removeSampleDataAndWorkspace(url, workspaceName) {
describe('removing workspace/sampledata', () => {
it('remove workspace', () => {
cy.visit(`${url}/app/workspace_list`);
cy.openWorkspaceDashboard(workspaceName);
cy.osd.openWorkspaceDashboard(workspaceName);
cy.getElementByTestId('toggleNavButton').eq(0).should('exist').click();
cy.wait(3000);
cy.getElementByTestId('collapsibleNavAppLink-workspace_detail').should('exist').click();
Expand Down
36 changes: 19 additions & 17 deletions cypress/utils/dashboards/setup_workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
const dataSourceTitle = Cypress.env('dataSourceTitle');

export function createWorkspaceAndSampleData(url, workspaceName) {
describe('checking home page', () => {
it('checking workspace initial page', () => {
cy.visit(`${url}/app/home`);
cy.getElementByTestId('workspace-initial-card-createWorkspace-button').should('be.visible');
describe('setup workspace', () => {
describe('checking home page', () => {
it('checking workspace initial page', () => {
cy.visit(`${url}/app/home`);
cy.getElementByTestId('workspace-initial-card-createWorkspace-button').should('be.visible');
});
});
});

describe('creating workspace', () => {
it('creating workspace with data source', () => {
cy.visit(`${url}/app/home`);
cy.createInitialWorkspaceWithDataSource(dataSourceTitle, workspaceName);
cy.wait(2000);
describe('creating workspace', () => {
it('creating workspace with data source', () => {
cy.visit(`${url}/app/home`);
cy.osd.createInitialWorkspaceWithDataSource(dataSourceTitle, workspaceName);
cy.wait(2000);
});
});
});

describe('adding sample data to workspace', () => {
it('add sample data to data source', () => {
cy.visit(`${url}/app/workspace_list`);
cy.openWorkspaceDashboard(workspaceName);
cy.openSampleDataPage();
cy.addSampleDataToDataSource(dataSourceTitle);
describe('adding sample data to workspace', () => {
it('add sample data to data source', () => {
cy.visit(`${url}/app/workspace_list`);
cy.osd.openWorkspaceDashboard(workspaceName);
cy.openSampleDataPage();
cy.addSampleDataToDataSource(dataSourceTitle);
});
});
});
}
24 changes: 16 additions & 8 deletions cypress/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,21 @@ declare namespace Cypress {
*/
drag<S = any>(targetSelector: string): Chainable<S>;

/**
* Creates workspace and attaches it to the provided data source
* It also saves the created workspace id as the alias @WORKSPACE_ID
*/
createInitialWorkspaceWithDataSource<S = any>(
dataSourceTitle: string,
workspaceName: string
): Chainable<S>;
// osd namespace
osd: {
/**
* Creates workspace and attaches it to the provided data source
* It also saves the created workspace id as the alias @WORKSPACE_ID
*/
createInitialWorkspaceWithDataSource<S = any>(
dataSourceTitle: string,
workspaceName: string
): Chainable<S>;

/**
* Opens workspace dashboard
*/
openWorkspaceDashboard<S = any>(workspaceName: string): Chainable<S>;
};
}
}

0 comments on commit 78956d0

Please sign in to comment.