Skip to content

Commit

Permalink
Merge pull request #132 from Marketionist/get-page-objects
Browse files Browse the repository at this point in the history
Separate and expose getting page objects
  • Loading branch information
Marketionist authored Nov 24, 2023
2 parents 96ca049 + ae4e560 commit 496db7d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
50 changes: 2 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// #############################################################################

const { ClientFunction, Selector } = require('testcafe');
const path = require('path');
const { readDirectories, createRequest } = require('js-automation-tools');
const { createRequest } = require('js-automation-tools');
const SelectorXPath = require('./utils/selector-xpath.js');
const errors = require('./utils/errors.js');
let pageObjects = require('./utils/get-page-objects.js');

let Given;
let When;
Expand All @@ -26,52 +26,6 @@ try {

const spacesToIndent = 4;

const isCalledExternally = __dirname.includes('node_modules');

const pageObjectsFolderPathes = 'PO_FOLDER_PATH' in process.env ?
process.env.PO_FOLDER_PATH.replace(/\s+/g, '').split(',') :
[path.join('tests', 'page-model')];

const fullPageObjectsFolderPathes = isCalledExternally ?
pageObjectsFolderPathes.map((pageObjectsFolderPath) => {
return path.join(__dirname, '..', '..', pageObjectsFolderPath)
}) :
pageObjectsFolderPathes.map((pageObjectsFolderPath) => {
return path.join(__dirname, pageObjectsFolderPath)
});

// Require all Page Object files in directory
let pageObjects = {};

/**
* Requires Page Object files
* @returns {Array} allRequiredPageObjects
*/
async function requirePageObjects () {
const allPageObjectFiles = await readDirectories(
fullPageObjectsFolderPathes);
const allRequiredPageObjects = allPageObjectFiles.filter(
(value) => {
return value.includes('.js');
}
).map((file) => {
const fileName = path.basename(file, '.js');

pageObjects[fileName] = require(file);

return file;
});

console.log(
'\nPage Objects from PO_FOLDER_PATH:',
`\n${JSON.stringify(pageObjects, null, spacesToIndent)}\n\n`
);

return allRequiredPageObjects;
}

requirePageObjects();

/**
* Checks if locator is XPath
* @param {String} locator
Expand Down
54 changes: 54 additions & 0 deletions utils/get-page-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

// #############################################################################

const path = require('path');
const { readDirectories } = require('js-automation-tools');

const spacesToIndent = 4;

const isCalledExternally = __dirname.includes('node_modules');

const pageObjectsFolderPathes = 'PO_FOLDER_PATH' in process.env ?
process.env.PO_FOLDER_PATH.replace(/\s+/g, '').split(',') :
[path.join('tests', 'page-model')];

const fullPageObjectsFolderPathes = isCalledExternally ?
pageObjectsFolderPathes.map((pageObjectsFolderPath) => {
return path.join(__dirname, '..', '..', '..', pageObjectsFolderPath)
}) :
pageObjectsFolderPathes.map((pageObjectsFolderPath) => {
return path.join(__dirname, '..', pageObjectsFolderPath)
});

// Require all Page Object files in directory
let pageObjects = {};

/**
* Requires Page Object files
* @returns {Array} allRequiredPageObjects
*/
(async function requirePageObjects () {
const allPageObjectFiles = await readDirectories(
fullPageObjectsFolderPathes);
const allRequiredPageObjects = allPageObjectFiles.filter(
(value) => {
return value.includes('.js');
}
).map((file) => {
const fileName = path.basename(file, '.js');

pageObjects[fileName] = require(file);

return file;
});

console.log(
'\nPage Objects from PO_FOLDER_PATH:',
`\n${JSON.stringify(pageObjects, null, spacesToIndent)}\n\n`
);

return pageObjects;
})();

module.exports = pageObjects;

0 comments on commit 496db7d

Please sign in to comment.