跳到主要内容

从 @grafana/e2e 迁移

危险

随着 Grafana 11.0.0 的发布,@grafana/e2e 包已弃用,不再提供支持。我们建议所有插件作者将其端到端测试迁移到使用 Playwright 和 @grafana/plugin-e2e,而不是 Cypress 和 @grafana/e2e

在本指南中,你将学习

  • 如何在你的插件中手动设置 @grafana/plugin-e2e
  • 如何迁移测试
  • 如何在 CI 中运行 Playwright 测试
  • 如何卸载 Cypress 和 @grafana/e2e

手动安装 @grafana/plugin-e2e

使用 create-plugin v4.6.0 及更高版本构建的插件会自动包含 @grafana/plugin-e2e@playwright/test 的配置。要手动添加此配置,请按照以下步骤操作

步骤 1:安装 Playwright

@grafana/plugin-e2e 工具扩展了 Playwright API,因此你需要在插件的 package.json 文件中将 @playwright/test 作为开发依赖项安装,最低版本为 1.41.2。有关如何安装的说明,请参阅Playwright 文档。请确保你可以运行安装期间生成的示例测试。如果示例测试成功运行,你可以删除它们,因为不再需要它们了。

步骤 2:安装 @grafana/plugin-e2e

打开终端并在插件的项目目录中运行以下命令

npm install @grafana/plugin-e2e@latest --save-dev

步骤 3:配置 Playwright

打开安装 Playwright 时生成的 Playwright 配置文件。

  1. 取消注释 baseUrl 并将其更改为 'https://:3000'
playwright.config.ts
  baseURL: 'https://:3000',
  1. Playwright 使用projects 将具有相同配置的测试进行逻辑分组。我们将添加两个项目

    1. auth 是一个设置项目,它将登录 Grafana 并将认证状态存储到磁盘。
    2. run-tests 在你选择的浏览器中运行所有测试。通过向 auth 项目添加依赖项,我们确保登录只发生一次,并且 run-tests 项目中的所有测试都将以已认证状态开始。

    你的 Playwright 配置应包含以下项目配置

playwright.config.ts
import { dirname } from 'path';
import { defineConfig, devices } from '@playwright/test';
import type { PluginOptions } from '@grafana/plugin-e2e';

const pluginE2eAuth = `${dirname(require.resolve('@grafana/plugin-e2e'))}/auth`;

export default defineConfig<PluginOptions>({
...
projects: [
{
name: 'auth',
testDir: pluginE2eAuth,
testMatch: [/.*\.js/],
},
{
name: 'run-tests',
use: {
...devices['Desktop Chrome'],
// @grafana/plugin-e2e writes the auth state to this file,
// the path should not be modified
storageState: 'playwright/.auth/admin.json',
},
dependencies: ['auth'],
}
],
});

认证状态以以下文件名模式存储在磁盘上:<plugin-root>/playwright/.auth/<username>.json

为了防止这些文件被版本控制,你可以将以下行添加到你的 .gitignore 文件中

.gitignore
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
/playwright/.auth/

@grafana/e2e 迁移

安装并配置好 Playwright 和 @grafana/plugin-e2e 后,你可以按照以下步骤从 @grafana/e2e 迁移。

迁移测试

目前没有工具可以自动将现有的基于 @grafana/e2e 的 Cypress 测试迁移到基于 @grafana/plugin-e2e 的 Playwright 测试。这意味着你必须一个一个地转换你的测试,或者用一套新的基于 Playwright 的测试来替换它们。请参阅以下资源,以获取有关如何编写 Playwright 测试的灵感

在 CI 中运行测试

要在 CI 中运行针对多个 Grafana 版本的 Playwright 测试,请使用CI 指南中的示例工作流程之一。

注意

请注意,Grafana 不提供任何受支持的方式在其他 CI 平台(例如 Drone 或 CircleCI)中运行针对多个 Grafana 版本的端到端测试。但你可以轻松配置你的 CI 来复制所引用的 Github Action 所做的事情,因为我们所做的没有任何是不能在其他 CI 系统中完成的。

卸载 Cypress 和 @grafana/e2e

尽管我们建议及时从 @grafana/e2e 迁移到 @grafana/plugin-e2e,但在过渡阶段同时使用两者也没有问题。

当所有 Cypress 测试都已迁移后,打开终端并在你的本地插件开发目录中运行以下脚本

1. 移除 Cypress 测试和配置文件

rm ./cypress.json
rm -rf ./cypress

2. 卸载依赖项

npm uninstall --save-dev @grafana/e2e @grafana/e2e-selectors

3. 更新脚本

package.json 文件中,完全移除 e2e:update 脚本,并将 e2e 脚本更改为以下内容

"e2e": "playwright test",