报错日志

在 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 ChromiumYou can learn more including how to fix Docker here:https://on.cypress.io/renderer-process-crashed

Cypress的报错信息很具体,曾经剖析出该谬误的大略的几个起因。

起因

上述报错的起因,尽管大概率是因为Chromium出现异常退出。但咱们还能从Cypress本身编写用例的标准进行优化,缩小上述问题呈现的概率。

解决方案

1、在编写js测试用例时,尽量不要太多内嵌的context测试用例,或者一个describecontext测试用例集内,不要寄存太多的 itcontext尽量管制在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浏览器测试通过,ElectronEdge浏览器未验证。