ng-po-gen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/bin/ng-po-gen +2 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/main.d.ts +9 -0
- package/lib/main.js +96 -0
- package/lib/page-object-builder.d.ts +22 -0
- package/lib/page-object-builder.js +175 -0
- package/lib/template-parser.d.ts +25 -0
- package/lib/template-parser.js +71 -0
- package/lib/utils.d.ts +10 -0
- package/lib/utils.js +57 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Marian Devecka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Description
|
|
2
|
+
|
|
3
|
+
Allows to auto-generate page objects from Angular templates.
|
|
4
|
+
Uses special HTML attributes to create named bindings for page objects.
|
|
5
|
+
By default the attribute is prefixed by '_'.
|
|
6
|
+
Options may be passed as a value to the attribute to denote usage.
|
|
7
|
+
Currently available options are 'text', 'text-input', 'text-area', 'button', 'checkbox', 'radio-button', 'dropdown', 'select', 'class', 'class={class name}' and 'list'.
|
|
8
|
+
The generator will try to auto detect the type from the html element (e.g. `<input type='checkbox'>` will automatically be resolved as CheckboxObject).
|
|
9
|
+
Attribute names ending with '_list' will treated as list of items.
|
|
10
|
+
Imports and custom code is preserved by default.
|
|
11
|
+
Custom code needs to be separated by a single line comment.
|
|
12
|
+
If you are using Puppeteer you can use package `npm i puppeteer-page-objects` for the base classes.
|
|
13
|
+
Otherwise you need to implement base classes for PageObject, ObjectList, TextObject, ButtonObject, CheckboxObject, RadioButtonObject, DropdownObject, TextInputObject and TextAreaObject.
|
|
14
|
+
For more information, please check the test cases inside the test folder or example todo-list application with e2e tests.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm i ng-po-gen
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npx ng-po-gen ./src/app --target-dir ./e2e/page-objects
|
|
26
|
+
```
|
|
27
|
+
```
|
|
28
|
+
Angular Page Object Generator
|
|
29
|
+
|
|
30
|
+
Usage
|
|
31
|
+
|
|
32
|
+
ng-po-gen source-dir ...
|
|
33
|
+
|
|
34
|
+
Options
|
|
35
|
+
|
|
36
|
+
--help Print this usage guide.
|
|
37
|
+
--output-dir string Output directory.
|
|
38
|
+
If not specified source directory will be used.
|
|
39
|
+
--eol string End of line characters, either "unix" or "win".
|
|
40
|
+
Default is "unix".
|
|
41
|
+
--attribute-prefix string Prefix of template attribute used by generator.
|
|
42
|
+
Default is "_".
|
|
43
|
+
--selector-prefix string Optional prefix of angular component tags.
|
|
44
|
+
--lib string Import library name used for core page objects.
|
|
45
|
+
Default is "puppeteer-page-objects".
|
|
46
|
+
--overwrite Overwrite generated files instead of merge.
|
|
47
|
+
Default is false.
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Result
|
|
52
|
+
|
|
53
|
+
### Source Angular Template
|
|
54
|
+
```html
|
|
55
|
+
<header class="header" _header='class'></header>
|
|
56
|
+
<main class="items">
|
|
57
|
+
@if(!loading()){
|
|
58
|
+
<li class="item-wrapper">
|
|
59
|
+
@for(item of items();track item.id){
|
|
60
|
+
<todo-item class="item" [text]="item.text" [completed]="item.completed"
|
|
61
|
+
(textChange)="onTextChanged(item,$event)" (completedChange)="onCompletedChanged(item,$event)"
|
|
62
|
+
(delete)="onItemDelete(item)" _item_list></todo-item>
|
|
63
|
+
}
|
|
64
|
+
</li>
|
|
65
|
+
} @else {
|
|
66
|
+
<div class="loading" _loading_text>
|
|
67
|
+
LOADING...
|
|
68
|
+
</div>
|
|
69
|
+
}
|
|
70
|
+
</main>
|
|
71
|
+
<footer class="footer" _footer='class'></footer>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Generated Page Object
|
|
75
|
+
```typescript
|
|
76
|
+
import { TextObject, PageObject } from 'puppeteer-page-objects';
|
|
77
|
+
import { HeaderObject } from './header/header.po';
|
|
78
|
+
import { TodoItemObject } from './todo-item/todo-item.po';
|
|
79
|
+
import { FooterObject } from './footer/footer.po';
|
|
80
|
+
|
|
81
|
+
export class TodoListObject extends PageObject {
|
|
82
|
+
get header() { return this.createChild(HeaderObject, 'div > header[_header]'); }
|
|
83
|
+
get itemList() { return this.createList(TodoItemObject, 'div > main > li > todo-item[_item_list]'); }
|
|
84
|
+
get loadingText() { return this.createChild(TextObject, 'div > main > div[_loading_text]'); }
|
|
85
|
+
get footer() { return this.createChild(FooterObject, 'div > footer[_footer]'); }
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Example Test
|
|
90
|
+
```typescript
|
|
91
|
+
...
|
|
92
|
+
const app = new TodoListObject(new ObjectContext(page, ['todo-list']));
|
|
93
|
+
await waitUntil(async () => !(await app.loadingText.visible));
|
|
94
|
+
await waitUntil(async () => (await app.itemList.length) > 0);
|
|
95
|
+
...
|
|
96
|
+
await app.header.newTaskName.type('wash car\n');
|
|
97
|
+
await retry([
|
|
98
|
+
() => expect(app.itemList.length).resolves.toEqual(4),
|
|
99
|
+
() => expect(app.footer.itemsLeft.text).resolves.toEqual('3 items left'),
|
|
100
|
+
() => expect(app.itemList.map(i => i.nameText.text)).resolves.toStrictEqual(['buy bannanas', 'do laundry', 'order new sofa', 'wash car']),
|
|
101
|
+
() => expect(app.itemList.map(i => i.completed)).resolves.toStrictEqual([false, true, false, false]),
|
|
102
|
+
]);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For more information check the full todo-list example app with e2e tests in the GitHub repository.
|
package/bin/ng-po-gen
ADDED
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/main.d.ts
ADDED
package/lib/main.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { findComponents } from './utils.js';
|
|
2
|
+
import { TemplateParser } from './template-parser.js';
|
|
3
|
+
import { PageObjectBuilder } from './page-object-builder.js';
|
|
4
|
+
import commandLineArgs from 'command-line-args';
|
|
5
|
+
import commandLineUsage from 'command-line-usage';
|
|
6
|
+
import pc from 'picocolors';
|
|
7
|
+
const cmdLineUsage = [
|
|
8
|
+
{
|
|
9
|
+
header: 'Angular Page Object Generator',
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
header: 'Usage',
|
|
13
|
+
content: [
|
|
14
|
+
'ng-po-gen {underline source-dir} ...',
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
header: 'Options',
|
|
19
|
+
optionList: [
|
|
20
|
+
{
|
|
21
|
+
name: 'help',
|
|
22
|
+
description: 'Print this usage guide.',
|
|
23
|
+
typeLabel: ' '
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'output-dir',
|
|
27
|
+
description: 'Output directory. \nIf not specified source directory will be used.'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: 'eol',
|
|
31
|
+
description: 'End of line characters, either "unix" or "win". Default is "unix".'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'attribute-prefix',
|
|
35
|
+
description: 'Prefix of template attribute used by generator. Default is "_".'
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'selector-prefix',
|
|
39
|
+
description: 'Optional prefix of angular component tags.'
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'lib',
|
|
43
|
+
description: 'Import library name used for core page objects. Default is "puppeteer-page-objects".'
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'overwrite',
|
|
47
|
+
type: Boolean,
|
|
48
|
+
description: 'Overwrite generated files instead of merge. Default is false.'
|
|
49
|
+
},
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
async function main() {
|
|
54
|
+
if (process.argv.length < 3 || ['--help', '-h'].includes(process.argv[2])) {
|
|
55
|
+
const usage = commandLineUsage(cmdLineUsage);
|
|
56
|
+
console.log(usage);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const opt = commandLineArgs([
|
|
60
|
+
{ name: 'source-dir', defaultOption: true },
|
|
61
|
+
{ name: 'output-dir' },
|
|
62
|
+
{ name: 'eol' },
|
|
63
|
+
{ name: 'attribute-prefix' },
|
|
64
|
+
{ name: 'selector-prefix' },
|
|
65
|
+
{ name: 'lib' },
|
|
66
|
+
{ name: 'overwrite', type: Boolean },
|
|
67
|
+
], { camelCase: true });
|
|
68
|
+
if (opt.eol != null && !['unix', 'win'].includes(opt.eol))
|
|
69
|
+
throw new Error(`unsupported eol '${opt.eol}'`);
|
|
70
|
+
const parser = new TemplateParser({ attributePrefix: opt.attributePrefix ?? '_' });
|
|
71
|
+
const builder = new PageObjectBuilder({
|
|
72
|
+
outputDir: opt.outputDir ?? opt.sourceDir,
|
|
73
|
+
eol: opt.eol ?? 'unix',
|
|
74
|
+
selectorPrefix: opt.selectorPrefix ?? '',
|
|
75
|
+
lib: opt.lib ?? 'puppeteer-page-objects',
|
|
76
|
+
overwrite: opt.overwrite,
|
|
77
|
+
});
|
|
78
|
+
const comps = findComponents(opt.sourceDir);
|
|
79
|
+
const compMap = new Map(comps.map(c => [c.className, c]));
|
|
80
|
+
for (const comp of comps) {
|
|
81
|
+
try {
|
|
82
|
+
const root = parser.parse(comp);
|
|
83
|
+
console.log(pc.greenBright(`• ${comp.name}`));
|
|
84
|
+
await builder.createPageObject(root, compMap);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (!Array.isArray(err))
|
|
88
|
+
throw err;
|
|
89
|
+
console.log(pc.redBright(`• ${comp.name}`));
|
|
90
|
+
for (const error of err) {
|
|
91
|
+
console.log(pc.whiteBright(error.msg));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
main().catch(err => console.error(err));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ComponentInfo } from './utils.js';
|
|
2
|
+
import { NodeInfo } from './template-parser.js';
|
|
3
|
+
export interface PageObjectBuilderOptions {
|
|
4
|
+
outputDir: string;
|
|
5
|
+
eol: 'unix' | 'win';
|
|
6
|
+
selectorPrefix: string;
|
|
7
|
+
lib: string;
|
|
8
|
+
overwrite?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare class PageObjectBuilder {
|
|
11
|
+
private opt;
|
|
12
|
+
readonly eolChar: string;
|
|
13
|
+
constructor(opt: PageObjectBuilderOptions);
|
|
14
|
+
createPageObject(root: NodeInfo, componentRef: Map<string, ComponentInfo>): Promise<void>;
|
|
15
|
+
getSourceCode(pageObjectCode: string | null, root: NodeInfo, componentRef: Map<string, ComponentInfo>): Promise<string>;
|
|
16
|
+
private createClass;
|
|
17
|
+
private parsePageObject;
|
|
18
|
+
private getUsedClasses;
|
|
19
|
+
private getTargetClassInfo;
|
|
20
|
+
private createRelativePath;
|
|
21
|
+
private comparePathItems;
|
|
22
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { TypescriptParser, NamedImport, ClassDeclaration } from 'typescript-parser';
|
|
2
|
+
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { convertName, capitalize, ensureDir } from './utils.js';
|
|
5
|
+
import htmlTags from 'html-tags';
|
|
6
|
+
export class PageObjectBuilder {
|
|
7
|
+
opt;
|
|
8
|
+
eolChar;
|
|
9
|
+
constructor(opt) {
|
|
10
|
+
this.opt = opt;
|
|
11
|
+
this.eolChar = (opt.eol === 'win') ? '\r\n' : '\n';
|
|
12
|
+
}
|
|
13
|
+
async createPageObject(root, componentRef) {
|
|
14
|
+
const ref = componentRef.get(root.className);
|
|
15
|
+
const dir = join(this.opt.outputDir, ...ref.pathItems);
|
|
16
|
+
const path = join(dir, `${ref.name}.po.ts`);
|
|
17
|
+
const sourceCode = (!this.opt.overwrite && existsSync(path)) ? readFileSync(path, 'utf8') : null;
|
|
18
|
+
const res = await this.getSourceCode(sourceCode, root, componentRef);
|
|
19
|
+
ensureDir(dir);
|
|
20
|
+
writeFileSync(path, res);
|
|
21
|
+
}
|
|
22
|
+
async getSourceCode(pageObjectCode, root, componentRef) {
|
|
23
|
+
const ref = componentRef.get(root.className);
|
|
24
|
+
let codeInfo = { importedSymbols: new Set(), importedCode: [], keepCode: new Map() };
|
|
25
|
+
if (pageObjectCode != null) {
|
|
26
|
+
codeInfo = await this.parsePageObject(pageObjectCode);
|
|
27
|
+
}
|
|
28
|
+
let res = '';
|
|
29
|
+
const usedClasses = this.getUsedClasses(root);
|
|
30
|
+
const coreImports = Array.from(usedClasses.core).concat('PageObject').filter(i => !codeInfo.importedSymbols.has(i));
|
|
31
|
+
const otherImports = Array.from(usedClasses.imported).filter(i => !codeInfo.importedSymbols.has(i));
|
|
32
|
+
for (const code of codeInfo.importedCode) {
|
|
33
|
+
res += code + this.eolChar;
|
|
34
|
+
}
|
|
35
|
+
if (coreImports.length > 0) {
|
|
36
|
+
res += `import { ${coreImports.join(', ')} } from '${this.opt.lib}';` + this.eolChar;
|
|
37
|
+
}
|
|
38
|
+
for (const name of otherImports) {
|
|
39
|
+
const cref = componentRef.get(name);
|
|
40
|
+
if (cref != null) {
|
|
41
|
+
const relPath = this.createRelativePath(ref.pathItems, cref.pathItems);
|
|
42
|
+
res += `import { ${name} } from '${relPath}/${cref.name}.po';` + this.eolChar;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const visitNodes = (nodeInfo) => {
|
|
46
|
+
if (nodeInfo.children.length > 0) {
|
|
47
|
+
res += this.eolChar;
|
|
48
|
+
res += this.createClass(nodeInfo, codeInfo.keepCode.get(nodeInfo.className));
|
|
49
|
+
}
|
|
50
|
+
for (const child of nodeInfo.children) {
|
|
51
|
+
visitNodes(child);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
visitNodes(root);
|
|
55
|
+
return res;
|
|
56
|
+
}
|
|
57
|
+
createClass(info, mergeCode) {
|
|
58
|
+
let res = `export class ${info.className} extends PageObject {` + this.eolChar;
|
|
59
|
+
for (const child of info.children) {
|
|
60
|
+
const isList = (child.selector && child.selector.endsWith('_list')) || child.options.includes('list');
|
|
61
|
+
const funcName = (isList) ? 'this.createList' : 'this.createChild';
|
|
62
|
+
const name = (isList) ? `${child.propName}List` : child.propName;
|
|
63
|
+
const selector = child.selectorPath.join(' > ').trim();
|
|
64
|
+
const className = this.getTargetClassInfo(child)[0];
|
|
65
|
+
res += ` get ${name}() { return ${funcName}(${className}, '${selector}'); }` + this.eolChar;
|
|
66
|
+
}
|
|
67
|
+
if (mergeCode != null) {
|
|
68
|
+
res += mergeCode + this.eolChar;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
res += "}" + this.eolChar;
|
|
72
|
+
}
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
async parsePageObject(sourceCode) {
|
|
76
|
+
const parser = new TypescriptParser();
|
|
77
|
+
const comments = Array.from(sourceCode.matchAll(/[ \t]*\/\/.*\r?\n/g));
|
|
78
|
+
const codeInfo = await parser.parseSource(sourceCode);
|
|
79
|
+
const importedSymbols = new Set();
|
|
80
|
+
const importedCode = [];
|
|
81
|
+
for (const item of codeInfo.imports) {
|
|
82
|
+
if (item instanceof NamedImport) {
|
|
83
|
+
for (const specifier of item.specifiers) {
|
|
84
|
+
importedSymbols.add(specifier.alias ?? specifier.specifier);
|
|
85
|
+
}
|
|
86
|
+
importedCode.push(sourceCode.substring(item.start, item.end));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const keepCode = new Map();
|
|
90
|
+
for (const decl of codeInfo.declarations) {
|
|
91
|
+
if (decl instanceof ClassDeclaration) {
|
|
92
|
+
const firstClassComment = comments.find(c => c.index >= decl.start && c.index <= decl.end);
|
|
93
|
+
if (firstClassComment != null) {
|
|
94
|
+
keepCode.set(decl.name, sourceCode.substring(firstClassComment.index, decl.end));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { importedSymbols, importedCode, keepCode };
|
|
99
|
+
}
|
|
100
|
+
getUsedClasses(root) {
|
|
101
|
+
const core = new Set();
|
|
102
|
+
const imported = new Set();
|
|
103
|
+
const visitNodes = (nodeInfo) => {
|
|
104
|
+
const [targetClass, origin] = this.getTargetClassInfo(nodeInfo);
|
|
105
|
+
if (origin === 'core') {
|
|
106
|
+
core.add(targetClass);
|
|
107
|
+
}
|
|
108
|
+
else if (origin === 'import') {
|
|
109
|
+
imported.add(targetClass);
|
|
110
|
+
}
|
|
111
|
+
for (const child of nodeInfo.children) {
|
|
112
|
+
visitNodes(child);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
visitNodes(root);
|
|
116
|
+
return { core, imported };
|
|
117
|
+
}
|
|
118
|
+
getTargetClassInfo(info) {
|
|
119
|
+
const classMap = new Map([
|
|
120
|
+
['text', 'TextObject'],
|
|
121
|
+
['text-input', 'TextInputObject'],
|
|
122
|
+
['text-area', 'TextAreaObject'],
|
|
123
|
+
['button', 'ButtonObject'],
|
|
124
|
+
['checkbox', 'CheckboxObject'],
|
|
125
|
+
['radio-button', 'RadioButtonObject'],
|
|
126
|
+
['dropdown', 'DropdownObject'],
|
|
127
|
+
['select', 'DropdownObject'],
|
|
128
|
+
]);
|
|
129
|
+
const classAlias = info.options.find(opt => classMap.has(opt));
|
|
130
|
+
if (classAlias != null)
|
|
131
|
+
return [classMap.get(classAlias), 'core'];
|
|
132
|
+
const classOption = info.options.find(o => o.startsWith('class='));
|
|
133
|
+
if (classOption != null) {
|
|
134
|
+
const className = classOption.substring(6).trim();
|
|
135
|
+
return [className, 'import'];
|
|
136
|
+
}
|
|
137
|
+
if (!htmlTags.includes(info.nodeType) || info.options.includes('class')) {
|
|
138
|
+
const prefix = this.opt.selectorPrefix;
|
|
139
|
+
return [capitalize(convertName(info.nodeType.substring(prefix.length))) + 'Object', 'import'];
|
|
140
|
+
}
|
|
141
|
+
if (info.children.length === 0) {
|
|
142
|
+
if (info.nodeType === 'input') {
|
|
143
|
+
if (info.typeAttr === 'checkbox')
|
|
144
|
+
return ['CheckboxObject', 'core'];
|
|
145
|
+
if (info.typeAttr === 'radio')
|
|
146
|
+
return ['RadioButtonObject', 'core'];
|
|
147
|
+
return ['TextInputObject', 'core'];
|
|
148
|
+
}
|
|
149
|
+
if (info.nodeType === 'button') {
|
|
150
|
+
return ['ButtonObject', 'core'];
|
|
151
|
+
}
|
|
152
|
+
if (info.nodeType === 'textarea') {
|
|
153
|
+
return ['TextAreaObject', 'core'];
|
|
154
|
+
}
|
|
155
|
+
if (info.nodeType === 'select') {
|
|
156
|
+
return ['DropdownObject', 'core'];
|
|
157
|
+
}
|
|
158
|
+
return ['TextObject', 'core'];
|
|
159
|
+
}
|
|
160
|
+
return [info.className, 'new'];
|
|
161
|
+
}
|
|
162
|
+
createRelativePath(path1, path2) {
|
|
163
|
+
const count = this.comparePathItems(path1, path2);
|
|
164
|
+
const diff = path1.length - count;
|
|
165
|
+
const prefix = (diff === 0) ? './' : '../'.repeat(diff);
|
|
166
|
+
return prefix + path2.slice(count).join('/');
|
|
167
|
+
}
|
|
168
|
+
comparePathItems(path1, path2) {
|
|
169
|
+
let i = 0;
|
|
170
|
+
while (path1.length > i && path2.length > i && path1[i] === path2[i]) {
|
|
171
|
+
i++;
|
|
172
|
+
}
|
|
173
|
+
return i;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Element } from 'angular-html-parser/lib/compiler/src/ml_parser/ast.js';
|
|
2
|
+
import { ComponentInfo } from './utils.js';
|
|
3
|
+
export interface NodeInfo {
|
|
4
|
+
propName: string;
|
|
5
|
+
className: string;
|
|
6
|
+
nodeType: string;
|
|
7
|
+
typeAttr?: string;
|
|
8
|
+
namePath: string[];
|
|
9
|
+
selector?: string;
|
|
10
|
+
selectorPath: string[];
|
|
11
|
+
options: string[];
|
|
12
|
+
children: NodeInfo[];
|
|
13
|
+
node?: Element;
|
|
14
|
+
}
|
|
15
|
+
export interface TemplateParserOptions {
|
|
16
|
+
attributePrefix: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class TemplateParser {
|
|
19
|
+
private opt;
|
|
20
|
+
constructor(opt: TemplateParserOptions);
|
|
21
|
+
parse(info: ComponentInfo): NodeInfo;
|
|
22
|
+
parseTemplate(templateName: string, templateText: string): NodeInfo;
|
|
23
|
+
private parseNodes;
|
|
24
|
+
private getClassName;
|
|
25
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { parse } from 'angular-html-parser';
|
|
2
|
+
import { Element } from 'angular-html-parser/lib/compiler/src/ml_parser/ast.js';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { convertName, capitalize } from './utils.js';
|
|
5
|
+
export class TemplateParser {
|
|
6
|
+
opt;
|
|
7
|
+
constructor(opt) {
|
|
8
|
+
this.opt = opt;
|
|
9
|
+
}
|
|
10
|
+
parse(info) {
|
|
11
|
+
const contents = readFileSync(info.path, 'utf8');
|
|
12
|
+
return this.parseTemplate(info.name, contents);
|
|
13
|
+
}
|
|
14
|
+
parseTemplate(templateName, templateText) {
|
|
15
|
+
const { rootNodes, errors } = parse(templateText);
|
|
16
|
+
if (errors.length > 0)
|
|
17
|
+
throw errors;
|
|
18
|
+
const nodeName = convertName(templateName);
|
|
19
|
+
const root = {
|
|
20
|
+
propName: nodeName,
|
|
21
|
+
className: this.getClassName([nodeName]),
|
|
22
|
+
nodeType: 'body',
|
|
23
|
+
namePath: [nodeName],
|
|
24
|
+
selectorPath: [],
|
|
25
|
+
options: [],
|
|
26
|
+
children: [],
|
|
27
|
+
};
|
|
28
|
+
this.parseNodes(root, rootNodes);
|
|
29
|
+
return root;
|
|
30
|
+
}
|
|
31
|
+
parseNodes(parent, nodes, path = []) {
|
|
32
|
+
for (const node of nodes) {
|
|
33
|
+
if (node instanceof Element) {
|
|
34
|
+
const prefix = this.opt.attributePrefix;
|
|
35
|
+
const idAttr = node.attrs.find(attr => attr.name.startsWith(prefix));
|
|
36
|
+
const typeAttr = node.attrs.find(attr => attr.name === 'type');
|
|
37
|
+
if (node.name === 'ng-container' || idAttr?.name === '__ignore') {
|
|
38
|
+
this.parseNodes(parent, node.children, path);
|
|
39
|
+
}
|
|
40
|
+
else if (idAttr != null) {
|
|
41
|
+
const name = convertName(idAttr.name.substring(prefix.length).replace(/_list$/, ''));
|
|
42
|
+
const selector = `${node.name}[${idAttr.name}]`;
|
|
43
|
+
const options = (idAttr.value ?? '').split(',').map(i => i.trim());
|
|
44
|
+
const namePath = [...parent.namePath, name];
|
|
45
|
+
const item = {
|
|
46
|
+
propName: name,
|
|
47
|
+
className: this.getClassName(namePath),
|
|
48
|
+
nodeType: node.name,
|
|
49
|
+
typeAttr: typeAttr?.value,
|
|
50
|
+
selector: idAttr.name,
|
|
51
|
+
options: options,
|
|
52
|
+
node: node,
|
|
53
|
+
namePath: namePath,
|
|
54
|
+
selectorPath: [...path, selector],
|
|
55
|
+
children: [],
|
|
56
|
+
};
|
|
57
|
+
parent.children.push(item);
|
|
58
|
+
this.parseNodes(item, node.children, ['']);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const selector = node.name;
|
|
62
|
+
this.parseNodes(parent, node.children, [...path, selector]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
getClassName(namePath) {
|
|
68
|
+
const name = namePath.map(p => capitalize(p)).join('');
|
|
69
|
+
return name + 'Object';
|
|
70
|
+
}
|
|
71
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface ComponentInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
className: string;
|
|
4
|
+
pathItems: string[];
|
|
5
|
+
path: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function findComponents(basePath: string): ComponentInfo[];
|
|
8
|
+
export declare function capitalize(name: string): string;
|
|
9
|
+
export declare function convertName(name: string): string;
|
|
10
|
+
export declare function ensureDir(path: string): void;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readdirSync, mkdirSync, existsSync, lstatSync } from 'fs';
|
|
2
|
+
import { join, parse } from 'path';
|
|
3
|
+
export function findComponents(basePath) {
|
|
4
|
+
const result = [];
|
|
5
|
+
const visit = (basePath, pathItems) => {
|
|
6
|
+
const path = join(basePath, ...pathItems);
|
|
7
|
+
const items = readdirSync(path);
|
|
8
|
+
for (const item of items) {
|
|
9
|
+
const { ext, name } = parse(item);
|
|
10
|
+
const filePath = join(path, item);
|
|
11
|
+
const stats = lstatSync(filePath);
|
|
12
|
+
if (stats.isDirectory()) {
|
|
13
|
+
visit(basePath, [...pathItems, item]);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (stats.isFile() && ext === '.html') {
|
|
17
|
+
const componentName = name.replace(/\.component$/, '');
|
|
18
|
+
const info = {
|
|
19
|
+
name: componentName,
|
|
20
|
+
className: capitalize(convertName(componentName)) + 'Object',
|
|
21
|
+
pathItems: pathItems,
|
|
22
|
+
path: filePath,
|
|
23
|
+
};
|
|
24
|
+
result.push(info);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
visit(basePath, []);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
export function capitalize(name) {
|
|
32
|
+
return (name.length === 0) ? name : name[0].toUpperCase() + name.substring(1);
|
|
33
|
+
}
|
|
34
|
+
export function convertName(name) {
|
|
35
|
+
let result = "";
|
|
36
|
+
let cap = false;
|
|
37
|
+
for (let i = 0; i < name.length; i++) {
|
|
38
|
+
const ch = name[i];
|
|
39
|
+
if (ch === '_' || ch === '-') {
|
|
40
|
+
cap = (i !== 0);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (cap) {
|
|
44
|
+
result += ch.toUpperCase();
|
|
45
|
+
cap = false;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
result += ch;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
export function ensureDir(path) {
|
|
54
|
+
if (!existsSync(path)) {
|
|
55
|
+
mkdirSync(path, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ng-po-gen",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Marian Devecka",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Allows to auto-generate page objects from Angular templates.",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/mdevecka/ng-po-gen"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"typescript",
|
|
13
|
+
"angular",
|
|
14
|
+
"testing",
|
|
15
|
+
"page objects"
|
|
16
|
+
],
|
|
17
|
+
"main": "./lib/index.js",
|
|
18
|
+
"types": "./lib/index.d.ts",
|
|
19
|
+
"bin": {
|
|
20
|
+
"ng-po-gen": "./bin/ng-po-gen"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"angular-html-parser": "^9.2.0",
|
|
24
|
+
"command-line-args": "^6.0.1",
|
|
25
|
+
"command-line-usage": "^7.0.3",
|
|
26
|
+
"html-tags": "^5.0.0",
|
|
27
|
+
"picocolors": "^1.1.1",
|
|
28
|
+
"typescript-parser": "^2.6.1"
|
|
29
|
+
},
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"files": [
|
|
32
|
+
"**/*.js",
|
|
33
|
+
"**/*.d.ts"
|
|
34
|
+
]
|
|
35
|
+
}
|