首页 > 解决方案 > Cypress - supportFile 中的 before() 钩子在每个规范之前运行

问题描述

根据文档,这个钩子应该只在每个套件运行时执行一次(每次cypress run调用一次),但这似乎在每个规范之前执行。这是一个错误吗?还是我做错了什么?

   Cypress.Cookies.defaults({
    preserve: ['cookie']
  })
before(() => {
      cy.log("Should only run once")
    })
});

标签: automationmocha.jscypressui-automation

解决方案


before()钩子每个规范文件运行一次,而不是每个cypress run.

看起来您需要Before Run API

on('before:run', (details) => {
  /* ... */
})

推荐阅读