Skip to content

Commit

Permalink
Merge pull request #29 from DP6/configAdapt
Browse files Browse the repository at this point in the history
Config adapt
  • Loading branch information
vandersonmaroni authored Jun 22, 2021
2 parents 70dc7f6 + 9c429b6 commit aeb471c
Showing 1 changed file with 112 additions and 107 deletions.
219 changes: 112 additions & 107 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,115 +31,120 @@ if (!(export_opt === 'xlsx' || export_opt === 'txt')) {
// Defining export extension based on export option extracted from command line params.
let filename = `${__dirname}/results/results_${new Date().getTime()}.${export_opt}`;

const config = require(`./config/${config_file}`);
const schema = require(`./schema/${config.schema_name}`);

// let startChromeInDebugMode = async () => {
// let operatingSystem = os.platform();
// if (operatingSystem === "linux") {
// await exec(
// "google-chrome --remote-debugging-port=9222 --user-data-dir=remote-profile"
// );
// } else if (operatingSystem === "win32") {
// await exec(
// "start chrome --remote-debugging-port=9222 --user-data-dir=remote-profile"
// );
// }

// return new Promise((res) => {
// setTimeout((_) => {
// req("http://localhost:9222/json/version")
// .then((result) => {
// res(JSON.parse(result).webSocketDebuggerUrl);
// })
// .catch((err) =>
// console.log(
// "Error while trying to pick the Debugger URL. Did you open the chrome instance with debugging port?"
// )
// );
// }, 5000);
// });
// };

(async () => {
// const browser = await puppeteer.connect({
// browserWSEndpoint: await startChromeInDebugMode(),
// });
const browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();

// .pages
// .then((res) => res[0])
// .catch((err) => new Error(err));

if (config.gtmPreviewModeURL) await page.goto(config.gtmPreviewModeURL);

if (config.mobile_enabled === true) {
await page.emulate(iPhone);
} else {
await page.setViewport({ width: 1366, height: 768 });
}

await page.exposeFunction('bowser', (event) => {
bowserjr.validate(schema, event, function (result) {
resultsArray.push(result[0]);
const configFile = require(`./config/${config_file}`);
configFile.validator.forEach(async (config) => {
const schema = require(`./schema/${config.schema_name[0]}`);
// let startChromeInDebugMode = async () => {
// let operatingSystem = os.platform();
// if (operatingSystem === "linux") {
// await exec(
// "google-chrome --remote-debugging-port=9222 --user-data-dir=remote-profile"
// );
// } else if (operatingSystem === "win32") {
// await exec(
// "start chrome --remote-debugging-port=9222 --user-data-dir=remote-profile"
// );
// }

// return new Promise((res) => {
// setTimeout((_) => {
// req("http://localhost:9222/json/version")
// .then((result) => {
// res(JSON.parse(result).webSocketDebuggerUrl);
// })
// .catch((err) =>
// console.log(
// "Error while trying to pick the Debugger URL. Did you open the chrome instance with debugging port?"
// )
// );
// }, 5000);
// });
// };

(async () => {
// const browser = await puppeteer.connect({
// browserWSEndpoint: await startChromeInDebugMode(),
// });
const browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();

// .pages
// .then((res) => res[0])
// .catch((err) => new Error(err));

if (config.gtmPreviewModeURL) await page.goto(config.gtmPreviewModeURL);

if (config.mobile_enabled === true) {
await page.emulate(iPhone);
} else {
await page.setViewport({ width: 1366, height: 768 });
}

await page.exposeFunction('bowser', (event) => {
bowserjr.validate(schema, event, function (result) {
resultsArray.push(result[0]);
});
});
});

async function runAfterGTMDebug() {
page.on('load', async () => {
await page.waitFor(5000);
if (page.url() === config.url) {
let path = `Url validating: ${page.url()}\n`;
//doc.text(path, 10, 10);
await page.evaluate(() => {
//Validate first hits.
window.dataLayer.forEach((elem) => {
bowser(elem);
});
window.dataLayer.push_c = window.dataLayer.push;
window.dataLayer.push = function (obj) {
window.dataLayer.push_c(obj);
bowser(obj);
};
});
if (config.browserClose) {
config.time ? await page.waitFor(config.time) : await page.waitFor(0);
await browser.close();
}
} else {
let path = `Path : ${page.url()}\n`;
//doc.text(path, 10, 10);
}
});

await page.goto(config.url);
}

await runAfterGTMDebug();

await fs.appendFile(filename, '', () => {});

let cleanupEval = () => {
console.log('Realizing last eval');
bowserjr.validate(schema, {}, function (result) {
resultsArray.push(result[0]);
});
resultsArray.forEach((resultObject) => {
if (resultObject) {
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/(\r\n|\n|\r)/gm, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/\s/g, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.split(',').join(' ');
fs.appendFileSync(
filename,
`${resultObject.status}, ${resultObject.message}, ${resultObject.dataLayerObject}\n`,
(err) => {
async function runAfterGTMDebug() {
page.on('load', async () => {
await page.waitFor(2000);
if (page.url() === config.url) {
await fs.appendFileSync(filename, `Url validating: ${page.url()}\n`, (err) => {
if (err) throw err;
});
//doc.text(path, 10, 10);
await page.evaluate(() => {
//Validate first hits.
window.dataLayer.forEach((elem) => {
bowser(elem);
});
window.dataLayer.push_c = window.dataLayer.push;
window.dataLayer.push = function (obj) {
window.dataLayer.push_c(obj);
bowser(obj);
};
});
if (config.browserClose) {
config.time ? await page.waitFor(config.time) : await page.waitFor(0);
await browser.close();
}
);
}
});
};
} else {
await fs.appendFileSync(filename, `Path : ${page.url()}\n`, (err) => {
if (err) throw err;
});
//doc.text(path, 10, 10);
}
});

await page.goto(config.url);
}

await runAfterGTMDebug();

await fs.appendFile(filename, '', () => {});

let cleanupEval = () => {
console.log('Realizing last eval');
bowserjr.validate(schema, {}, function (result) {
resultsArray.push(result[0]);
});
resultsArray.forEach((resultObject) => {
if (resultObject) {
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/(\r\n|\n|\r)/gm, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.replace(/\s/g, '');
resultObject.dataLayerObject = resultObject.dataLayerObject.split(',').join(' ');
fs.appendFileSync(
filename,
`${resultObject.status}, ${resultObject.message}, ${resultObject.dataLayerObject}\n`,
(err) => {
if (err) throw err;
}
);
}
});
};

process.on('exit', cleanupEval);
})();
await process.on('exit', cleanupEval);
})();
});

0 comments on commit aeb471c

Please sign in to comment.