Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
refactored how page, shared, helper objects are loaded into global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doherty committed Jan 7, 2021
1 parent 286cf79 commit feb0cc5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var program = require('commander');
var pjson = require('./package.json');
var cucumber = require('cucumber');
var chalk = require('chalk');
var helpers = require('./runtime/helpers.js');
var merge = require('merge');
var requireDir = require('require-dir');

var config = {
featureFiles: './features',
Expand Down Expand Up @@ -91,7 +94,14 @@ global.devTools = program.devTools;
global.userAgent = String(program.userAgent || '').replace(/(^"|"$)/g, '');

// used within world.js to import page objects
global.pageObjectPath = path.resolve(config.pageObjects);
var pageObjectPath = path.resolve(config.pageObjects);

// load page objects (after global vars have been created)
if (fs.existsSync(pageObjectPath)) {

// require all page objects using camel case as object names
global.pageObjects = requireDir(pageObjectPath, { camelcase: true, recurse: true });
}

// used within world.js to output reports
global.reportsPath = path.resolve(config.reports);
Expand All @@ -109,7 +119,26 @@ global.noScreenshot = (program.noScreenshot);
global.DEFAULT_TIMEOUT = global.DEFAULT_TIMEOUT || program.timeOut || 10 * 1000;

// used within world.js to import shared objects into the shared namespace
global.sharedObjectPaths = path.resolve(config.sharedObjects);
var sharedObjectsPath = path.resolve(config.sharedObjects);

// import shared objects
if (fs.existsSync(sharedObjectsPath)) {

var allDirs = {};
var dir = requireDir(sharedObjectsPath, { camelcase: true, recurse: true });

merge(allDirs, dir);

// if we managed to import some directories, expose them
if (Object.keys(allDirs).length > 0) {

// expose globally
global.sharedObjects = allDirs;
}
}

// add helpers
global.helpers = helpers;

// rewrite command line switches for cucumber
process.argv.splice(2, 100);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-cucumber-js",
"version": "1.2.0",
"version": "1.3.0",
"description": "Browser Automation framework using puppeteer and cucumber-js",
"main": "index.js",
"scripts": {
Expand Down
38 changes: 0 additions & 38 deletions runtime/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

var fs = require('fs-plus');
var path = require('path');
var requireDir = require('require-dir');
var merge = require('merge');
var chalk = require('chalk');
var puppeteer = require('puppeteer');
var expect = require('chai').expect;
var assert = require('chai').assert;
var reporter = require('cucumber-html-reporter');
var cucumberJunit = require('cucumber-junit');
var helpers = require('../runtime/helpers.js');
var edgePaths = require('edge-paths');

var platform = process.platform;
Expand All @@ -29,41 +26,6 @@ catch (e) {
var browserWidth = 1024;
var browserHeight = 768;

// import page objects (after global vars have been created)
if (global.pageObjectPath && fs.existsSync(global.pageObjectPath)) {

// require all page objects using camel case as object names
global.pageObjects = requireDir(global.pageObjectPath, { camelcase: true, recurse: true });
}


// import shared objects from multiple paths (after global vars have been created)
if (global.sharedObjectPaths && Array.isArray(global.sharedObjectPaths) && global.sharedObjectPaths.length > 0) {

var allDirs = {};

// first require directories into objects by directory
global.sharedObjectPaths.forEach(function (itemPath) {

if (fs.existsSync(itemPath)) {

var dir = requireDir(itemPath, { camelcase: true, recurse: true });

merge(allDirs, dir);
}
});

// if we managed to import some directories, expose them
if (Object.keys(allDirs).length > 0) {

// expose globally
global.sharedObjects = allDirs;
}
}

// add helpers
global.helpers = helpers;

/**
* log output to the console in a readable/visible format
* @returns {void}
Expand Down

0 comments on commit feb0cc5

Please sign in to comment.