login.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. login(username?: string, password?: string): Chainable<Element>;
  5. logout(): Chainable<Element>;
  6. }
  7. }
  8. }
  9. const KUBEADMIN_USERNAME = 'kubeadmin';
  10. const loginUsername = Cypress.env('BRIDGE_KUBEADMIN_PASSWORD') ? 'user-dropdown' : 'username';
  11. // This will add 'cy.login(...)'
  12. // ex: cy.login('my-user', 'my-password')
  13. Cypress.Commands.add('login', (username: string, password: string) => {
  14. // Check if auth is disabled (for a local development environment).
  15. cy.visit('/'); // visits baseUrl which is set in plugins/index.js
  16. cy.window().then((win) => {
  17. if (win.SERVER_FLAGS?.authDisabled) {
  18. return;
  19. }
  20. // Make sure we clear the cookie in case a previous test failed to logout.
  21. cy.clearCookie('openshift-session-token');
  22. cy.get('#inputUsername').type(username || KUBEADMIN_USERNAME);
  23. cy.get('#inputPassword').type(password || Cypress.env('BRIDGE_KUBEADMIN_PASSWORD'));
  24. cy.get('button[type=submit]').click();
  25. cy.get(`[data-test="${loginUsername}"]`).should('be.visible');
  26. });
  27. });
  28. Cypress.Commands.add('logout', () => {
  29. // Check if auth is disabled (for a local development environment).
  30. cy.window().then((win) => {
  31. if (win.SERVER_FLAGS?.authDisabled) {
  32. return;
  33. }
  34. cy.get('[data-test="user-dropdown"]').click();
  35. cy.get('[data-test="log-out"]').should('be.visible');
  36. cy.get('[data-test="log-out"]').click({ force: true });
  37. });
  38. });