login.ts 1.3 KB

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