Skip to content

Commit 9285f8c

Browse files
committed
Move all 'helpers' functions to Cypress 'commands'
1 parent 7fddb83 commit 9285f8c

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

tests/cypress/e2e/helpers.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,3 @@ export const inputServiceRegexSelector = 'input[data-test-id="service_regex-filt
1010
export const inputServiceSelector = 'input[id="input_Service"]';
1111
export const inputTemplateSelector = 'input[id="input_Predefined graph"]';
1212
export const panelContentSelector = '[class="panel-content"]';
13-
14-
export function addCmkDatasource(cmkUsername: string, cmkPassword: string) {
15-
cy.visit('/datasources/new');
16-
cy.get('button[aria-label="Add data source Checkmk"]').contains('Checkmk').click();
17-
18-
cy.get('[data-test-id="checkmk-url"]').type(Cypress.env('grafanaToCheckmkUrl'));
19-
cy.get('[data-test-id="checkmk-username"]').type(cmkUsername);
20-
cy.get('[data-test-id="checkmk-password"]').type(cmkPassword);
21-
cy.get('[id="checkmk-version"]').type('<{enter}');
22-
23-
cy.get('[aria-label="Data source settings page Save and Test button"]').click();
24-
25-
cy.get('[data-testid="data-testid Alert success"]').should('be.visible');
26-
cy.contains('Data source is working').should('be.visible');
27-
}
28-
29-
export function rmCmkDatasource() {
30-
// Remove the previously-created datasource as teardown process.
31-
// This makes sure the tests use the newly generated datasource in each execution.
32-
// Important especially when running the tests locally if docker images are up during multiple tests' executions.
33-
34-
cy.visit('/datasources/');
35-
36-
cy.get('[class="page-container page-body"]').contains('Checkmk').click();
37-
cy.contains('Delete').click();
38-
cy.get('button[aria-label="Confirm Modal Danger Button"]').click();
39-
40-
cy.contains('No data sources defined').should('be.visible');
41-
}
42-
43-
export function saveDashboard(dashboardID: string) {
44-
cy.get('button[title="Apply changes and save dashboard"]').contains('Save').click();
45-
cy.get('input[aria-label="Save dashboard title field"]').type(' ' + dashboardID);
46-
47-
cy.get('button[aria-label="Save dashboard button"]').click();
48-
cy.contains('Dashboard saved').should('be.visible');
49-
}

tests/cypress/e2e/spec.cy.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
executeServiceDiscovery,
99
} from './api_helpers';
1010
import {
11-
addCmkDatasource,
1211
inputFilterSelector,
1312
inputGraphTypeSelector,
1413
inputHostLabelsSelector,
@@ -19,8 +18,6 @@ import {
1918
inputServiceSelector,
2019
inputTemplateSelector,
2120
panelContentSelector,
22-
rmCmkDatasource,
23-
saveDashboard,
2421
} from './helpers';
2522

2623
describe('e2e tests', () => {
@@ -42,7 +39,7 @@ describe('e2e tests', () => {
4239
activateCmkChanges('cmk');
4340

4441
cy.loginGrafana();
45-
addCmkDatasource(cmkUser, cmkPassword);
42+
cy.addCmkDatasource(cmkUser, cmkPassword);
4643
});
4744

4845
it('time-usage panel by service (single host)', {}, () => {
@@ -63,8 +60,7 @@ describe('e2e tests', () => {
6360

6461
cy.get(panelContentSelector).contains('CPU time in user space').should('be.visible');
6562

66-
const randInt = Math.floor(Math.random() * 1000);
67-
saveDashboard(randInt.toString());
63+
cy.saveDashboard();
6864
});
6965

7066
it('time-usage panel by service (multiple hosts)', {}, () => {
@@ -86,8 +82,7 @@ describe('e2e tests', () => {
8682
.contains('CPU time in user space, ' + hostName1)
8783
.should('be.visible');
8884

89-
const randInt = Math.floor(Math.random() * 1000);
90-
saveDashboard(randInt.toString());
85+
cy.saveDashboard();
9186
});
9287

9388
it('RAM-used panel by service regex (multiple hosts)', {}, () => {
@@ -107,8 +102,7 @@ describe('e2e tests', () => {
107102
cy.get(panelContentSelector).contains(hostName0).should('be.visible');
108103
cy.get(panelContentSelector).contains(hostName1).should('be.visible');
109104

110-
const randInt = Math.floor(Math.random() * 1000);
111-
saveDashboard(randInt.toString());
105+
cy.saveDashboard();
112106
});
113107

114108
it('RAM-used panel by host labels (multiple hosts, single metric)', {}, () => {
@@ -134,8 +128,7 @@ describe('e2e tests', () => {
134128
cy.get(panelContentSelector).contains(hostName0).should('be.visible');
135129
cy.get(panelContentSelector).contains(hostName1).should('be.visible');
136130

137-
const randInt = Math.floor(Math.random() * 1000);
138-
saveDashboard(randInt.toString());
131+
cy.saveDashboard();
139132
});
140133

141134
it('RAM-used panel by service regex and hostname regex', {}, () => {
@@ -175,7 +168,7 @@ describe('e2e tests', () => {
175168
});
176169

177170
after(function () {
178-
rmCmkDatasource();
171+
cy.rmCmkDatasource();
179172

180173
deleteCmkHost(hostName0);
181174
deleteCmkHost(hostName1);

tests/cypress/support/commands.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,55 @@ Cypress.Commands.add('addNewPanel', () => {
1313
cy.get('button[aria-label="Add new panel"]').click();
1414
});
1515

16+
Cypress.Commands.add('addCmkDatasource', (cmkUser: string, cmkPass: string) => {
17+
cy.visit('/datasources/new');
18+
cy.get('button[aria-label="Add data source Checkmk"]').contains('Checkmk').click();
19+
20+
cy.get('[data-test-id="checkmk-url"]').type(Cypress.env('grafanaToCheckmkUrl'));
21+
cy.get('[data-test-id="checkmk-username"]').type(cmkUser);
22+
cy.get('[data-test-id="checkmk-password"]').type(cmkPass);
23+
cy.get('[id="checkmk-version"]').type('<{enter}');
24+
25+
cy.get('[aria-label="Data source settings page Save and Test button"]').click();
26+
27+
cy.get('[data-testid="data-testid Alert success"]').should('be.visible');
28+
cy.contains('Data source is working').should('be.visible');
29+
});
30+
31+
Cypress.Commands.add('rmCmkDatasource', () => {
32+
// Remove the previously-created datasource as teardown process.
33+
// This makes sure the tests use the newly generated datasource in each execution.
34+
// Important especially when running the tests locally if docker images are up during multiple tests' executions.
35+
36+
cy.visit('/datasources/');
37+
38+
cy.get('[class="page-container page-body"]').contains('Checkmk').click();
39+
cy.contains('Delete').click();
40+
cy.get('button[aria-label="Confirm Modal Danger Button"]').click();
41+
42+
cy.contains('No data sources defined').should('be.visible');
43+
});
44+
45+
Cypress.Commands.add('saveDashboard', () => {
46+
const randInt = Math.floor(Math.random() * 1000).toString();
47+
48+
cy.get('button[title="Apply changes and save dashboard"]').contains('Save').click();
49+
cy.get('input[aria-label="Save dashboard title field"]').type(' ' + randInt);
50+
51+
cy.get('button[aria-label="Save dashboard button"]').click();
52+
cy.contains('Dashboard saved').should('be.visible');
53+
54+
return cy.wrap(randInt);
55+
});
56+
1657
declare global {
1758
namespace Cypress {
1859
interface Chainable {
1960
loginGrafana(): Chainable<void>;
2061
addNewPanel(): Chainable<void>;
62+
addCmkDatasource(cmkUser: string, cmkPass: string): Chainable<void>;
63+
rmCmkDatasource(): Chainable<void>;
64+
saveDashboard(): Chainable<string>;
2165
}
2266
}
2367
}

0 commit comments

Comments
 (0)