cy.request在Cypress插件文件中不起作用?

时间:2018-11-23 10:43:33

标签: cypress

 return cy.request('https://webiste.com/config.json').then( (appConfig) => {
  // save to Cypress.config
 });

上面的代码抛出此错误:

插件文件导出的函数抛出错误。

我们调用了'C:\ projects \ nfe-credit-flow \ cypress \ plugins \ index.js'导出的函数,但是抛出了错误。

引发了以下错误:

  

ReferenceError:未定义cy       然后在Promise.then(C:\ projects \ nfe-credit-flow \ cypress \ plugins \ index.js:74:4)       在tryCatcher(C:\ projects \ nfe-credit-flow \ node_modules \ bluebird \ js \ release \ util.js:16:23)       在Promise._settlePromiseFromHandler(C:\ projects \ nfe-credit-flow \ node_modules \ bluebird \ js \ release \ promise.js:512:31)       在Promise._settlePromise(C:\ projects \ nfe-credit-flow \ node_modules \ bluebird \ js \ release \ promise.js:569:18)       在Promise._settlePromise0(C:\ projects \ nfe-credit-flow \ node_modules \ bluebird \ js \ release \ promise.js:614:10)       在Promise._settlePromises(C:\ projects \ nfe-credit-flow \ node_modules \ bluebird \ js \ release \ promise.js:693:18)

1 个答案:

答案 0 :(得分:2)

插件在cypress在浏览器中运行时,插件运行node.js任务。您应使用request之类的npm软件包来执行此操作。 config变量可从插件访问,例如module.exports = (on, config) => { ...

由于cypress使用请求,所以最好具有相同的依赖关系,而不要像axios这样的新依赖项。因此,npm i request比:

const request = require('request');
request('https://webiste.com/config.json').then( (appConfig) => {
  config.whatever = appConfig.whatever
 });

或者您可以使用merge函数(例如从loadsh)覆盖config