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

feat: improve gpo command #3625

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions lib/command/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ module.exports = {
}
`;

const poModuleTemplateTS = `const { I } = inject();

export = {

// insert your locators and methods here
}
`;

const poClassTemplate = `const { I } = inject();

class {{name}} {
constructor() {
//insert your locators
// this.button = '#button'
}
// insert your methods here
}

// For inheritance
module.exports = new {{name}}();
export = {{name}};
`;

module.exports.pageObject = function (genPath, opts) {
const testsPath = getTestRoot(genPath);
const config = getConfig(testsPath);
Expand Down Expand Up @@ -110,7 +133,15 @@ module.exports.pageObject = function (genPath, opts) {
name: 'filename',
message: 'Where should it be stored',
default: answers => `./${kind}s/${answers.name}.${extension}`,
}]).then((result) => {
},
{
type: 'list',
name: 'objectType',
message: 'What is your preferred object type',
choices: ['module', 'class'],
default: 'module',
},
]).then((result) => {
const pageObjectFile = path.join(testsPath, result.filename);
const dir = path.dirname(pageObjectFile);
if (!fileExists(dir)) fs.mkdirSync(dir);
Expand All @@ -124,13 +155,25 @@ module.exports.pageObject = function (genPath, opts) {
}
actor = `require('${actorPath}')`;
}
if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return;

const name = lcfirst(result.name) + ucfirst(kind);
console.log(extension);
if (result.objectType === 'module' && extension === 'ts') {
if (!safeFileWrite(pageObjectFile, poModuleTemplateTS.replace('{{actor}}', actor))) return;
} else if (result.objectType === 'module' && extension === 'js') {
if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return;
} else if (result.objectType === 'class') {
const content = poClassTemplate.replace(/{{actor}}/g, actor).replace(/{{name}}/g, name);
if (!safeFileWrite(pageObjectFile, content)) return;
}

let data = readConfig(configFile);
config.include[name] = result.filename;

if (!data) throw Error('Config file is empty');
const currentInclude = `${data.match(/include:[\s\S][^\}]*/i)[0]}\n ${name}:${JSON.stringify(config.include[name])}`;

data = data.replace(/include:[\s\S][^\}]*/i, `${currentInclude}`);
data = data.replace(/include:[\s\S][^\}]*/i, `${currentInclude},`);

fs.writeFileSync(configFile, beautify(data), 'utf-8');

Expand Down