@agnos-ui/base-po 0.0.1-alpha.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/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # @agnos-ui/base-po
2
+
3
+ [![npm](https://img.shields.io/npm/v/@agnos-ui/base-po)](https://www.npmjs.com/package/@agnos-ui/base-po)
4
+
5
+ Base class to build page objects for end-to-end tests with Playwright.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @agnos-ui/base-po
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import {BasePO} from '@agnos-ui/base-po';
17
+ import {Locator} from '@playwright/test';
18
+
19
+ export const customComponentSelectors = {
20
+ rootComponent: '.custom',
21
+ otherContent: '.content-class',
22
+ };
23
+
24
+ export class CustomComponentPO extends BasePO {
25
+ selectors = {...customComponentSelectors};
26
+
27
+ override getComponentSelector(): string {
28
+ return this.selectors.rootComponent;
29
+ }
30
+
31
+ get locatorOtherContent(): Locator {
32
+ return this.locatorRoot.locator(this.selectors.otherContent);
33
+ }
34
+ }
35
+ ```
36
+
37
+ ## Main features
38
+
39
+ Please refer to the documentation included in [the source code](lib/base.po.ts).
@@ -0,0 +1,121 @@
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 { }
@@ -0,0 +1 @@
1
+ "use strict";var d=Object.defineProperty;var f=(o,t,e)=>t in o?d(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var g=(o,t,e)=>(f(o,typeof t!="symbol"?t+"":t,e),e),p=(o,t,e)=>{if(!t.has(o))throw TypeError("Cannot "+e)};var s=(o,t,e)=>(p(o,t,"read from private field"),e?e.call(o):t.get(o)),c=(o,t,e)=>{if(t.has(o))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(o):t.set(o,e)},n=(o,t,e,i)=>(p(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 m{constructor(t,e=null){c(this,r,void 0);c(this,a,void 0);c(this,l,void 0);g(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=m;
@@ -0,0 +1,92 @@
1
+ var R = Object.defineProperty;
2
+ var f = (o, t, e) => t in o ? R(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var u = (o, t, e) => (f(o, typeof t != "symbol" ? t + "" : t, e), e), g = (o, t, e) => {
4
+ if (!t.has(o))
5
+ throw TypeError("Cannot " + e);
6
+ };
7
+ var l = (o, t, e) => (g(o, t, "read from private field"), e ? e.call(o) : t.get(o)), s = (o, t, e) => {
8
+ if (t.has(o))
9
+ throw TypeError("Cannot add the same private member more than once");
10
+ t instanceof WeakSet ? t.add(o) : t.set(o, e);
11
+ }, n = (o, t, e, i) => (g(o, t, "write to private field"), i ? i.call(o, e) : t.set(o, e), e);
12
+ function h(o) {
13
+ return "waitFor" in o;
14
+ }
15
+ var r, a, c;
16
+ class d {
17
+ /**
18
+ *
19
+ * @param container - Container locator (page if there is no container) where the component is located
20
+ * @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.
21
+ */
22
+ constructor(t, e = null) {
23
+ s(this, r, void 0);
24
+ s(this, a, void 0);
25
+ s(this, c, void 0);
26
+ /**
27
+ * Playwright page object from which the page object has been created.
28
+ * It can be convenient for situation where you can't work (internally) with the locatorRoot
29
+ */
30
+ u(this, "_page");
31
+ n(this, r, t), n(this, a, e), this._page = h(t) ? t.page() : t;
32
+ }
33
+ /**
34
+ * The root locator of your page object.
35
+ * It can be used to be chain with internal locators.
36
+ *
37
+ * 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.
38
+ *
39
+ * @example
40
+ *
41
+ * ```typescript
42
+ *
43
+ * // Button locator in this component
44
+ * get locatorButton() {
45
+ * return this.locatorRoot().locator('button').nth(0);
46
+ * }
47
+ * ```
48
+ */
49
+ get locatorRoot() {
50
+ let t = l(this, c);
51
+ if (!t) {
52
+ const e = this.getComponentSelector();
53
+ let i = l(this, r);
54
+ if (e) {
55
+ i = i.locator(e);
56
+ const p = l(this, a);
57
+ p !== null && (i = i.nth(p));
58
+ }
59
+ t = h(i) ? i : i.locator("html"), n(this, c, t);
60
+ }
61
+ return t;
62
+ }
63
+ /**
64
+ * Returns the root selector for the component
65
+ * This method must be usually overriden to return the css selector of your component
66
+ *
67
+ * @example
68
+ *
69
+ * ```typescript
70
+ * getComponentSelector(): string {
71
+ * return 'app-component';
72
+ * }
73
+ * ```
74
+ * Will target the html component:
75
+ * ```html
76
+ * <app-component>...</app-component>
77
+ * ```
78
+ */
79
+ getComponentSelector() {
80
+ return "";
81
+ }
82
+ /**
83
+ * Wait for the locatorRoot to be displayed in the page
84
+ */
85
+ async waitLoaded() {
86
+ h(this.locatorRoot) && await this.locatorRoot.waitFor();
87
+ }
88
+ }
89
+ r = new WeakMap(), a = new WeakMap(), c = new WeakMap();
90
+ export {
91
+ d as BasePO
92
+ };
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.36.4"
9
+ }
10
+ ]
11
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@agnos-ui/base-po",
3
+ "description": "Base class to build page objects for end-to-end tests with Playwright.",
4
+ "keywords": [
5
+ "playwright",
6
+ "page-object",
7
+ "e2e",
8
+ "testing"
9
+ ],
10
+ "main": "dist/lib/index.js",
11
+ "module": "dist/lib/index.mjs",
12
+ "types": "dist/lib/index.d.ts",
13
+ "scripts": {
14
+ "build": "npm run build:rollup && npm run build:dts && npm run build:api-extractor",
15
+ "build:rollup": "tsc && vite build -c vite.config.ts",
16
+ "build:dts": "tsc -p tsconfig.d.json",
17
+ "build:api-extractor": "api-extractor run"
18
+ },
19
+ "peerDependencies": {
20
+ "@playwright/test": "*"
21
+ },
22
+ "files": [
23
+ "dist/lib"
24
+ ],
25
+ "license": "MIT",
26
+ "bugs": "https://github.com/AmadeusITGroup/AgnosUI/issues",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/AmadeusITGroup/AgnosUI.git",
30
+ "directory": "base-po"
31
+ },
32
+ "version": "0.0.1-alpha.0"
33
+ }