pomwright 1.0.2 → 1.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,204 @@
1
1
  # pomwright
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#29](https://github.com/DyHex/POMWright/pull/29) [`14b9bff`](https://github.com/DyHex/POMWright/commit/14b9bff99ec337a4d8b8940c9904620892faae3a) Thanks [@DyHex](https://github.com/DyHex)! - Exports ExtractFullUrlType, updates devDeps and adds additional keywords
8
+
9
+ ## 1.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#27](https://github.com/DyHex/POMWright/pull/27) [`ab778b2`](https://github.com/DyHex/POMWright/commit/ab778b268fcc77c03289f24617efa74518dcbe12) Thanks [@DyHex](https://github.com/DyHex)! - **Added optional support for dynamic URL types (`string` and/or `RegExp`) for `baseUrl` and `urlPath` via an options-based approach.**
14
+
15
+ - **WHAT**: This update allows `baseUrl` and `urlPath` to be defined as either `string` or `RegExp` using `BasePageOptions`. The `fullUrl` is automatically inferred based on the types of `baseUrl` and `urlPath`. If either `baseUrl` or `urlPath` is a `RegExp`, `fullUrl` will also be a `RegExp`.
16
+
17
+ - **WHY**: This change provides flexibility for handling dynamic URLs, such as those containing variables or patterns. It allows validating URLs that follow a predictable format but may have dynamic values that are not known before navigation.
18
+
19
+ - **HOW**:
20
+ - No changes are required for existing implementations since `string` remains the default type.
21
+ - To use `RegExp` for `baseUrl` or `urlPath`, define them explicitly in the POC using `BasePageOptions`, which will automatically infer `fullUrl`.
22
+ - **Note**: POCs using `RegExp` for `baseUrl` or `urlPath` cannot use `page.goto(fullUrl)` since it requires a `string`. Instead, methods like `page.waitForURL()`, which accept `RegExp`, can be used to validate navigation. This feature is ideal for scenarios where URL values are dynamic and can only be validated by format.
23
+
24
+ # Examples
25
+
26
+ ## Example: Abstract Base Class extending BasePage without BasePageOptions (default, same as prior versions)
27
+
28
+ ```ts
29
+ import { type Page, type TestInfo } from "@playwright/test";
30
+ import { BasePage, PlaywrightReportLogger } from "pomwright";
31
+ // import helper methods / classes etc, here... (To be used in the Base POC)
32
+
33
+ export default abstract class Base<
34
+ LocatorSchemaPathType extends string
35
+ > extends BasePage<LocatorSchemaPathType> {
36
+ // add properties here (available to all POCs extending this abstract Base POC)
37
+
38
+ constructor(
39
+ page: Page,
40
+ testInfo: TestInfo,
41
+ urlPath: string,
42
+ pocName: string,
43
+ pwrl: PlaywrightReportLogger
44
+ ) {
45
+ super(page, testInfo, "http://localhost:8080", urlPath, pocName, pwrl);
46
+
47
+ // initialize properties here (available to all POCs extending this abstract Base POC)
48
+ }
49
+
50
+ // add helper methods here (available to all POCs extending this abstract Base POC)
51
+ }
52
+ ```
53
+
54
+ ## Example: POC extending abstract Base Class without BasePageOptions (default, same as prior versions)
55
+
56
+ ```ts
57
+ import { type Page, type TestInfo } from "@playwright/test";
58
+ import { PlaywrightReportLogger } from "pomwright";
59
+ import Base from "../base/base.page";
60
+ import {
61
+ type LocatorSchemaPath,
62
+ initLocatorSchemas,
63
+ } from "./testPage.locatorSchema";
64
+
65
+ export default class TestPage extends Base<LocatorSchemaPath> {
66
+ constructor(page: Page, testInfo: TestInfo, pwrl: PlaywrightReportLogger) {
67
+ super(page, testInfo, "/", TestPage.name, pwrl);
68
+ }
69
+
70
+ protected initLocatorSchemas() {
71
+ initLocatorSchemas(this.locators);
72
+ }
73
+
74
+ // add your helper methods here...
75
+ }
76
+ ```
77
+
78
+ ## Example: Abstract Base Class extending BasePage with BasePageOptions, allows urlPath type as string xOR RegExp
79
+
80
+ ```ts
81
+ import { type Page, type TestInfo } from "@playwright/test";
82
+ import {
83
+ BasePage,
84
+ type BasePageOptions,
85
+ type ExtractUrlPathType,
86
+ PlaywrightReportLogger,
87
+ } from "pomwright";
88
+
89
+ // BaseWithOptions extends BasePage and enforces baseUrlType as string
90
+ export default abstract class BaseWithOptions<
91
+ LocatorSchemaPathType extends string,
92
+ Options extends BasePageOptions = {
93
+ urlOptions: { baseUrlType: string; urlPathType: string };
94
+ }
95
+ > extends BasePage<
96
+ LocatorSchemaPathType,
97
+ {
98
+ urlOptions: {
99
+ baseUrlType: string;
100
+ urlPathType: ExtractUrlPathType<Options>;
101
+ };
102
+ }
103
+ > {
104
+ constructor(
105
+ page: Page,
106
+ testInfo: TestInfo,
107
+ urlPath: ExtractUrlPathType<{
108
+ urlOptions: { urlPathType: ExtractUrlPathType<Options> };
109
+ }>, // Ensure the correct type for urlPath
110
+ pocName: string,
111
+ pwrl: PlaywrightReportLogger
112
+ ) {
113
+ // Pass baseUrl as a string and let urlPath be flexible
114
+ super(page, testInfo, "http://localhost:8080", urlPath, pocName, pwrl);
115
+
116
+ // Initialize additional properties if needed
117
+ }
118
+
119
+ // Add any helper methods here, if needed
120
+ }
121
+ ```
122
+
123
+ ## Example: POC extending abstract Base Class with BasePageOptions, but uses defaults (type string)
124
+
125
+ ```ts
126
+ import {
127
+ type LocatorSchemaPath,
128
+ initLocatorSchemas,
129
+ } from "@page-object-models/testApp/without-options/pages/testPage.locatorSchema"; // same page & same locator schema as above
130
+ import { type Page, type TestInfo } from "@playwright/test";
131
+ import { PlaywrightReportLogger } from "pomwright";
132
+ import BaseWithOptions from "../base/baseWithOptions.page";
133
+
134
+ // Note, if BasePageOptions aren't specified, default options are used
135
+ export default class TestPage extends BaseWithOptions<LocatorSchemaPath> {
136
+ constructor(page: Page, testInfo: TestInfo, pwrl: PlaywrightReportLogger) {
137
+ super(page, testInfo, "/", TestPage.name, pwrl);
138
+ }
139
+
140
+ protected initLocatorSchemas() {
141
+ initLocatorSchemas(this.locators);
142
+ }
143
+
144
+ // add your helper methods here...
145
+ }
146
+ ```
147
+
148
+ ## Example: POC extending abstract Base Class with BasePageOptions, and defines urlPath type as RegExp
149
+
150
+ ```ts
151
+ import { test } from "@fixtures/withOptions";
152
+ import BaseWithOptions from "@page-object-models/testApp/with-options/base/baseWithOptions.page";
153
+ import { type Page, type TestInfo, expect } from "@playwright/test";
154
+ import { PlaywrightReportLogger } from "pomwright";
155
+ import {
156
+ type LocatorSchemaPath,
157
+ initLocatorSchemas,
158
+ } from "./color.locatorSchema";
159
+
160
+ // By providing the urlOptions, the urlPath property now has RegExp type instead of string type (default) for this POC
161
+ export default class Color extends BaseWithOptions<
162
+ LocatorSchemaPath,
163
+ { urlOptions: { urlPathType: RegExp } }
164
+ > {
165
+ constructor(page: Page, testInfo: TestInfo, pwrl: PlaywrightReportLogger) {
166
+ /**
167
+ * Matches "/testpath/randomcolor/" followed by a valid 3 or 6-character hex color code.
168
+ */
169
+ const urlPathRegex = /\/testpath\/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
170
+
171
+ super(page, testInfo, urlPathRegex, Color.name, pwrl);
172
+ }
173
+
174
+ protected initLocatorSchemas() {
175
+ initLocatorSchemas(this.locators);
176
+ }
177
+
178
+ async expectThisPage() {
179
+ await test.step("Expect color page", async () => {
180
+ await this.page.waitForURL(this.fullUrl);
181
+
182
+ const heading = await this.getNestedLocator("body.heading");
183
+ await heading.waitFor({ state: "visible" });
184
+ });
185
+ }
186
+
187
+ // add your helper methods here...
188
+ }
189
+ ```
190
+
191
+ # Summary of Changes
192
+
193
+ - **Dynamic URL Support**: You can now define `baseUrl` and `urlPath` as `RegExp` for dynamic URL handling.
194
+ - **Backward Compatibility**: No changes are required for existing implementations using `string`.
195
+ - **Restructuring of tests**: Unit tests moved from ./src to ./test, integration tests moved from ./test to ./intTest
196
+ - **New unit tests**: To cover optional-dynamic-url-types (string xOR RegExp)
197
+ - **New integration tests with Playwright/test**: To cover optional-dynamic-url-types (string xOR RegExp)
198
+ - **peerDependencies change**: Bumped minimum Playwright/test version from 1.39.0 to 1.41.0 as everything below 1.41.0 is deprecated. Also excluded v. 2.x.x as they have known bugs for chaining of locators. Limited max version to <2.0.0
199
+
200
+ Changes have additionally been tested with two seperate playwright/test projects (130+ E2E tests) without issues, and against the latest Playwright/test versions.
201
+
3
202
  ## 1.0.2
4
203
 
5
204
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -232,14 +232,14 @@ interface ModifiedLocatorSchema extends UpdatableLocatorSchemaProperties {
232
232
  * and for building nested locators based on these schemas.
233
233
  */
234
234
  declare class GetLocatorBase<LocatorSchemaPathType extends string> {
235
- protected pageObjectClass: BasePage<LocatorSchemaPathType>;
235
+ protected pageObjectClass: BasePage<LocatorSchemaPathType, BasePageOptions>;
236
236
  protected log: PlaywrightReportLogger;
237
237
  private getBy;
238
238
  private locatorSchemas;
239
239
  /**
240
240
  * Initializes the GetLocatorBase class with a page object class and a logger.
241
241
  */
242
- constructor(pageObjectClass: BasePage<LocatorSchemaPathType>, log: PlaywrightReportLogger);
242
+ constructor(pageObjectClass: BasePage<LocatorSchemaPathType, BasePageOptions>, log: PlaywrightReportLogger);
243
243
  /**
244
244
  * Retrieves a locator schema with additional methods for manipulation and retrieval of locators.
245
245
  */
@@ -363,8 +363,30 @@ declare class SessionStorage {
363
363
  clear(): Promise<void>;
364
364
  }
365
365
 
366
+ type BasePageOptions = {
367
+ urlOptions?: {
368
+ baseUrlType?: string | RegExp;
369
+ urlPathType?: string | RegExp;
370
+ };
371
+ };
372
+ type ExtractBaseUrlType<T extends BasePageOptions> = T["urlOptions"] extends {
373
+ baseUrlType: RegExp;
374
+ } ? RegExp : string;
375
+ type ExtractUrlPathType<T extends BasePageOptions> = T["urlOptions"] extends {
376
+ urlPathType: RegExp;
377
+ } ? RegExp : string;
378
+ type ExtractFullUrlType<T extends BasePageOptions> = T["urlOptions"] extends {
379
+ baseUrlType: RegExp;
380
+ } | {
381
+ urlPathType: RegExp;
382
+ } ? RegExp : string;
366
383
  /** The BasePage class, extended by all Page Object Classes */
367
- declare abstract class BasePage<LocatorSchemaPathType extends string> {
384
+ declare abstract class BasePage<LocatorSchemaPathType extends string, Options extends BasePageOptions = {
385
+ urlOptions: {
386
+ baseUrlType: string;
387
+ urlPathType: string;
388
+ };
389
+ }> {
368
390
  /** Provides Playwright page methods */
369
391
  page: Page;
370
392
  /** Playwright TestInfo contains information about currently running test, available to any test function */
@@ -372,10 +394,10 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
372
394
  /** Selectors can be used to install custom selector engines.*/
373
395
  selector: Selectors;
374
396
  /** The base URL of the Page Object Class */
375
- baseUrl: string;
397
+ baseUrl: ExtractBaseUrlType<Options>;
376
398
  /** The URL path of the Page Object Class */
377
- urlPath: string;
378
- fullUrl: string;
399
+ urlPath: ExtractUrlPathType<Options>;
400
+ fullUrl: ExtractFullUrlType<Options>;
379
401
  /** The name of the Page Object Class */
380
402
  pocName: string;
381
403
  /** The Page Object Class' PlaywrightReportLogger instance, prefixed with its name. Log levels: debug, info, warn, and error. */
@@ -383,7 +405,8 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
383
405
  /** The SessionStorage class provides methods for setting and getting session storage data in Playwright.*/
384
406
  sessionStorage: SessionStorage;
385
407
  protected locators: GetLocatorBase<LocatorSchemaPathType>;
386
- constructor(page: Page, testInfo: TestInfo, baseUrl: string, urlPath: string, pocName: string, pwrl: PlaywrightReportLogger);
408
+ constructor(page: Page, testInfo: TestInfo, baseUrl: ExtractBaseUrlType<Options>, urlPath: ExtractUrlPathType<Options>, pocName: string, pwrl: PlaywrightReportLogger);
409
+ private constructFullUrl;
387
410
  /**
388
411
  * getNestedLocator(indices?: { [key: number]: number | null } | null)
389
412
  * - Asynchronously retrieves a nested locator based on the LocatorSchemaPath provided by getLocatorSchema("...")
@@ -394,7 +417,7 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
394
417
  */
395
418
  getNestedLocator: (locatorSchemaPath: LocatorSchemaPathType, indices?: {
396
419
  [key: number]: number | null;
397
- } | null | undefined) => Promise<Locator>;
420
+ } | null) => Promise<Locator>;
398
421
  /**
399
422
  * getLocator()
400
423
  * - Asynchronously retrieves a locator based on the current LocatorSchema. This method does not perform nesting,
@@ -488,16 +511,6 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
488
511
  * GetLocatorBase class. It enriches the returned schema with additional methods to handle updates and retrieval of
489
512
  * deep copy locators.
490
513
  *
491
- * Providing a precise and powerful solution for interacting with elements through locators in a structured
492
- * or hierarchical manner:
493
- * - Effortless validation of any element's expected location in the DOM.
494
- * - Improved readability and maintainability of tests.
495
- * - Improved readability and maintainability of Page Object Classes (POCs), through the use of a single source of
496
- * truth and flat locator (LocatorSchema) structure.
497
- * - Improved rebustness of tests in the face of DOM changes.
498
- * - Simpler debugging and maintenance as a result of limitin/scoping the number of possible resolvable elements
499
- * - Highly veratile usage
500
- *
501
514
  * getLocatorSchema adds the following chainable methods to the returned LocatorSchemaWithMethods object:
502
515
  *
503
516
  * update(updates: Partial< UpdatableLocatorSchemaProperties >)
@@ -558,4 +571,4 @@ declare class BaseApi {
558
571
  constructor(baseUrl: string, apiName: string, context: APIRequestContext, pwrl: PlaywrightReportLogger);
559
572
  }
560
573
 
561
- export { type AriaRoleType, BaseApi, BasePage, GetByMethod, GetLocatorBase, type LocatorSchema, PlaywrightReportLogger, test };
574
+ export { type AriaRoleType, BaseApi, BasePage, type BasePageOptions, type ExtractBaseUrlType, type ExtractFullUrlType, type ExtractUrlPathType, GetByMethod, GetLocatorBase, type LocatorSchema, PlaywrightReportLogger, test };
package/dist/index.d.ts CHANGED
@@ -232,14 +232,14 @@ interface ModifiedLocatorSchema extends UpdatableLocatorSchemaProperties {
232
232
  * and for building nested locators based on these schemas.
233
233
  */
234
234
  declare class GetLocatorBase<LocatorSchemaPathType extends string> {
235
- protected pageObjectClass: BasePage<LocatorSchemaPathType>;
235
+ protected pageObjectClass: BasePage<LocatorSchemaPathType, BasePageOptions>;
236
236
  protected log: PlaywrightReportLogger;
237
237
  private getBy;
238
238
  private locatorSchemas;
239
239
  /**
240
240
  * Initializes the GetLocatorBase class with a page object class and a logger.
241
241
  */
242
- constructor(pageObjectClass: BasePage<LocatorSchemaPathType>, log: PlaywrightReportLogger);
242
+ constructor(pageObjectClass: BasePage<LocatorSchemaPathType, BasePageOptions>, log: PlaywrightReportLogger);
243
243
  /**
244
244
  * Retrieves a locator schema with additional methods for manipulation and retrieval of locators.
245
245
  */
@@ -363,8 +363,30 @@ declare class SessionStorage {
363
363
  clear(): Promise<void>;
364
364
  }
365
365
 
366
+ type BasePageOptions = {
367
+ urlOptions?: {
368
+ baseUrlType?: string | RegExp;
369
+ urlPathType?: string | RegExp;
370
+ };
371
+ };
372
+ type ExtractBaseUrlType<T extends BasePageOptions> = T["urlOptions"] extends {
373
+ baseUrlType: RegExp;
374
+ } ? RegExp : string;
375
+ type ExtractUrlPathType<T extends BasePageOptions> = T["urlOptions"] extends {
376
+ urlPathType: RegExp;
377
+ } ? RegExp : string;
378
+ type ExtractFullUrlType<T extends BasePageOptions> = T["urlOptions"] extends {
379
+ baseUrlType: RegExp;
380
+ } | {
381
+ urlPathType: RegExp;
382
+ } ? RegExp : string;
366
383
  /** The BasePage class, extended by all Page Object Classes */
367
- declare abstract class BasePage<LocatorSchemaPathType extends string> {
384
+ declare abstract class BasePage<LocatorSchemaPathType extends string, Options extends BasePageOptions = {
385
+ urlOptions: {
386
+ baseUrlType: string;
387
+ urlPathType: string;
388
+ };
389
+ }> {
368
390
  /** Provides Playwright page methods */
369
391
  page: Page;
370
392
  /** Playwright TestInfo contains information about currently running test, available to any test function */
@@ -372,10 +394,10 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
372
394
  /** Selectors can be used to install custom selector engines.*/
373
395
  selector: Selectors;
374
396
  /** The base URL of the Page Object Class */
375
- baseUrl: string;
397
+ baseUrl: ExtractBaseUrlType<Options>;
376
398
  /** The URL path of the Page Object Class */
377
- urlPath: string;
378
- fullUrl: string;
399
+ urlPath: ExtractUrlPathType<Options>;
400
+ fullUrl: ExtractFullUrlType<Options>;
379
401
  /** The name of the Page Object Class */
380
402
  pocName: string;
381
403
  /** The Page Object Class' PlaywrightReportLogger instance, prefixed with its name. Log levels: debug, info, warn, and error. */
@@ -383,7 +405,8 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
383
405
  /** The SessionStorage class provides methods for setting and getting session storage data in Playwright.*/
384
406
  sessionStorage: SessionStorage;
385
407
  protected locators: GetLocatorBase<LocatorSchemaPathType>;
386
- constructor(page: Page, testInfo: TestInfo, baseUrl: string, urlPath: string, pocName: string, pwrl: PlaywrightReportLogger);
408
+ constructor(page: Page, testInfo: TestInfo, baseUrl: ExtractBaseUrlType<Options>, urlPath: ExtractUrlPathType<Options>, pocName: string, pwrl: PlaywrightReportLogger);
409
+ private constructFullUrl;
387
410
  /**
388
411
  * getNestedLocator(indices?: { [key: number]: number | null } | null)
389
412
  * - Asynchronously retrieves a nested locator based on the LocatorSchemaPath provided by getLocatorSchema("...")
@@ -394,7 +417,7 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
394
417
  */
395
418
  getNestedLocator: (locatorSchemaPath: LocatorSchemaPathType, indices?: {
396
419
  [key: number]: number | null;
397
- } | null | undefined) => Promise<Locator>;
420
+ } | null) => Promise<Locator>;
398
421
  /**
399
422
  * getLocator()
400
423
  * - Asynchronously retrieves a locator based on the current LocatorSchema. This method does not perform nesting,
@@ -488,16 +511,6 @@ declare abstract class BasePage<LocatorSchemaPathType extends string> {
488
511
  * GetLocatorBase class. It enriches the returned schema with additional methods to handle updates and retrieval of
489
512
  * deep copy locators.
490
513
  *
491
- * Providing a precise and powerful solution for interacting with elements through locators in a structured
492
- * or hierarchical manner:
493
- * - Effortless validation of any element's expected location in the DOM.
494
- * - Improved readability and maintainability of tests.
495
- * - Improved readability and maintainability of Page Object Classes (POCs), through the use of a single source of
496
- * truth and flat locator (LocatorSchema) structure.
497
- * - Improved rebustness of tests in the face of DOM changes.
498
- * - Simpler debugging and maintenance as a result of limitin/scoping the number of possible resolvable elements
499
- * - Highly veratile usage
500
- *
501
514
  * getLocatorSchema adds the following chainable methods to the returned LocatorSchemaWithMethods object:
502
515
  *
503
516
  * update(updates: Partial< UpdatableLocatorSchemaProperties >)
@@ -558,4 +571,4 @@ declare class BaseApi {
558
571
  constructor(baseUrl: string, apiName: string, context: APIRequestContext, pwrl: PlaywrightReportLogger);
559
572
  }
560
573
 
561
- export { type AriaRoleType, BaseApi, BasePage, GetByMethod, GetLocatorBase, type LocatorSchema, PlaywrightReportLogger, test };
574
+ export { type AriaRoleType, BaseApi, BasePage, type BasePageOptions, type ExtractBaseUrlType, type ExtractFullUrlType, type ExtractUrlPathType, GetByMethod, GetLocatorBase, type LocatorSchema, PlaywrightReportLogger, test };