dashboard/cypress/support/commands.ts

97 lines
2.5 KiB
TypeScript

import { LoginPagePo } from '@/cypress/e2e/po/pages/login-page.po';
import { Matcher } from '~/cypress/support/types';
/**
* Login local authentication, including first login and bootstrap if not cached
*/
Cypress.Commands.add('login', (
username = Cypress.env('username'),
password = Cypress.env('password'),
cacheSession = true,
) => {
const login = () => {
cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq');
LoginPagePo.goTo(); // Needs to happen before the page element is created/located
const loginPage = new LoginPagePo();
loginPage
.checkIsCurrentPage();
loginPage.switchToLocal();
loginPage.canSubmit()
.should('eq', true);
loginPage.username()
.set(username);
loginPage.password()
.set(password);
loginPage.canSubmit()
.should('eq', true);
loginPage.submit();
cy.wait('@loginReq');
};
if (cacheSession) {
(cy as any).session([username, password], login);
} else {
login();
}
});
/**
* Get input field for given label
*/
Cypress.Commands.add('byLabel', (label) => {
return cy.get('.labeled-input').contains(label).siblings('input');
});
/**
* Wrap the cy.find() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('findId', (id: string, matcher?: Matcher = '') => {
return cy.find(`[data-testid${ matcher }="${ id }"]`);
});
/**
* Wrap the cy.get() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('getId', (id: string, matcher?: Matcher = '') => {
return cy.get(`[data-testid${ matcher }="${ id }"]`);
});
/**
* Override user preferences to default values, allowing to pass custom preferences for a deterministic scenario
*/
// eslint-disable-next-line no-undef
Cypress.Commands.add('userPreferences', (preferences: Partial<UserPreferences> = {}) => {
return cy.intercept('/v1/userpreferences', (req) => {
req.reply({
statusCode: 201,
body: {
data: [{
type: 'userpreference',
data: {
'after-login-route': '\"home\"',
cluster: 'local',
'group-by': 'none',
'home-page-cards': '',
'last-namespace': 'default',
'last-visited': '',
'ns-by-cluster': '',
provisioner: '',
'read-whatsnew': '',
'seen-whatsnew': '2.x.x',
theme: '',
...preferences,
},
}]
},
});
});
});