您可以定义 'page_objects_path' 目录,该目录将从所有子文件夹中读取而无需明确指定它们吗?

Can you can define 'page_objects_path' directory which will read from all sub-folders without specifying them explicitly?

我目前正在做的项目,使用了 Selenium WebDriver Nightwatch 和 Cucumber。

问题是项目的文件夹结构已更改,现在 'nightwatch.conf.js' 文件中的 'page_objects_path' 看起来有些问题像这样:

'page_objects_path':
    [
        "./componentTests/page-objects",
        "./componentTests/page-objects/xxxxxx",
        "./componentTests/page-objects/xxxxx xxxx",
        "./endToEndTests/page-objects",
        "./endToEndTests/page-objects/xxxx",
        "./endToEndTests/page-objects/xxxxxxx",
        "./endToEndTests/page-objects/xxxx xxxxx",
        "./endToEndTests/page-objects/xxxxxx"
        "./endToEndTests/page-objects/xxxxxxxxxx"
    ],

有什么方法可以让 Nightwatch 从 /page-objects/ 目录中读取所有子文件夹,而无需在数组中明确指定为单独的路径?

我相信

'page_objects_path':
    [
        "./componentTests/page-objects",
        "./endToEndTests/page-objects",
    ],

应该够了。 page class 应该有子 class 由你的结构的子文件夹调用。 例如。 “./endToEndTests/page-objects/mainPage/SubPage.js”中的方法 getTheCoolElement() 应该这样调用:browser.page.mainPage.SubPage().getTheCoolElement()

查看 owncloud phoenix 项目中的工作示例,它具有页面对象的层次结构:https://github.com/owncloud/phoenix/tree/master/tests/acceptance/pageObjects

或者,您可以使用 JS 以编程方式创建该数组。例如

const fs = require('fs')
// const path = require('path')

const getAllFolders = function (dirPath, arrayOfFiles) {
  const files = fs.readdirSync(dirPath)

  arrayOfFiles = arrayOfFiles || []

  files.forEach(function (file) {
    if (fs.statSync(dirPath + '/' + file).isDirectory()) {
      arrayOfFiles = getAllFolders(dirPath + '/' + file, arrayOfFiles)
      arrayOfFiles.push(path.join(dirPath, '/', file))
    }
  })

  return arrayOfFiles
}

let allPageObjectPath = getAllFolders(
  path.join(__dirname, '/componentTests/page-objects')
)
allPageObjectPath = allPageObjectPath.concat(
  getAllFolders(path.join(__dirname, '/endToEndTests/page-objects'))
)

module.exports = {
  page_objects_path: allPageObjectPath,
....
}