You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Cypress中全局设置contains的{matchCase:false}参数?

全局配置 Cypress contains 的 matchCase: false

当然可以全局配置,不用每次调用都重复写参数,下面是两种实用方法:

方法一:重写 Cypress 命令(推荐)

在 cypress/support/commands.js 文件中重写 contains 命令,默认注入 matchCase: false 配置,同时保留局部覆盖的可能性:

Cypress.Commands.overwrite('contains', (originalFn, subject, filter, text, options = {}) => {
  // 兼容参数顺序:当不传filter时,参数会发生偏移
  if (typeof text === 'object') {
    options = text
    text = filter
    filter = undefined
  }
  // 全局默认关闭大小写匹配,若局部需要开启可传{matchCase: true}覆盖
  options.matchCase = false
  return originalFn(subject, filter, text, options)
})

配置完成后,所有 cy.contains('目标文本') 调用都会自动启用大小写不敏感匹配。

方法二:修改全局配置文件

如果使用 Cypress 12 及以上版本,可直接在 cypress.config.js(或 .ts)的全局配置中设置:

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // 其他节点事件配置
    },
    // 全局设置contains的默认参数
    contains: {
      matchCase: false
    }
  }
})

这种方式更简洁,但仅支持较新版本的 Cypress。


内容的提问来源于stack exchange,提问作者Noel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 22:00:23