Search code examples
node.jsautomationcucumbernightwatch.jscucumberjs

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


The project that I am currently working on, uses Selenium WebDriver Nightwatch and Cucumber.

The issue is that project's folder structure has changed and now 'page_objects_path' in 'nightwatch.conf.js' file looks something like this:

'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"
    ],

Is there any way, where Nightwatch can read all sub-folders from /page-objects/ directory without being explicitly specified in the array as separate paths?


Solution

  • I believe

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

    should be enough. The page class should have sub-classes called by the sub-folders of your structure. E.g. the method getTheCoolElement() from "./endToEndTests/page-objects/mainPage/SubPage.js" should be called like: browser.page.mainPage.SubPage().getTheCoolElement()

    See working example in owncloud phoenix project, that has a hierarchy of page objects: https://github.com/owncloud/phoenix/tree/master/tests/acceptance/pageObjects

    alternatively you could just create that array programmatically using JS. e.g.

    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,
    ....
    }