Skip to content

Commit

Permalink
Plugin E2E: Fix APIs that are broken in older versions of Grafana (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunker authored Apr 3, 2024
1 parent c2f0cae commit 300cdde
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ test('"Save & test" should be successful when configuration is valid', async ({
const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' });
const configPage = await createDataSourceConfigPage({ type: ds.type });
const healthCheckPath = `${selectors.apis.DataSource.proxy(configPage.datasource.uid)}/test`;
await page.route(healthCheckPath, async (route) => await route.fulfill({ status: 200, body: 'OK' }));
await page.route(healthCheckPath, async (route) => await route.fulfill({ status: 200, body: 'OK' })
const healthCheckPath = `${selectors.apis.DataSource.proxy(
configPage.datasource.uid,
configPage.datasource.id.toString()
)}/test`;);
await expect(configPage.saveAndTest({ path: healthCheckPath })).toBeOK();
});
```
Expand All @@ -84,7 +88,10 @@ test('"Save & test" should display success alert box when config is valid', asyn
}) => {
const ds = await readProvisionedDataSource({ fileName: 'datasources.yml' });
const configPage = await createDataSourceConfigPage({ type: ds.type });
const healthCheckPath = `${selectors.apis.DataSource.proxy(configPage.datasource.uid)}/test`;
const healthCheckPath = `${selectors.apis.DataSource.proxy(
configPage.datasource.uid,
configPage.datasource.id.toString()
)}/test`;
await page.route(healthCheckPath, async (route) => await route.fulfill({ status: 200, body: 'OK' }));
await expect(configPage.saveAndTest({ path: healthCheckPath })).toBeOK();
await expect(configPage).toHaveAlert('success');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function printGenerateSuccessMessage(answers: CliArgs) {
: []),
'- `docker-compose up` to start a grafana development server. ' +
(answers.hasBackend
? 'The plugin backend will be reloaded on every code change and a debugger can be attached on port `2345`.'
? 'The plugin backend will be reloaded on every code change and a debugger can be attached on port `2345`.'
: ''),
'- Open http://localhost:3000 in your browser to create a dashboard to begin developing your plugin.',
];
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-e2e/src/e2e-selectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type APIs = {
query: string;
health: (uid: string, id: string) => string;
datasourceByUID: (uid: string) => string;
proxy: (uid: string) => string;
proxy: (uid: string, id: string) => string;
};
Dashboard: {
delete: (uid: string) => string;
Expand Down Expand Up @@ -179,10 +179,12 @@ export type Pages = {
newButton: string;
addVariableCTAV2: (variableName: string) => string;
addVariableCTAV2Item: string;
table: string;
};
Edit: {
url: (dashboardUid: string, editIndex: string) => string;
General: {
selectionOptionsIncludeAllSwitch: string;
generalTypeSelectV2: string;
previewOfValuesOption: string;
submitButton: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-e2e/src/e2e-selectors/versioned/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const versionedAPIs = {
[MIN_GRAFANA_VERSION]: (uid: string) => `/api/datasources/uid/${uid}`,
},
proxy: {
[MIN_GRAFANA_VERSION]: (uid: string) => `api/datasources/proxy/uid/${uid}`,
'9.4.0': (uid: string, _: string) => `api/datasources/proxy/uid/${uid}`,
[MIN_GRAFANA_VERSION]: (_: string, id: string) => `/api/datasources/proxy/${id}`,
},
},
Dashboard: {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-e2e/src/e2e-selectors/versioned/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export const versionedPages = {
'10.4.0': 'data-testid Variable editor Run Query button',
[MIN_GRAFANA_VERSION]: 'Variable editor Submit button',
},
selectionOptionsIncludeAllSwitch: {
[MIN_GRAFANA_VERSION]: 'Variable editor Form IncludeAll switch',
},
},
},
},
Expand Down
10 changes: 9 additions & 1 deletion packages/plugin-e2e/src/models/pages/AnnotationEditPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ export class AnnotationEditPage extends GrafanaPage {
? Dashboard.Settings.Annotations.Edit.url(this.args.dashboard.uid, this.args.id)
: AddDashboard.Settings.Annotations.Edit.url(this.args.id);

return super.navigate(url, options);
await super.navigate(url, options);

// In versions before 9.2.0, the annotation index is not part of the URL so there's no way to navigate to it directly.
// Instead, we have to click the nth row in the variable list to navigate to the edit page for a given annotation index.
if (semver.lt(this.ctx.grafanaVersion, '9.2.0') && this.args.id) {
const list = this.ctx.page.locator('tbody tr');
const variables = await list.all();
await variables[Number(this.args.id)].click();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-e2e/src/models/pages/ExplorePage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const semver = require('semver');
import * as semver from 'semver';
import { Locator, expect } from '@playwright/test';
import { NavigateOptions, PluginTestCtx, RequestOptions } from '../../types';
import { DataSourcePicker } from '../components/DataSourcePicker';
Expand Down
25 changes: 20 additions & 5 deletions packages/plugin-e2e/src/models/pages/VariableEditPage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const gte = require('semver/functions/gte');

import * as semver from 'semver';
import { DashboardEditViewArgs, NavigateOptions, PluginTestCtx } from '../../types';
import { DataSourcePicker } from '../components/DataSourcePicker';
import { GrafanaPage } from './GrafanaPage';
Expand All @@ -24,6 +23,16 @@ export class VariableEditPage extends GrafanaPage {
: AddDashboard.Settings.Variables.Edit.url(this.args.id);

await super.navigate(url, options);

// In versions before 9.2.0, the variable index is not part of the URL so there's no way to navigate to it directly.
// Instead, we have to click the nth row in the variable list to navigate to the edit page for a given variable index.
if (semver.lt(this.ctx.grafanaVersion, '9.2.0') && this.args.id) {
const list = this.getByGrafanaSelector(this.ctx.selectors.pages.Dashboard.Settings.Variables.List.table).locator(
'tbody tr'
);
const variables = await list.all();
await variables[Number(this.args.id)].click();
}
}

/**
Expand Down Expand Up @@ -51,13 +60,19 @@ export class VariableEditPage extends GrafanaPage {
*/
async runQuery() {
// in 9.2.0, the submit button got a new purpose. it no longer submits the form, but instead runs the query
if (gte(this.ctx.grafanaVersion, '9.2.0')) {
if (semver.gte(this.ctx.grafanaVersion, '9.2.0')) {
await this.getByGrafanaSelector(
this.ctx.selectors.pages.Dashboard.Settings.Variables.Edit.General.submitButton
).click();
} else {
// in 9.1.3, the submit button submits the form
await this.ctx.page.keyboard.press('Tab');
// in version before 9.2.0, there's no submit button, so instead we click the "Include all" button to trigger the query
const queryDataRequest = this.waitForQueryDataRequest();
const includeAllSwitch = this.getByGrafanaSelector(
this.ctx.selectors.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsIncludeAllSwitch
).locator('..');
await includeAllSwitch.click();
await queryDataRequest;
await includeAllSwitch.click();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as semver from 'semver';
import { test, expect, AnnotationPage } from '../../../../src';
import { REDSHIFT_SCHEMAS } from '../mocks/resource';

Expand All @@ -13,7 +14,7 @@ test('should load resources and display them as options when clicking on an inpu
await expect(annotationEditPage.getByGrafanaSelector('Select option')).toContainText(REDSHIFT_SCHEMAS);
});

test('should be able to add a new annotation when there annotations already exist', async ({
test('should be able to add a new annotation when annotations already exist', async ({
page,
selectors,
grafanaVersion,
Expand All @@ -24,5 +25,9 @@ test('should be able to add a new annotation when there annotations already exis
const annotationPage = new AnnotationPage({ page, selectors, grafanaVersion, request, testInfo }, dashboard);
await annotationPage.goto();
await annotationPage.clickAddNew();
await expect(page).toHaveTitle(/New annotation.*/);
if (semver.gte(grafanaVersion, '9.2.0')) {
await expect(page).toHaveTitle(/New annotation.*/);
} else {
await expect(page.getByText('When enabled the annotation query is issued every dashboard refresh')).toBeVisible();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ test('should call a custom health endpoint when healthCheckPath is provided', as
selectors,
}) => {
const configPage = await createDataSourceConfigPage({ type: 'marcusolsson-json-datasource' });
const healthCheckPath = selectors.apis.DataSource.proxy(configPage.datasource.uid);
const healthCheckPath = selectors.apis.DataSource.proxy(
configPage.datasource.uid,
configPage.datasource.id.toString()
);
await page.route(healthCheckPath, async (route) => {
await route.fulfill({ status: 200, body: 'OK' });
});
Expand Down

0 comments on commit 300cdde

Please sign in to comment.