乐趣区

关于测试工具:Cypress-修复测试时Chromium会自动Crash的问题

报错日志

在 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 测试用例,或者一个 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浏览器未验证。

退出移动版