login.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { Locator, Page } from '@playwright/test';
  2. declare global {
  3. interface Window {
  4. SERVER_FLAGS?: {
  5. authDisabled?: boolean;
  6. };
  7. }
  8. }
  9. export const KUBEADMIN_USERNAME = 'kubeadmin';
  10. export class LoginPage {
  11. constructor(private readonly page: Page) {}
  12. private async isAuthDisabled() {
  13. return this.page.evaluate(() => window.SERVER_FLAGS?.authDisabled);
  14. }
  15. // Fill a field via CDP to avoid exposing the value in Playwright traces
  16. // https://github.com/microsoft/playwright/issues/19992#issuecomment-4078945450
  17. private async fillSensitive(locator: Locator, text: string) {
  18. await locator.focus();
  19. const cdpSession = await this.page.context().newCDPSession(this.page);
  20. await cdpSession.send('Input.dispatchKeyEvent', {
  21. type: 'keyDown',
  22. key: 'a',
  23. commands: ['selectAll'],
  24. });
  25. await cdpSession.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'a' });
  26. await cdpSession.send('Input.insertText', { text });
  27. await cdpSession.detach();
  28. }
  29. async login(
  30. username: string = KUBEADMIN_USERNAME,
  31. password: string = process.env.BRIDGE_KUBEADMIN_PASSWORD ?? '',
  32. ) {
  33. await this.page.context().clearCookies();
  34. await this.page.goto('/');
  35. if (await this.isAuthDisabled()) {
  36. return;
  37. }
  38. await this.page.locator('[data-test-id="login"]').waitFor({ state: 'visible' });
  39. await this.page.locator('#inputUsername').fill(username);
  40. await this.fillSensitive(this.page.locator('#inputPassword'), password);
  41. await this.page.locator('button[type=submit]').click();
  42. await this.page.getByTestId('username').waitFor({ state: 'attached' });
  43. }
  44. async logout() {
  45. if (await this.isAuthDisabled()) {
  46. return;
  47. }
  48. await this.page.getByTestId('username').click();
  49. await this.page.getByTestId('log-out').waitFor({ state: 'visible' });
  50. // eslint-disable-next-line playwright/no-force-option -- dropdown may be covered by overlay
  51. await this.page.getByTestId('log-out').click({ force: true });
  52. }
  53. }