-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflat.js
125 lines (80 loc) · 3.31 KB
/
flat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var mkdirp = require("mkdirp");
var fs = require('fs-extra');
var path = require('path');
var sanitizeForFilename = require("sanitize-filename");
var beautify = require('js-beautify').js_beautify;
var varname = require('varname');
var generateActionList = require('./generateActionList').generateActionList;
var util = require('./util');
var ejs = require('ejs');
module.exports = function () {
var testData = this;
var topDirPath = process.cwd() + "/" + this.topDirName;
var commonDirPath = topDirPath + "/common";
var testPath = topDirPath + "/tests";
mkdirp.sync(topDirPath);
mkdirp.sync(testPath);
// generate skeleton directories
testData.folders.forEach((folderPath) => {
mkdirp.sync(topDirPath + "/tests/" + folderPath)
});
mkdirp.sync(topDirPath + "/common");
// Generate contents of tests
testData.tests.forEach((test) => {
test.content = generateTestCode(test, testData.components);
});
var componentCode = generateComponentCode(testData.components);
// Create file process
testData.tests.forEach((test) => {
fs.writeFileSync(testPath + "/" + test.folderPath + sanitizeForFilename(test.name) + ".js", test.content);
});
fs.writeFileSync(commonDirPath + "/components.js", componentCode);
// create shim assets via EJS
var driverString = fs.readFileSync(__dirname + "/./assets/chromeless-shims.ejs", 'utf8');
var driver = ejs.render(driverString, {cli: true, extension: false});
fs.writeFileSync(commonDirPath + "/chromeless-shims.js", driver);
process.exit();
};
function generateComponentCode(components) {
var generatedCompCode = "";
components.forEach((component) => {
var actionListCode = generateActionList(component.actions, components);
var cArguments = util.getKeyParamsForComponent(component);
generatedCompCode += `
chromeless.snapComponents.${varname.camelback(component.name)} = async function(${util.buildParamStringFromArray(cArguments)}) {
await this${actionListCode};
return new Chromeless({}, this);
}.bind(chromeless);
`;
});
var resultCode = `
const { Chromeless} = require('chromeless')
module.exports.bindComponents = function(chromeless) {
chromeless.snapComponents = {};
${generatedCompCode}
}
`;
return beautify(resultCode, { indent_size: 2 });
}
function generateTestCode(test, components) {
var generatedCode = generateActionList(test.actions, components);
var defaultLaunchUrlInfo = util.getDefaultLaunchUrlInfo(test.actions);
var code =
`
const { Chromeless } = require('chromeless')
const TIMEOUT = 10000;
const random= "" + parseInt(Math.random() * 1000000);
const random1 = "" + parseInt(Math.random() * 1000000);
const random2 = "" + parseInt(Math.random() * 1000000);
const random3 = "" + parseInt(Math.random() * 1000000);
async function run() {
const baseUrl = "${defaultLaunchUrlInfo.launchUrl}";
const chromeless = new Chromeless()
require('./../${test.pathToRoot}common/chromeless-shims.js').bindShims(chromeless);
await chromeless${generatedCode}
await chromeless.end();
};
run().catch(console.error.bind(console))
`;
return beautify(code, { indent_size: 2 });
}