Skip to content

Commit 7fddb83

Browse files
committed
Create custom commands in Cypress
Two functions are moved from the 'helpers' module to Cypress 'commands'.
1 parent 4ba6b47 commit 7fddb83

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

tests/cypress/e2e/helpers.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ export const inputServiceSelector = 'input[id="input_Service"]';
1111
export const inputTemplateSelector = 'input[id="input_Predefined graph"]';
1212
export const panelContentSelector = '[class="panel-content"]';
1313

14-
export function loginGrafana(grafanaUsername: string, passwordGrafana: string) {
15-
cy.visit('/login');
16-
cy.get('input[name="user"]').type(grafanaUsername);
17-
cy.get('input[name="password"]').type(passwordGrafana);
18-
cy.get('[aria-label="Login button"]').click();
19-
}
20-
21-
export function addNewPanel() {
22-
// add a new panel in a new dashboard
23-
cy.visit('/dashboard/new');
24-
cy.get('button[aria-label="Add new panel"]').click();
25-
}
26-
2714
export function addCmkDatasource(cmkUsername: string, cmkPassword: string) {
2815
cy.visit('/datasources/new');
2916
cy.get('button[aria-label="Add data source Checkmk"]').contains('Checkmk').click();

tests/cypress/e2e/spec.cy.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../support/commands';
12
import {
23
activateCmkChanges,
34
createCmkAutomationUser,
@@ -8,7 +9,6 @@ import {
89
} from './api_helpers';
910
import {
1011
addCmkDatasource,
11-
addNewPanel,
1212
inputFilterSelector,
1313
inputGraphTypeSelector,
1414
inputHostLabelsSelector,
@@ -18,7 +18,6 @@ import {
1818
inputServiceRegexSelector,
1919
inputServiceSelector,
2020
inputTemplateSelector,
21-
loginGrafana,
2221
panelContentSelector,
2322
rmCmkDatasource,
2423
saveDashboard,
@@ -42,13 +41,13 @@ describe('e2e tests', () => {
4241
executeServiceDiscovery(hostName1, 'new');
4342
activateCmkChanges('cmk');
4443

45-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
44+
cy.loginGrafana();
4645
addCmkDatasource(cmkUser, cmkPassword);
4746
});
4847

4948
it('time-usage panel by service (single host)', {}, () => {
50-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
51-
addNewPanel();
49+
cy.loginGrafana();
50+
cy.addNewPanel();
5251

5352
cy.get(inputFilterSelector).type('Hostname{enter}'); // Filter -> 'Host name'
5453
cy.get(inputFilterSelector).type('Service{enter}'); // Filter -> 'Service'
@@ -69,8 +68,8 @@ describe('e2e tests', () => {
6968
});
7069

7170
it('time-usage panel by service (multiple hosts)', {}, () => {
72-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
73-
addNewPanel();
71+
cy.loginGrafana();
72+
cy.addNewPanel();
7473

7574
cy.get(inputFilterSelector).type('Service{enter}'); // Filter -> 'Service'
7675

@@ -92,8 +91,8 @@ describe('e2e tests', () => {
9291
});
9392

9493
it('RAM-used panel by service regex (multiple hosts)', {}, () => {
95-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
96-
addNewPanel();
94+
cy.loginGrafana();
95+
cy.addNewPanel();
9796

9897
cy.get(inputFilterSelector).type('Service regex{enter}'); // Filter -> 'Service'
9998
cy.contains('Service regex').should('exist');
@@ -113,8 +112,8 @@ describe('e2e tests', () => {
113112
});
114113

115114
it('RAM-used panel by host labels (multiple hosts, single metric)', {}, () => {
116-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
117-
addNewPanel();
115+
cy.loginGrafana();
116+
cy.addNewPanel();
118117

119118
cy.get(inputFilterSelector).type('Host labels{enter}'); // Filter -> 'Host labels'
120119
cy.contains('Host labels').should('exist');
@@ -140,8 +139,8 @@ describe('e2e tests', () => {
140139
});
141140

142141
it('RAM-used panel by service regex and hostname regex', {}, () => {
143-
loginGrafana(Cypress.env('grafanaUsername'), Cypress.env('grafanaPassword'));
144-
addNewPanel();
142+
cy.loginGrafana();
143+
cy.addNewPanel();
145144

146145
cy.get(inputFilterSelector).type('Service regex{enter}'); // Filter -> 'Service'
147146
cy.contains('Service regex').should('exist');

tests/cypress/support/commands.ts

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1-
/// <reference types="cypress" />
2-
// ***********************************************
3-
// This example commands.ts shows you how to
4-
// create various custom commands and overwrite
5-
// existing commands.
6-
//
7-
// For more comprehensive examples of custom
8-
// commands please read more here:
9-
// https://on.cypress.io/custom-commands
10-
// ***********************************************
11-
//
12-
//
13-
// -- This is a parent command --
14-
// Cypress.Commands.add('login', (email, password) => { ... })
15-
//
16-
//
17-
// -- This is a child command --
18-
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19-
//
20-
//
21-
// -- This is a dual command --
22-
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23-
//
24-
//
25-
// -- This will overwrite an existing command --
26-
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27-
//
28-
// declare global {
29-
// namespace Cypress {
30-
// interface Chainable {
31-
// login(email: string, password: string): Chainable<void>
32-
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33-
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34-
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35-
// }
36-
// }
37-
// }
1+
export {};
2+
3+
Cypress.Commands.add('loginGrafana', () => {
4+
cy.visit('/login');
5+
cy.get('input[name="user"]').type(Cypress.env('grafanaUsername'));
6+
cy.get('input[name="password"]').type(Cypress.env('grafanaPassword'));
7+
cy.get('[aria-label="Login button"]').click();
8+
});
9+
10+
Cypress.Commands.add('addNewPanel', () => {
11+
// add a new panel in a new dashboard
12+
cy.visit('/dashboard/new');
13+
cy.get('button[aria-label="Add new panel"]').click();
14+
});
15+
16+
declare global {
17+
namespace Cypress {
18+
interface Chainable {
19+
loginGrafana(): Chainable<void>;
20+
addNewPanel(): Chainable<void>;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)