@agnos-ui/base-po 0.4.0-next.0 → 0.4.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/.eslintrc.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["../.eslintrc.json"]
3
+ }
package/lib/base.po.ts ADDED
@@ -0,0 +1,159 @@
1
+ import type {Page, Locator} from '@playwright/test';
2
+
3
+ function isLocator(locator: Page | Locator): locator is Locator {
4
+ return 'waitFor' in locator;
5
+ }
6
+
7
+ /**
8
+ * Base class to be extended for page objects.
9
+ * The purpose of this architecture is to standardize the way locators are created in the page object, to be able to target precisely the dom elements you want to interact with.
10
+ *
11
+ * The constructor takes two parameters:
12
+ *
13
+ * - `container: Page | Locator` is the container locator from which other locators are built
14
+ * - `index: number | null = null` is the nth index component found in the container locator
15
+ *
16
+ * Page objects must usually override the `getComponentSelector()` method to return the html selector of the component
17
+ *
18
+ * @example
19
+ *
20
+ * ```html
21
+ * <!-- page.html -->
22
+ * <div class="component1">
23
+ * <div class="component2">
24
+ * <button>Click me</button>
25
+ * </div>
26
+ * <div class="component2">
27
+ * <button>Click me</button>
28
+ * </div>
29
+ * </div>
30
+ * ```
31
+ *
32
+ * ```typescript
33
+ * // component1.po.ts
34
+ * export Component1Page extends BasePo {
35
+ * getComponentSelector(): string {
36
+ * return 'div.component1';
37
+ * }
38
+ * }
39
+ *
40
+ * // component2.po.ts
41
+ * export Component2Page extends BasePo {
42
+ * getComponentSelector(): string {
43
+ * return 'div.component2';
44
+ * }
45
+ *
46
+ * // Button locator in component2
47
+ * get locatorButton() {
48
+ * return this.locatorRoot.locator('button');
49
+ * }
50
+ * }
51
+ *
52
+ * // test.spec.ts
53
+ * test('should click', ({page}) => {
54
+ *
55
+ * // will manage the component1 section
56
+ * const component1Po = new Component1Page(page);
57
+ *
58
+ * // click on the button of the first component2 of component1
59
+ * const firstComponent2Po = new Component2Page(component1Po.locatorRoot);
60
+ * await firstComponent2Po.locatorButton.click();
61
+ *
62
+ * // click on the button of the second component2 of component1
63
+ * const secondComponent2Po = new Component2Page(component1Po.locatorRoot, 1);
64
+ * await secondComponent2Po.locatorButton.click();
65
+ *
66
+ * });
67
+ *
68
+ * ```
69
+ *
70
+ */
71
+ export class BasePO {
72
+ readonly #container: Page | Locator;
73
+ readonly #index: number | null;
74
+ #locatorRoot: Locator | undefined;
75
+
76
+ /**
77
+ * Playwright page object from which the page object has been created.
78
+ * It can be convenient for situation where you can't work (internally) with the locatorRoot
79
+ */
80
+ protected _page: Page;
81
+
82
+ /**
83
+ *
84
+ * @param container - Container locator (page if there is no container) where the component is located
85
+ * @param index - If you have multiple components **inside the container**, you must provide the index to select the component to work with. You can omit this parameter if you have a single component.
86
+ */
87
+ constructor(container: Page | Locator, index: number | null = null) {
88
+ this.#container = container;
89
+ this.#index = index;
90
+ this._page = isLocator(container) ? container.page() : container;
91
+ }
92
+
93
+ /**
94
+ * The root locator of your page object.
95
+ * It can be used to be chain with internal locators.
96
+ *
97
+ * It is built with the provided `container` from the constructor, the `getComponentSelector` (one of them must be provided, at least) and the optional `index` parameter from the contructor.
98
+ *
99
+ * @example
100
+ *
101
+ * ```typescript
102
+ *
103
+ * // Button locator in this component
104
+ * get locatorButton() {
105
+ * return this.locatorRoot().locator('button').nth(0);
106
+ * }
107
+ * ```
108
+ */
109
+ get locatorRoot(): Locator {
110
+ let locatorRoot = this.#locatorRoot;
111
+ if (!locatorRoot) {
112
+ const componentSelector = this.getComponentSelector();
113
+ let tmpLocatorRoot = this.#container;
114
+
115
+ if (componentSelector) {
116
+ tmpLocatorRoot = tmpLocatorRoot.locator(componentSelector);
117
+
118
+ // Use the nth engine : https://playwright.dev/docs/selectors#n-th-element-selector
119
+ const index = this.#index;
120
+ if (index !== null) {
121
+ tmpLocatorRoot = tmpLocatorRoot.nth(index);
122
+ }
123
+ }
124
+
125
+ locatorRoot = isLocator(tmpLocatorRoot) ? tmpLocatorRoot : tmpLocatorRoot.locator('html');
126
+ this.#locatorRoot = locatorRoot;
127
+ }
128
+ return locatorRoot;
129
+ }
130
+
131
+ /**
132
+ * Returns the root selector for the component
133
+ * This method must be usually overriden to return the css selector of your component
134
+ *
135
+ * @example
136
+ *
137
+ * ```typescript
138
+ * getComponentSelector(): string {
139
+ * return 'app-component';
140
+ * }
141
+ * ```
142
+ * Will target the html component:
143
+ * ```html
144
+ * <app-component>...</app-component>
145
+ * ```
146
+ */
147
+ getComponentSelector(): string {
148
+ return '';
149
+ }
150
+
151
+ /**
152
+ * Wait for the locatorRoot to be displayed in the page
153
+ */
154
+ async waitLoaded() {
155
+ if (isLocator(this.locatorRoot)) {
156
+ await this.locatorRoot.waitFor();
157
+ }
158
+ }
159
+ }
package/lib/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './base.po';
package/package.json CHANGED
@@ -1,67 +1,61 @@
1
1
  {
2
2
  "name": "@agnos-ui/base-po",
3
3
  "description": "Base class to build page objects for end-to-end tests with Playwright.",
4
- "version": "0.4.0-next.0",
4
+ "version": "0.4.0",
5
5
  "keywords": [
6
6
  "e2e",
7
7
  "page-object",
8
8
  "playwright",
9
9
  "testing"
10
10
  ],
11
- "main": "dist/lib/index.js",
12
- "module": "dist/lib/index.mjs",
13
- "types": "dist/lib/index.d.ts",
11
+ "type": "module",
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
14
15
  "scripts": {
15
16
  "build": "wireit"
16
17
  },
17
18
  "wireit": {
18
19
  "build:src": {
19
- "command": "vite build",
20
+ "command": "vite build && tsc",
20
21
  "files": [
21
22
  "lib/**",
23
+ "tsconfig.json",
24
+ "../tsconfig.json",
22
25
  "vite.config.ts"
23
26
  ],
24
27
  "output": [
25
- "dist/lib/index.js",
26
- "dist/lib/index.mjs"
28
+ "dist/**",
29
+ "!dist/package.json",
30
+ "!dist/README.md"
27
31
  ]
28
32
  },
29
- "build:dts": {
30
- "command": "tsc -p tsconfig.d.json",
33
+ "build:pkg": {
34
+ "command": "node ../scripts/buildPackageJson.js . dist",
35
+ "dependencies": [
36
+ "build:src"
37
+ ],
31
38
  "files": [
32
- "lib/**",
33
- "tsconfig.json",
34
- "tsconfig.d.json",
35
- "../tsconfig.json"
39
+ "../scripts/buildPackageJson.js",
40
+ "../package.json",
41
+ "package.json",
42
+ "README.md"
36
43
  ],
37
44
  "output": [
38
- "dist/dts/**"
45
+ "dist/README.md",
46
+ "dist/package.json"
39
47
  ]
40
48
  },
41
49
  "build": {
42
- "command": "api-extractor run",
43
50
  "dependencies": [
44
51
  "build:src",
45
- "build:dts"
46
- ],
47
- "files": [
48
- "api-extractor.json",
49
- "../api-extractor.json",
50
- "tsconfig.json",
51
- "tsconfig.d.json",
52
- "../tsconfig.json"
53
- ],
54
- "output": [
55
- "dist/lib/index.d.ts"
52
+ "build:pkg"
56
53
  ]
57
54
  }
58
55
  },
59
56
  "peerDependencies": {
60
- "@playwright/test": "^1.44.1"
57
+ "@playwright/test": "^1.45.2"
61
58
  },
62
- "files": [
63
- "dist/lib"
64
- ],
65
59
  "license": "MIT",
66
60
  "bugs": "https://github.com/AmadeusITGroup/AgnosUI/issues",
67
61
  "repository": {
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": false,
5
+ "emitDeclarationOnly": true,
6
+ "outDir": "dist",
7
+ "declaration": true
8
+ },
9
+ "include": ["lib"]
10
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,17 @@
1
+ import {peerDependencies} from './package.json';
2
+ import {defineConfig} from 'vite';
3
+
4
+ // https://vitejs.dev/config/
5
+ export default defineConfig({
6
+ build: {
7
+ lib: {
8
+ entry: 'lib/index',
9
+ fileName: 'index',
10
+ formats: ['es', 'cjs'],
11
+ },
12
+ rollupOptions: {
13
+ external: [...Object.keys(peerDependencies)],
14
+ },
15
+ emptyOutDir: true,
16
+ },
17
+ });
@@ -1,121 +0,0 @@
1
- import type { Locator } from '@playwright/test';
2
- import type { Page } from '@playwright/test';
3
-
4
- /**
5
- * Base class to be extended for page objects.
6
- * The purpose of this architecture is to standardize the way locators are created in the page object, to be able to target precisely the dom elements you want to interact with.
7
- *
8
- * The constructor takes two parameters:
9
- *
10
- * - `container: Page | Locator` is the container locator from which other locators are built
11
- * - `index: number | null = null` is the nth index component found in the container locator
12
- *
13
- * Page objects must usually override the `getComponentSelector()` method to return the html selector of the component
14
- *
15
- * @example
16
- *
17
- * ```html
18
- * <!-- page.html -->
19
- * <div class="component1">
20
- * <div class="component2">
21
- * <button>Click me</button>
22
- * </div>
23
- * <div class="component2">
24
- * <button>Click me</button>
25
- * </div>
26
- * </div>
27
- * ```
28
- *
29
- * ```typescript
30
- * // component1.po.ts
31
- * export Component1Page extends BasePo {
32
- * getComponentSelector(): string {
33
- * return 'div.component1';
34
- * }
35
- * }
36
- *
37
- * // component2.po.ts
38
- * export Component2Page extends BasePo {
39
- * getComponentSelector(): string {
40
- * return 'div.component2';
41
- * }
42
- *
43
- * // Button locator in component2
44
- * get locatorButton() {
45
- * return this.locatorRoot.locator('button');
46
- * }
47
- * }
48
- *
49
- * // test.spec.ts
50
- * test('should click', ({page}) => {
51
- *
52
- * // will manage the component1 section
53
- * const component1Po = new Component1Page(page);
54
- *
55
- * // click on the button of the first component2 of component1
56
- * const firstComponent2Po = new Component2Page(component1Po.locatorRoot);
57
- * await firstComponent2Po.locatorButton.click();
58
- *
59
- * // click on the button of the second component2 of component1
60
- * const secondComponent2Po = new Component2Page(component1Po.locatorRoot, 1);
61
- * await secondComponent2Po.locatorButton.click();
62
- *
63
- * });
64
- *
65
- * ```
66
- *
67
- */
68
- export declare class BasePO {
69
- #private;
70
- /**
71
- * Playwright page object from which the page object has been created.
72
- * It can be convenient for situation where you can't work (internally) with the locatorRoot
73
- */
74
- protected _page: Page;
75
- /**
76
- *
77
- * @param container - Container locator (page if there is no container) where the component is located
78
- * @param index - If you have multiple components **inside the container**, you must provide the index to select the component to work with. You can omit this parameter if you have a single component.
79
- */
80
- constructor(container: Page | Locator, index?: number | null);
81
- /**
82
- * The root locator of your page object.
83
- * It can be used to be chain with internal locators.
84
- *
85
- * It is built with the provided `container` from the constructor, the `getComponentSelector` (one of them must be provided, at least) and the optional `index` parameter from the contructor.
86
- *
87
- * @example
88
- *
89
- * ```typescript
90
- *
91
- * // Button locator in this component
92
- * get locatorButton() {
93
- * return this.locatorRoot().locator('button').nth(0);
94
- * }
95
- * ```
96
- */
97
- get locatorRoot(): Locator;
98
- /**
99
- * Returns the root selector for the component
100
- * This method must be usually overriden to return the css selector of your component
101
- *
102
- * @example
103
- *
104
- * ```typescript
105
- * getComponentSelector(): string {
106
- * return 'app-component';
107
- * }
108
- * ```
109
- * Will target the html component:
110
- * ```html
111
- * <app-component>...</app-component>
112
- * ```
113
- */
114
- getComponentSelector(): string;
115
- /**
116
- * Wait for the locatorRoot to be displayed in the page
117
- */
118
- waitLoaded(): Promise<void>;
119
- }
120
-
121
- export { }
package/dist/lib/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var f=Object.defineProperty;var g=o=>{throw TypeError(o)};var m=(o,t,e)=>t in o?f(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var p=(o,t,e)=>m(o,typeof t!="symbol"?t+"":t,e),d=(o,t,e)=>t.has(o)||g("Cannot "+e);var s=(o,t,e)=>(d(o,t,"read from private field"),e?e.call(o):t.get(o)),c=(o,t,e)=>t.has(o)?g("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(o):t.set(o,e),n=(o,t,e,i)=>(d(o,t,"write to private field"),i?i.call(o,e):t.set(o,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function h(o){return"waitFor"in o}var r,a,l;class R{constructor(t,e=null){c(this,r);c(this,a);c(this,l);p(this,"_page");n(this,r,t),n(this,a,e),this._page=h(t)?t.page():t}get locatorRoot(){let t=s(this,l);if(!t){const e=this.getComponentSelector();let i=s(this,r);if(e){i=i.locator(e);const u=s(this,a);u!==null&&(i=i.nth(u))}t=h(i)?i:i.locator("html"),n(this,l,t)}return t}getComponentSelector(){return""}async waitLoaded(){h(this.locatorRoot)&&await this.locatorRoot.waitFor()}}r=new WeakMap,a=new WeakMap,l=new WeakMap;exports.BasePO=R;
@@ -1,88 +0,0 @@
1
- var f = Object.defineProperty;
2
- var u = (o) => {
3
- throw TypeError(o);
4
- };
5
- var m = (o, t, e) => t in o ? f(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
6
- var g = (o, t, e) => m(o, typeof t != "symbol" ? t + "" : t, e), R = (o, t, e) => t.has(o) || u("Cannot " + e);
7
- var l = (o, t, e) => (R(o, t, "read from private field"), e ? e.call(o) : t.get(o)), s = (o, t, e) => t.has(o) ? u("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(o) : t.set(o, e), n = (o, t, e, i) => (R(o, t, "write to private field"), i ? i.call(o, e) : t.set(o, e), e);
8
- function h(o) {
9
- return "waitFor" in o;
10
- }
11
- var r, a, c;
12
- class w {
13
- /**
14
- *
15
- * @param container - Container locator (page if there is no container) where the component is located
16
- * @param index - If you have multiple components **inside the container**, you must provide the index to select the component to work with. You can omit this parameter if you have a single component.
17
- */
18
- constructor(t, e = null) {
19
- s(this, r);
20
- s(this, a);
21
- s(this, c);
22
- /**
23
- * Playwright page object from which the page object has been created.
24
- * It can be convenient for situation where you can't work (internally) with the locatorRoot
25
- */
26
- g(this, "_page");
27
- n(this, r, t), n(this, a, e), this._page = h(t) ? t.page() : t;
28
- }
29
- /**
30
- * The root locator of your page object.
31
- * It can be used to be chain with internal locators.
32
- *
33
- * It is built with the provided `container` from the constructor, the `getComponentSelector` (one of them must be provided, at least) and the optional `index` parameter from the contructor.
34
- *
35
- * @example
36
- *
37
- * ```typescript
38
- *
39
- * // Button locator in this component
40
- * get locatorButton() {
41
- * return this.locatorRoot().locator('button').nth(0);
42
- * }
43
- * ```
44
- */
45
- get locatorRoot() {
46
- let t = l(this, c);
47
- if (!t) {
48
- const e = this.getComponentSelector();
49
- let i = l(this, r);
50
- if (e) {
51
- i = i.locator(e);
52
- const p = l(this, a);
53
- p !== null && (i = i.nth(p));
54
- }
55
- t = h(i) ? i : i.locator("html"), n(this, c, t);
56
- }
57
- return t;
58
- }
59
- /**
60
- * Returns the root selector for the component
61
- * This method must be usually overriden to return the css selector of your component
62
- *
63
- * @example
64
- *
65
- * ```typescript
66
- * getComponentSelector(): string {
67
- * return 'app-component';
68
- * }
69
- * ```
70
- * Will target the html component:
71
- * ```html
72
- * <app-component>...</app-component>
73
- * ```
74
- */
75
- getComponentSelector() {
76
- return "";
77
- }
78
- /**
79
- * Wait for the locatorRoot to be displayed in the page
80
- */
81
- async waitLoaded() {
82
- h(this.locatorRoot) && await this.locatorRoot.waitFor();
83
- }
84
- }
85
- r = new WeakMap(), a = new WeakMap(), c = new WeakMap();
86
- export {
87
- w as BasePO
88
- };