| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  | import { Page } from 'playwright-core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { test, expect, E2ESelectorGroups } from '@grafana/plugin-e2e'; | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { addDashboard } from '../utils/dashboard-helpers'; | 
					
						
							|  |  |  | import { getResources } from '../utils/prometheus-helpers'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  | test.describe( | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |   'Prometheus variable query editor', | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     tag: ['@various'], | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   () => { | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     const DATASOURCE_PREFIX = 'prometheusVariableDS'; | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Click dashboard settings and then the variables tab | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     async function navigateToVariables(page: Page, selectors: E2ESelectorGroups) { | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       const editButton = page.getByTestId(selectors.components.NavToolbar.editDashboard.editButton); | 
					
						
							|  |  |  |       await expect(editButton).toBeVisible(); | 
					
						
							|  |  |  |       await editButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const settingsButton = page.getByTestId(selectors.components.NavToolbar.editDashboard.settingsButton); | 
					
						
							|  |  |  |       await expect(settingsButton).toBeVisible(); | 
					
						
							|  |  |  |       await settingsButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const variablesTab = page.getByTestId(selectors.components.Tab.title('Variables')); | 
					
						
							|  |  |  |       await variablesTab.click(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Begin the process of adding a query type variable for a Prometheus data source | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     async function addPrometheusQueryVariable( | 
					
						
							|  |  |  |       page: Page, | 
					
						
							|  |  |  |       selectors: E2ESelectorGroups, | 
					
						
							|  |  |  |       datasourceName: string, | 
					
						
							|  |  |  |       variableName: string | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       const addVariableButton = page.getByTestId(selectors.pages.Dashboard.Settings.Variables.List.addVariableCTAV2); | 
					
						
							|  |  |  |       await addVariableButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const nameInput = page.getByTestId(selectors.pages.Dashboard.Settings.Variables.Edit.General.generalNameInputV2); | 
					
						
							|  |  |  |       await nameInput.clear(); | 
					
						
							|  |  |  |       await nameInput.fill(variableName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const dataSourcePicker = page.getByTestId(selectors.components.DataSourcePicker.container); | 
					
						
							|  |  |  |       await expect(dataSourcePicker).toBeVisible(); | 
					
						
							|  |  |  |       await dataSourcePicker.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       const dataSourceOption = page.getByText(datasourceName); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       await dataSourceOption.scrollIntoViewIfNeeded(); | 
					
						
							|  |  |  |       await expect(dataSourceOption).toBeVisible(); | 
					
						
							|  |  |  |       await dataSourceOption.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await getResources(page); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a Prometheus variable and navigate to the query editor to check that it is available to use. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     async function variableFlowToQueryEditor( | 
					
						
							|  |  |  |       page: Page, | 
					
						
							|  |  |  |       selectors: E2ESelectorGroups, | 
					
						
							|  |  |  |       datasourceName: string, | 
					
						
							|  |  |  |       variableName: string, | 
					
						
							|  |  |  |       queryType: string | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       await addDashboard(page); | 
					
						
							|  |  |  |       await navigateToVariables(page, selectors); | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       await addPrometheusQueryVariable(page, selectors, datasourceName, variableName); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Select query type
 | 
					
						
							|  |  |  |       const queryTypeSelect = page.getByTestId( | 
					
						
							|  |  |  |         selectors.components.DataSource.Prometheus.variableQueryEditor.queryType | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       await queryTypeSelect.click(); | 
					
						
							|  |  |  |       await selectOption(page, queryType); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Apply the variable
 | 
					
						
							|  |  |  |       const applyButton = page.getByTestId(selectors.pages.Dashboard.Settings.Variables.Edit.General.applyButton); | 
					
						
							|  |  |  |       await applyButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Close to return to dashboard
 | 
					
						
							|  |  |  |       const backToDashboardButton = page.getByTestId( | 
					
						
							|  |  |  |         selectors.components.NavToolbar.editDashboard.backToDashboardButton | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       await expect(backToDashboardButton).toBeVisible(); | 
					
						
							|  |  |  |       await backToDashboardButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Add visualization
 | 
					
						
							|  |  |  |       const createNewPanelButton = page.getByTestId(selectors.pages.AddDashboard.itemButton('Create new panel button')); | 
					
						
							|  |  |  |       await expect(createNewPanelButton).toBeVisible(); | 
					
						
							|  |  |  |       await createNewPanelButton.click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Close the data source picker modal
 | 
					
						
							|  |  |  |       const closeButton = page.getByRole('button', { name: 'Close menu' }); | 
					
						
							|  |  |  |       await closeButton.click({ force: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Select prom data source from the data source list
 | 
					
						
							|  |  |  |       const dataSourcePickerInput = page.getByTestId(selectors.components.DataSourcePicker.inputV2); | 
					
						
							|  |  |  |       await dataSourcePickerInput.click(); | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       await dataSourcePickerInput.fill(datasourceName); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       await page.keyboard.press('Enter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Confirm the variable exists in the correct input
 | 
					
						
							|  |  |  |       switch (queryType) { | 
					
						
							|  |  |  |         case 'Label names': | 
					
						
							|  |  |  |           const labelSelect = page.getByTestId(selectors.components.QueryBuilder.labelSelect); | 
					
						
							|  |  |  |           await expect(labelSelect).toBeVisible(); | 
					
						
							|  |  |  |           await labelSelect.click(); | 
					
						
							|  |  |  |           await selectOption(page, variableName); | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         case 'Label values': | 
					
						
							|  |  |  |           const valueSelect = page.getByTestId(selectors.components.QueryBuilder.valueSelect); | 
					
						
							|  |  |  |           await expect(valueSelect).toBeVisible(); | 
					
						
							|  |  |  |           await valueSelect.click(); | 
					
						
							|  |  |  |           await selectOption(page, variableName); | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         case 'Metrics': | 
					
						
							|  |  |  |           const metricSelect = page.getByTestId( | 
					
						
							|  |  |  |             selectors.components.DataSource.Prometheus.queryEditor.builder.metricSelect | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |           await expect(metricSelect).toBeVisible(); | 
					
						
							|  |  |  |           await metricSelect.click(); | 
					
						
							|  |  |  |           await selectOption(page, variableName); | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |           // do nothing
 | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('should navigate to variable query editor', async ({ page, selectors }) => { | 
					
						
							|  |  |  |       await addDashboard(page); | 
					
						
							|  |  |  |       await navigateToVariables(page, selectors); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |     test('should select a query type for a Prometheus variable query', async ({ | 
					
						
							|  |  |  |       createDataSource, | 
					
						
							|  |  |  |       page, | 
					
						
							|  |  |  |       selectors, | 
					
						
							|  |  |  |     }) => { | 
					
						
							|  |  |  |       const DATASOURCE_NAME = `${DATASOURCE_PREFIX}_${Date.now()}`; | 
					
						
							|  |  |  |       await createDataSource({ type: 'prometheus', name: DATASOURCE_NAME }); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       await addDashboard(page); | 
					
						
							|  |  |  |       await navigateToVariables(page, selectors); | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       await addPrometheusQueryVariable(page, selectors, DATASOURCE_NAME, 'labelsVariable'); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Select query type
 | 
					
						
							|  |  |  |       const queryTypeSelect = page.getByTestId( | 
					
						
							|  |  |  |         selectors.components.DataSource.Prometheus.variableQueryEditor.queryType | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       await queryTypeSelect.click(); | 
					
						
							|  |  |  |       await selectOption(page, 'Label names'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('should create a label names variable that is selectable in the label select in query builder', async ({ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       createDataSource, | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       page, | 
					
						
							|  |  |  |       selectors, | 
					
						
							|  |  |  |     }) => { | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       const DATASOURCE_NAME = `${DATASOURCE_PREFIX}_${Date.now()}`; | 
					
						
							|  |  |  |       await createDataSource({ type: 'prometheus', name: DATASOURCE_NAME }); | 
					
						
							|  |  |  |       await variableFlowToQueryEditor(page, selectors, DATASOURCE_NAME, 'labelnames', 'Label names'); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('should create a label values variable that is selectable in the label values select in query builder', async ({ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       createDataSource, | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       page, | 
					
						
							|  |  |  |       selectors, | 
					
						
							|  |  |  |     }) => { | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       const DATASOURCE_NAME = `${DATASOURCE_PREFIX}_${Date.now()}`; | 
					
						
							|  |  |  |       await createDataSource({ type: 'prometheus', name: DATASOURCE_NAME }); | 
					
						
							|  |  |  |       await variableFlowToQueryEditor(page, selectors, DATASOURCE_NAME, 'labelvalues', 'Label values'); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('should create a metric names variable that is selectable in the metric select in query builder', async ({ | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       createDataSource, | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |       page, | 
					
						
							|  |  |  |       selectors, | 
					
						
							|  |  |  |     }) => { | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  |       const DATASOURCE_NAME = `${DATASOURCE_PREFIX}_${Date.now()}`; | 
					
						
							|  |  |  |       await createDataSource({ type: 'prometheus', name: DATASOURCE_NAME }); | 
					
						
							|  |  |  |       await variableFlowToQueryEditor(page, selectors, DATASOURCE_NAME, 'metrics', 'Metrics'); | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-08 23:57:39 +08:00
										 |  |  | async function selectOption(page: Page, option: string) { | 
					
						
							| 
									
										
										
										
											2025-07-11 17:31:33 +08:00
										 |  |  |   const optionElement = page.getByRole('option', { name: option }); | 
					
						
							|  |  |  |   await expect(optionElement).toBeVisible(); | 
					
						
							|  |  |  |   await optionElement.click(); | 
					
						
							|  |  |  | } |