0

Suppose I've ordered my page objects files in the following folders:

pages
    modals
        modal-1.coffee
        modal-2.coffee
    panels
        panel-1.coffee
        panel-2.coffee
        subpanels
            subpanel-1.coffee
            subpanel-2.coffee
    mainPage.coffee

As of right now, I'm requiring each page object individually at the top of my spec files like thus:

{Modal1} = require <path to modal-1>
...
{MainPage} = require <path to mainPage>

However, I'd like to be able to just make one call, along the lines of:

Pages = require <path to pages dir>

I found this SA question and this blog post. But I'm getting an error where while the page objects are being read (confirmed with console.log statements in my index.coffee file), their member functions are inaccessible:

Pages.MainPage is not a function
Cannot read property 'getSomething' of undefined

So my question is - how do I access the member functions of the page objects when I've exported them as a single module Pages?

Any insights would be really appreciated!

0Celestine
  • 81
  • 2
  • 4

1 Answers1

0

I am not familiar with coffeescript, but in JS you can try to re-export with index.js

pages
    modals
        modal-1.coffee
        modal-2.coffee
    panels
        panel-1.coffee
        panel-2.coffee
        subpanels
            subpanel-1.coffee
            subpanel-2.coffee
    mainPage.coffee
    index.coffee (?)

in your index file:

module.exports.modal-1 = require('modals/modal-1.coffee')
module.exports.modal-2 = require('modals/modal-2.coffee')
module.exports.panel-1 = require('panels/panel-1.coffee')
module.exports.mainPage = require('./mainPage.coffee')
// and so on

So in specs it would be -

let {modal-1, modal-2, mainPage} = require('pages')

https://nodejs.org/api/modules.html#modules_folders_as_modules

If there is no package.json file present in the directory, then Node.js will attempt to load an index.js

Xotabu4
  • 3,063
  • 17
  • 29