報(bào)錯(cuò)日志
在 Jenkins CI,全量運(yùn)行Cypress全量測(cè)試用例匾寝,可能會(huì)出現(xiàn)以下報(bào)錯(cuò)搬葬。
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的報(bào)錯(cuò)信息很詳細(xì),已經(jīng)分析出該錯(cuò)誤的大概的幾個(gè)原因艳悔。
原因
上述報(bào)錯(cuò)的原因急凰,雖然大概率是由于Chromium出現(xiàn)異常退出。但我們還能從Cypress自身編寫(xiě)用例的規(guī)范進(jìn)行優(yōu)化猜年,減少上述問(wèn)題出現(xiàn)的概率抡锈。
解決方案
1、在編寫(xiě)js測(cè)試用例時(shí)乔外,盡量不要太多內(nèi)嵌的context
測(cè)試用例企孩,或者一個(gè)describe
或context
測(cè)試用例集內(nèi),不要存放太多的 it
袁稽。context
盡量控制在3-4個(gè)內(nèi),而且不要在context
內(nèi)再嵌套context
測(cè)試用例集擒抛。 it
步驟盡量控制在10個(gè)內(nèi)推汽。
上述方案不是固定的解決方案,是我在編寫(xiě)Cypress測(cè)試用例時(shí)總結(jié)的規(guī)律歧沪。
2歹撒、啟動(dòng)瀏覽器時(shí),添加參數(shù)--disable-dev-shm-usage
诊胞。該參數(shù)使用本地local/tmp
代替/dev/shm
作為 Chrome 的運(yùn)行空間暖夭,local/tmp
比/dev/shm
有更大的空間,可以使Cypress運(yùn)行時(shí)撵孤,不容易因?yàn)橐粋€(gè)文件的測(cè)試用例數(shù)多迈着,導(dǎo)致內(nèi)存溢出的問(wèn)題。
在Cypress項(xiàng)目根目錄邪码,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
瀏覽器測(cè)試通過(guò),Electron
闭专、Edge
瀏覽器未驗(yàn)證奴潘。