【问题标题】:Cypress | require() function does not work when variable is passed柏树 |传递变量时 require() 函数不起作用
【发布时间】:2021-10-21 12:04:05
【问题描述】:

请帮助我为什么这不起作用

// Does not work when varialbe pass in require()
var fileName =
  '/Users/ashdiksh/Documents/Automation/CypressTest/cypress/integration/common/pageObjects/cypressDemo.js'
var fileObj = require(fileName)
cy.log(fileObj.elements.web.email)
  // Works fine when path is hardcoded
  var fileObj = require('/Users/ashdiksh/Documents/Automation/CypressTest/cypress/integration/common/pageObjects/cypressDemo.js');
  cy.log(fileObj.elements.web.email);

收到错误:

找不到模块 '/Users/ashdiksh/Documents/Automation/CypressTest/cypress/integration/common/pageObjects/cypressDemo.js'

【问题讨论】:

  • 这已经被回答了。你可以看看这个答案 - stackoverflow.com/a/17448839/4571271
  • 不,这个答案在赛普拉斯不起作用。
  • @AshokDikshit 我很想知道您为什么要动态导入 pageObject?

标签: cypress cypress-cucumber-preprocessor


【解决方案1】:

我认为您需要参与 webpack 编译过程才能使其工作,但这有点麻烦。

尝试将index.js 添加到您的/cypress/integration/common/pageObjects 文件夹。

/cypress/integration/common/pageObjects/index.js

module.exports = {
  pageObject1: require('./pageObject1'),
  pageObject2: require('./pageObject2')
}

然后在测试中

const pageObjects = require('./common/pageObjects')  // load the index.js
                                                     // relative to /cypress/integration

it('uses a dynamic pageObject', () => {

  const pageObject = pageObjects['pageObject1']

})

【讨论】:

  • 感谢您的评论@Fody,我认为这是一个常见问题。这可能是柏树相关的行为。
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 2018-08-29
  • 2012-01-18
  • 2018-03-23
  • 2015-10-25
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
相关资源
最近更新 更多