报错日志
在 Jenkins CI,全量运行Cypress全量测试用例,可能会出现以下报错。
We detected that the Chromium Renderer process just crashed.
This is the equivalent to seeing the 'sad face' when Chrome dies.
This can happen for a number of different reasons:
- You wrote an endless loop and you must fix your own code
- There is a memory leak in Cypress (unlikely but possible)
- You are running Docker (there is an easy fix for this: see link below)
- You are running lots of tests on a memory intense application
- You are running in a memory starved VM environment
- There are problems with your GPU / GPU drivers
- There are browser bugs in Chromium
You can learn more including how to fix Docker here:
https://on.cypress.io/renderer-process-crashed
Cypress的报错信息很详细,已经分析出该错误的大概的几个原因。
原因
上述报错的原因,虽然大概率是由于Chromium出现异常退出。但我们还能从Cypress自身编写用例的规范进行优化,减少上述问题出现的概率。
解决方案
1、在编写js测试用例时,尽量不要太多内嵌的context
测试用例,或者一个describe
或context
测试用例集内,不要存放太多的 it
。context
尽量控制在3-4个内,而且不要在context
内再嵌套context
测试用例集。 it
步骤尽量控制在10个内。
上述方案不是固定的解决方案,是我在编写Cypress测试用例时总结的规律。
2、启动浏览器时,添加参数--disable-dev-shm-usage
。该参数使用本地local/tmp
代替/dev/shm
作为 Chrome 的运行空间,local/tmp
比/dev/shm
有更大的空间,可以使Cypress运行时,不容易因为一个文件的测试用例数多,导致内存溢出的问题。
在Cypress项目根目录,cypress/plugins/index.js
文件中的 module.exports
添加以下代码。
# 完整代码
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on("before:browser:launch", (browser = {}, launchOptions) => {
launchOptions.preferences.darkTheme = true
if (browser.name === "chrome") {
launchOptions.args.push("--disable-dev-shm-usage");
return launchOptions;
}
})
}
PS:上述代码仅在Chrome
浏览器测试通过,Electron
、Edge
浏览器未验证。