【问题标题】:Can you can define 'page_objects_path' directory which will read from all sub-folders without specifying them explicitly?您能否定义“page_objects_path”目录,该目录将从所有子文件夹中读取,而无需明确指定它们?
【发布时间】:2020-04-14 11:51:46
【问题描述】:

我目前正在进行的项目使用 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/ 目录中的所有子文件夹,而无需在数组中明确指定为单独的路径?

【问题讨论】:

    标签: node.js automation cucumber nightwatch.js cucumberjs


    【解决方案1】:

    我相信

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

    应该够了。 page 类应该具有由结构的子文件夹调用的子类。 例如。来自“./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,
    ....
    }
    

    【讨论】:

    • 第一个建议不是最好的选择,因为如果你重命名其中一个文件,那么你应该替换所有地方。您能否添加有关第二个选项的更多详细信息? “或者,您可以使用 JS 以编程方式创建该数组”
    • 我已经编辑了我的答案,请看看它对你有帮助
    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 2013-06-20
    • 2021-09-20
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多