Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 76 additions & 30 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* eslint-disable @typescript-eslint/no-namespace */
/// <reference types="cypress" />
import { KUBEADMIN_IDP, KUBEADMIN_USERNAME, SELECTORS } from './constants';
import { KUBEADMIN_IDP, KUBEADMIN_USERNAME } from './constants';
import { ConsoleWindowType } from './types';

import Loggable = Cypress.Loggable;
import Timeoutable = Cypress.Timeoutable;
import Withinable = Cypress.Withinable;
import Shadow = Cypress.Shadow;

const MINUTE = 60 * 1000;
const TOUR_DISMISS = '[data-test="tour-step-footer-secondary"]';
const TOUR_WAIT_MS = 20_000;
const TOUR_POLL_MS = 500;

declare global {
namespace Cypress {
interface Chainable {
Expand All @@ -23,13 +28,39 @@ declare global {
}
}

Cypress.Commands.add('login', (provider, username, password) => {
// Check if auth is disabled (for a local development environment).
/**
* The guided tour modal can mount asynchronously after the page is ready.
* Poll briefly and dismiss it if present; never fail when absent.
*/
const dismissWelcomeTourIfPresent = () => {
const deadline = Date.now() + TOUR_WAIT_MS;

const tryDismiss = () => {
cy.get('body').then(($body) => {
if ($body.find(TOUR_DISMISS).length > 0) {
cy.get(TOUR_DISMISS).click({ force: true });
cy.get('.pf-v6-l-bullseye', { timeout: 15_000 }).should('not.exist');
return;
}

if (Date.now() >= deadline) {
return;
}

cy.visit(''); // visits baseUrl which is set in plugins.js
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(TOUR_POLL_MS).then(tryDismiss);
});
};

tryDismiss();
};

Cypress.Commands.add('login', (provider, username, password) => {
cy.visit('');
cy.window().then((win: ConsoleWindowType) => {
if (win.SERVER_FLAGS?.authDisabled) {
cy.log('skipping login, console is running with auth disabled');
dismissWelcomeTourIfPresent();

cy.contains('li[data-test="nav"]', 'Networking').click();
cy.contains(
Expand All @@ -42,42 +73,57 @@ Cypress.Commands.add('login', (provider, username, password) => {
cy.clearCookie('openshift-session-token');

const idp = provider || KUBEADMIN_IDP;

cy.get('main form').should('be.visible');

cy.get('body').then(($body) => {
if ($body.text().includes(idp)) {
cy.contains(idp).should('be.visible').click();
}
});

cy.get(SELECTORS.usernameInput).type(username || KUBEADMIN_USERNAME);
cy.get(SELECTORS.passwordInput).type(password || Cypress.env('KUBEADMIN_PASSWORD'));
cy.get(SELECTORS.submitButton).click();

cy.wait(20000);

// Close tour popup if present
cy.get('body').then(($body) => {
if ($body.find(SELECTORS.tourPopup).length) {
cy.get(SELECTORS.tourPopup).click();
}
});
const loginUsername = username || KUBEADMIN_USERNAME;
const loginPassword = password || Cypress.env('KUBEADMIN_PASSWORD');

cy.origin(
Cypress.config('baseUrl').replace('console-openshift-console', 'oauth-openshift'),
{ args: { idp, loginUsername, loginPassword } },
({ idp: originIdp, loginUsername: originUsr, loginPassword: originPwd }) => {
cy.get('body', { timeout: 3 * 60_000 }).should('be.visible');
cy.get('body').then(($body) => {
if ($body.find('#inputUsername').length === 0) {
if ($body.text().includes(originIdp)) {
cy.contains('a', originIdp).click();
} else if ($body.text().includes('kubeadmin')) {
cy.contains('a', 'kubeadmin').click();
} else {
cy.get('a').first().click();
}
}
});
cy.get('#inputUsername', { timeout: 3 * 60_000 }).should('be.visible');
cy.get('#inputUsername').type(originUsr);
cy.get('#inputPassword').type(originPwd, { log: false });
cy.get('button[type=submit]').click();
},
);

cy.url({ timeout: 2 * MINUTE }).should('include', 'console-openshift-console');
cy.get('[data-test="user-dropdown"], [data-test="user-dropdown-toggle"], #page-sidebar', {
timeout: MINUTE,
}).should('exist');
dismissWelcomeTourIfPresent();
});
});

Cypress.Commands.add('logout', () => {
// Check if auth is disabled (for a local development environment).
cy.window().then((win: ConsoleWindowType) => {
if (win.SERVER_FLAGS?.authDisabled) {
cy.log('skipping logout, console is running with auth disabled');
return;
}
cy.log('Logging out');
cy.byTestID('user-dropdown').click();
cy.byTestID('log-out').should('be.visible');
cy.byTestID('log-out').click({ force: true });
cy.byLegacyTestID('login').should('be.visible');
cy.visit('/');
cy.get('body').then(($body) => {
if ($body.find('[data-test="user-dropdown"]').length === 0) {
cy.log('logout skipped: user menu not present');
return;
}
cy.get('[data-test="user-dropdown"]').click();
cy.get('[data-test="log-out"]').should('be.visible');
cy.get('[data-test="log-out"]').click({ force: true });
});
});
});

Expand Down
Loading
Loading