Use the api to login in cypress by default

This commit is contained in:
Ashley Harrison 2021-12-15 15:42:23 +00:00
parent 50c6c7a528
commit 5a0dd3cca0
No known key found for this signature in database
GPG Key ID: AEC29E54E8D7CD90
3 changed files with 29 additions and 14 deletions

View File

@ -6,6 +6,7 @@ export const smokeTestScenario = {
addScenarioDataSource: true,
addScenarioDashBoard: true,
skipScenario: false,
loginViaApi: false,
scenario: () => {
e2e.flows.openDashboard();
e2e.components.PageToolbar.item('Add panel').click();

View File

@ -1,22 +1,34 @@
import { e2e } from '../index';
import { fromBaseUrl } from '../support/url';
const DEFAULT_USERNAME = 'admin';
const DEFAULT_PASSWORD = 'admin';
export const login = (username = DEFAULT_USERNAME, password = DEFAULT_PASSWORD) => {
e2e().logToConsole('Logging in with username:', username);
e2e.pages.Login.visit();
e2e.pages.Login.username()
.should('be.visible') // prevents flakiness
.type(username);
e2e.pages.Login.password().type(password);
e2e.pages.Login.submit().click();
export const login = (username = DEFAULT_USERNAME, password = DEFAULT_PASSWORD, loginViaApi = true) => {
if (loginViaApi) {
cy.request({
method: 'POST',
url: fromBaseUrl('/login'),
body: {
user: username,
password,
},
});
} else {
e2e().logToConsole('Logging in with username:', username);
e2e.pages.Login.visit();
e2e.pages.Login.username()
.should('be.visible') // prevents flakiness
.type(username);
e2e.pages.Login.password().type(password);
e2e.pages.Login.submit().click();
// Local tests will have insecure credentials
if (password === DEFAULT_PASSWORD) {
e2e.pages.Login.skip().should('be.visible').click();
// Local tests will have insecure credentials
if (password === DEFAULT_PASSWORD) {
e2e.pages.Login.skip().should('be.visible').click();
}
e2e().get('.login-page').should('not.exist');
}
e2e().get('.login-page').should('not.exist');
e2e().logToConsole('Logged in with username:', username);
};

View File

@ -7,6 +7,7 @@ export interface ScenarioArguments {
skipScenario?: boolean;
addScenarioDataSource?: boolean;
addScenarioDashBoard?: boolean;
loginViaApi?: boolean;
}
export const e2eScenario = ({
@ -16,12 +17,13 @@ export const e2eScenario = ({
skipScenario = false,
addScenarioDataSource = false,
addScenarioDashBoard = false,
loginViaApi = true,
}: ScenarioArguments) => {
describe(describeName, () => {
if (skipScenario) {
it.skip(itName, () => scenario());
} else {
before(() => e2e.flows.login(e2e.env('USERNAME'), e2e.env('PASSWORD')));
before(() => e2e.flows.login(e2e.env('USERNAME'), e2e.env('PASSWORD'), loginViaApi));
beforeEach(() => {
Cypress.Cookies.preserveOnce('grafana_session');