Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate step from recorded script #276

Merged
merged 4 commits into from Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions lib/repl.js
@@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs-extra');
const util = require('util');
const { aEval } = require('./awaitEval');
const taiko = require('./taiko');
Expand Down Expand Up @@ -27,13 +27,13 @@ async function setVersionInfo() {
version = require('../package.json').version;
doc = require('./api.json');
browserVersion = require('../package.json').taiko.chromium_version;
} catch (_) {}
} catch (_) { }
displayTaiko();
}

const writer = w => output => {
if (util.isError(output)) return output.message;
else if (typeof(output) === 'object' && 'description' in output) {
else if (typeof (output) === 'object' && 'description' in output) {
output.description = symbols.pass + output.description;
return removeQuotes(util.inspect(output.description, { colors: true }), output.description);
} else return w(output);
Expand All @@ -55,6 +55,14 @@ function initCommands(repl) {
this.displayPrompt();
}
});
repl.defineCommand('step', {
help: 'Generate gauge steps from recorded script. (openBrowser and closeBrowser are not recorded as part of step)',
action(file) {
if (!file) console.log(step(true));
else writeStep(file);
this.displayPrompt();
}
});
repl.defineCommand('version', {
help: 'Prints version info',
action() {
Expand Down Expand Up @@ -82,7 +90,7 @@ function initCommands(repl) {
}

function code() {
if(commands[commands.length-1].includes('closeBrowser()')) commands.pop();
if (commands[commands.length - 1].includes('closeBrowser()')) commands.pop();
const text = commands.map(e => {
if (!e.endsWith(';')) e += ';';
return isTaikoFunc(e) ? ' await ' + e : '\t' + e;
Expand All @@ -91,7 +99,7 @@ function code() {
const importTaiko = cmds.length > 0 ? `const { ${cmds.join(', ')} } = require('taiko');\n\n` : '';
return importTaiko + `(async () => {
try {
${ text }
${ text}
} catch (e) {
console.error(e);
} finally {
Expand All @@ -100,14 +108,41 @@ ${ text }
})();`;
}

function step(withHooks = false, actions = commands) {
if (commands[0].includes('openBrowser()')) actions = actions.slice(0, -1);
if (commands[commands.length - 1].includes('closeBrowser()')) actions = actions.slice(1);
const actionsString = actions.map(e => {
if (!e.endsWith(';')) e += ';';
return isTaikoFunc(e) ? '\tawait ' + e : '\t' + e;
}).join('\n');

const hooks = 'beforeSuite(async () => { await openBrowser(); });' +
'\nafterSuite(async () => { await closeBrowser(); });\n';
const cmds = Object.keys(taikoCommands).filter((c) => {
return c !== 'openBrowser' && c !== 'closeBrowser';
});
const importTaiko = cmds.length > 0 ? `const { openBrowser, ${cmds.join(', ')}, closeBrowser } = require('taiko');\n` : '';
const step = !actionsString ? '' : `step("<insert step text here>", async function() {\n${actionsString}\n});\n`;
return !withHooks ? step : `${importTaiko}\n${hooks}\n${step}`;
}

function writeStep(file) {
if (fs.existsSync(file)) {
fs.appendFileSync(file, step());
} else {
fs.ensureFileSync(file);
fs.writeFileSync(file, step(true));
}
}

function initTaiko(repl) {
const openBrowser = taiko.openBrowser;
taiko.openBrowser = async (options = {}) => {
if (!options.headless) options.headless = false;
return await openBrowser(options);
};
for (let func in taiko) {
if (taiko[func].constructor.name === 'AsyncFunction') repl.context[func] = async function() {
if (taiko[func].constructor.name === 'AsyncFunction') repl.context[func] = async function () {
try {
lastStack = '';
let args = await Promise.all(Object.values(arguments));
Expand All @@ -120,7 +155,7 @@ function initTaiko(repl) {
util.inspect.styles.string = stringColor;
}
};
else repl.context[func] = function() {
else repl.context[func] = function () {
taikoCommands[func] = true;
return taiko[func].apply(this, arguments);
};
Expand Down Expand Up @@ -154,7 +189,7 @@ function displayUsageFor(name) {
function displayUsage() {
for (let k in taiko.metadata)
console.log(`
${removeQuotes(util.inspect(k, {colors: true}), k)}
${removeQuotes(util.inspect(k, { colors: true }), k)}
${taiko.metadata[k].join(', ')}`);
console.log(`
Run \`.api <name>\` for more info on a specific function. For Example: \`.api click\`.
Expand Down
28 changes: 25 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -39,6 +39,7 @@
"babylon": "^6.18.0",
"chrome-remote-interface": "^0.26.1",
"extract-zip": "^1.6.6",
"fs-extra": "^7.0.1",
"https-proxy-agent": "^2.2.1",
"progress": "^2.0.0",
"proxy-from-env": "^1.0.0",
Expand Down