Skip to content

Refactor dashboard and query analytics tests for improved readability#852

Open
travagliad wants to merge 3 commits intomainfrom
PMM-7-open-panels-on-test-files
Open

Refactor dashboard and query analytics tests for improved readability#852
travagliad wants to merge 3 commits intomainfrom
PMM-7-open-panels-on-test-files

Conversation

@travagliad
Copy link
Contributor

Refactor the Grafana helper functions for better modularity and reusability. Suppress unnecessary tours during authorization and improve route matching in tests. Update tests to utilize the new helper methods for opening dashboards and verifying metrics.

…rafana.helper; robust route match + JSON headers; keep upgrade modal mock
Comment on lines 27 to 53
export async function suppressTour(page: Page): Promise<void> {
await page.route('**/v1/users/me**', async (route) => {
await route.fulfill({
status: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
user_id: 1,
product_tour_completed: true,
alerting_tour_completed: true,
snoozed_pmm_version: '3.2.0',
}),
});
});
}

static getToken(username = 'admin', password = process.env.ADMIN_PASSWORD || 'admin') {
return Buffer.from(`${username}:${password}`).toString('base64');
}
export async function suppressUpgradeNotification(page: Page): Promise<void> {
await page.route('**/v1/server/updates?force=**', (route) =>
route.fulfill({
status: 200,
body: JSON.stringify({
installed: {},
last_check: new Date().toISOString(),
latest: {},
update_available: false,
}),
}),
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to move the suppress to grafana.helper.ts because it looks to me it serves the only purpose of navigating the UI and I thought it was the most appropriate file to be inserted too, please validate.

Comment on lines +79 to +83
async open(dashboardUrl: string, params: BuildUrlParameters) {
await super.open(dashboardUrl, params);
await this.loadAllPanels();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page (Dashboard), requires "loadAllPanels" after opening it, while QAN do not, it has it's own wait that I haven't touched, so I did this method overload to cover this case specifically (and not moving loadAllPanels to base.page.ts

If we decide to use loadAllPanels across all pages in PMM, I think we should move it to base.page.ts for less repetition so we won't need to overload open in every page we create.

@@ -1,24 +1,54 @@
import { Page } from '@playwright/test';

export default class GrafanaHelper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing class? It will get messy as previous JS tests.

Copy link
Contributor Author

@travagliad travagliad Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result of not having class:

import { authorize, suppressTour, suppressUpgradeNotification } from '@helpers/grafana.helper';

...

  loginUI: async ({ page }, use) => {
    await suppressTour(page);
    await suppressUpgradeNotification(page);
    await authorize(page);
    await use();
  },

  api: async ({ page, request }, use) => {
    const inventoryApi = new Api(page, request);
    await authorize(page);
    await use(inventoryApi);
  },

And this is using class:

import GrafanaHelper from '@helpers/grafana.helper';

...

  loginUI: async ({ page }, use) => {
    const grafanaHelper = new GrafanaHelper(page);
    await grafanaHelper.suppressTour(page);
    await grafanaHelper.suppressUpgradeNotification(page);
    await grafanaHelper.authorize(page);
    await use();
  },


  api: async ({ page, request }, use) => {
    const grafanaHelper = new GrafanaHelper(page);
    const inventoryApi = new Api(page, request);
    await grafanaHelper.authorize(page);
    await use(inventoryApi);
  },

This is basically the only difference, I just thought the second was more readable but it's more of a style I think, we can use both if you think this is worsening the code complexity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important to note that using fixtures, the grafanaHelper should be only used in here (unless specific test cases that we want to unauthorize and reauthorize). So this loginUI fixture being used inside the other fixtures make spec files more clean (no beforeEach and stuff)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants