浏览器启动事件
在 Cypress 启动浏览器之前,您可以通过 setupNodeEvents 函数修改浏览器偏好设置、安装扩展、添加或移除命令行参数,以及调整其他选项。
语法
⚠️ 这段代码属于
setupNodeEvents 函数的一部分,
因此会在 Node 环境中执行。你不能在此函数中调用 Cypress
或 cy
命令,
但可以直接访问文件系统和操作系统的其他部分。
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser = {}, launchOptions) => {
/* ... */
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser = {}, launchOptions) => {
/* ... */
})
},
},
})
browser (object)
描述正在启动的浏览器对象,包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
name | string | 机器友好名称,如 chrome 、electron 、edge 或 firefox 。 |
family | string | 使用的渲染引擎。chromium 或 firefox 。 |
channel | string | 浏览器发布渠道,如 stable 、dev 或 canary 。 |
displayName | string | 人类可读的浏览器显示名称。 |
version | string | 完整版本号。 |
path | string | 浏览器在磁盘上的路径。Electron 为空。 |
info | string | (可选) 浏览器的额外信息(用于浏览器选择器显示) |
majorVersion | number | 浏览器的主版本号。 |
isHeadless | boolean | 浏览器是否以无头模式运行。 |
isHeaded | boolean | 浏览器是否以有界面模式运行。 |
launchOptions (object)
可修改的浏览器启动选项对象,包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
preferences | object | 描述浏览器偏好的对象。不同浏览器间有差异。详见修改浏览器偏好设置。 |
args | string[] | 启动浏览器时作为命令行参数传递的字符串数组。对 Electron 无效。详见修改浏览器启动参数。 |
extensions | string[] | 包含未打包 WebExtensions 的文件夹路径数组,这些扩展将在浏览器启动前加载。详见添加浏览器扩展。 |
env | object | 传递给启动浏览器的环境变量对象。详见配置浏览器环境。 |
使用方式
通过 setupNodeEvents 函数,您可以监听 before:browser:launch
事件并修改 Cypress 启动浏览器的方式(例如修改参数、用户偏好和扩展)。
该事件会提供 browser
对象(包含浏览器信息)和 launchOptions
对象(允许修改浏览器启动方式)。
返回的 launchOptions
对象将成为浏览器的新启动选项。
修改浏览器启动参数
当前支持浏览器的可用参数:
默认打开开发者工具
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser = {}, launchOptions) => {
// `args` 是启动浏览器时将传递的所有参数数组
console.log(launchOptions.args) // 打印所有当前参数
if (browser.family === 'chromium' && browser.name !== 'electron') {
// 自动打开开发者工具
launchOptions.args.push('--auto-open-devtools-for-tabs')
}
if (browser.family === 'firefox') {
// 自动打开开发者工具
launchOptions.args.push('-devtools')
}
if (browser.name === 'electron') {
// 自动打开开发者工具
launchOptions.preferences.devTools = true
}
// 返回的内容将成为新的 launchOptions
return launchOptions
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser = {}, launchOptions) => {
// `args` 是启动浏览器时将传递的所有参数数组
console.log(launchOptions.args) // 打印所有当前参数
if (browser.family === 'chromium' && browser.name !== 'electron') {
// 自动打开开发者工具
launchOptions.args.push('--auto-open-devtools-for-tabs')
}
if (browser.family === 'firefox') {
// 自动打开开发者工具
launchOptions.args.push('-devtools')
}
if (browser.name === 'electron') {
// 自动打开开发者工具
launchOptions.preferences.devTools = true
}
// 返回的内容将成为新的 launchOptions
return launchOptions
})
},
},
})
添加浏览器扩展
限制:
- 无头 Chrome 不支持加载扩展。
- 标准 Chrome 等 Chrome 品牌浏览器 137 及以上版本由 于 Chrome 移除了
--load-extension
标志,不再支持通过此 API 加载扩展。建议使用 Chrome for Testing 或 Chromium。参见 Cypress Docker 镜像示例:Chrome for Testing 和 Chromium。 - Electron 目前仅支持 Chrome 开发者工具扩展。
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
// 提供未打包扩展文件夹的绝对路径
launchOptions.extensions.push('Users/jane/path/to/extension')
return launchOptions
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
// 提供未打包扩展文件夹的绝对路径
launchOptions.extensions.push('Users/jane/path/to/extension')
return launchOptions
})
},
},
})
配置浏览器环境
此选项不支持 Electron 目标。
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
launchOptions.env.CUSTOM_ENV_VALUE = '1'
return launchOptions
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
launchOptions.env.CUSTOM_ENV_VALUE = '1'
return launchOptions
})
},
},
})
修改浏览器偏好设置
当前支持浏览器的可用偏好设置:
- 基于 Chromium 的浏览器
- Electron
- Firefox:在 Firefox 浏览器中访问
about:config
URL 查看所有可用偏好设置。
如果想完全忽略 Chrome 偏好设置,可以在运行 Cypress 时设置 IGNORE_CHROME_PREFERENCES
环境变量。
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
if (browser.family === 'chromium' && browser.name !== 'electron') {
// 在 Chromium 中,偏好设置可以存在于 Local State、Preferences 或 Secure Preferences 中
// 通过 launchOptions.preferences,可以访问 `localState`、`default` 和 `secureDefault`
// 例如,在 Preferences 中设置 `somePreference: true`:
launchOptions.preferences.default['preference'] = true
return launchOptions
}
if (browser.family === 'firefox') {
// launchOptions.preferences 是偏好名称到值的映射
launchOptions.preferences['some.preference'] = true
return launchOptions
}
if (browser.name === 'electron') {
// launchOptions.preferences 是一个 `BrowserWindow` `options` 对象
launchOptions.preferences.darkTheme = true
return launchOptions
}
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
if (browser.family === 'chromium' && browser.name !== 'electron') {
// 在 Chromium 中,偏好设置可以存在于 Local State、Preferences 或 Secure Preferences 中
// 通过 launchOptions.preferences,可以访问 `localState`、`default` 和 `secureDefault`
// 例如,在 Preferences 中设置 `somePreference: true`:
launchOptions.preferences.default['preference'] = true
return launchOptions
}
if (browser.family === 'firefox') {
// launchOptions.preferences 是偏好名称到值的映射
launchOptions.preferences['some.preference'] = true
return launchOptions
}
if (browser.name === 'electron') {
// launchOptions.preferences 是一个 `BrowserWindow` `options` 对象
launchOptions.preferences.darkTheme = true
return launchOptions
}
})
},
},
})
修改 Electron 应用开关
Cypress Launchpad 是一个 Electron 应用程序,其行为(以及内置 Electron 浏览器的行为)可以通过命令行开关进行自定义。支持的开关取决于 Electron 版本,参见 Electron 文档。
您可以使用 ELECTRON_EXTRA_LAUNCH_ARGS
环境变量传 递 Electron 特定的启动参数。例如,要禁用 HTTP 浏览器缓存并忽略证书错误,可以像下面这样在运行 Cypress 前设置环境变量:
Linux/OSX
export ELECTRON_EXTRA_LAUNCH_ARGS=--disable-http-cache --lang=es
Windows
set ELECTRON_EXTRA_LAUNCH_ARGS=--disable-http-cache --lang=es
Cypress 已经在内部设置了一些 Electron 命令行开关。参见文件 packages/server/lib/environment.js。目前无法在 Electron 启动后查看所有已设置的开关。
查看所有 Chrome 浏览器开关
如果使用基于 Chromium 的浏览器运行 Cypress 测试,可以通过打开新标签页并输入 chrome://version
URL 查看所有当前设置的命令行开关和浏览器版本信息。
