login.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. login(username?: string, password?: string): Chainable<Element>;
  5. logout(): Chainable<Element>;
  6. }
  7. }
  8. interface Window {
  9. SERVER_FLAGS?: {
  10. authDisabled?: boolean;
  11. };
  12. }
  13. }
  14. export const KUBEADMIN_USERNAME = 'kubeadmin';
  15. // This will add 'cy.login(...)'
  16. // ex: cy.login('my-user', 'my-password')
  17. Cypress.Commands.add(
  18. 'login',
  19. (
  20. username: string = KUBEADMIN_USERNAME,
  21. password: string = Cypress.env('BRIDGE_KUBEADMIN_PASSWORD'),
  22. ) => {
  23. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  24. const baseURL = Cypress.config('baseUrl')!;
  25. // Make sure we clear the cookie in case a previous test failed to logout.
  26. cy.clearCookie('openshift-session-token');
  27. cy.visit(baseURL);
  28. cy.session(
  29. username,
  30. () => {
  31. // Check if auth is disabled (for a local development environment).
  32. cy.window().then((win) => {
  33. if (win.SERVER_FLAGS?.authDisabled) {
  34. return;
  35. }
  36. });
  37. cy.visit(baseURL);
  38. cy.get('[data-test-id="login"]').should('be.visible');
  39. cy.get('#inputUsername').type(username);
  40. cy.get('#inputPassword').type(password);
  41. cy.get('button[type=submit]').click();
  42. },
  43. {
  44. cacheAcrossSpecs: true,
  45. validate() {
  46. cy.visit(baseURL);
  47. cy.get('[data-test="username"]').should('exist');
  48. },
  49. },
  50. );
  51. },
  52. );
  53. Cypress.Commands.add('logout', () => {
  54. // Check if auth is disabled (for a local development environment).
  55. cy.window().then((win) => {
  56. if (win.SERVER_FLAGS?.authDisabled) {
  57. return;
  58. }
  59. cy.get('[data-test="username"]').click();
  60. cy.get('[data-test="log-out"]').should('be.visible');
  61. cy.get('[data-test="log-out"]').click({ force: true });
  62. });
  63. });