@empiricalrun/playwright-utils 0.14.2 → 0.14.3

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,11 @@
1
1
  # @empiricalrun/playwright-utils
2
2
 
3
+ ## 0.14.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 3c9f929: feat: AuthStore API changes, better login logic, docs
8
+
3
9
  ## 0.14.2
4
10
 
5
11
  ### Patch Changes
@@ -3,11 +3,16 @@ type WithKey<T> = T & {
3
3
  key: string;
4
4
  };
5
5
  export declare class AuthStore<T extends WithKey<{}>> {
6
- private page;
7
- private userContext;
8
- private loginFunc;
9
- private checkLoggedInFunc;
10
- constructor(page: Page, userContext: T, loginFunc: (page: Page, userContext: T) => Promise<void>, checkLoggedInFunc: (page: Page, userContext: T) => Promise<boolean>);
6
+ page: Page;
7
+ userContext: T;
8
+ loginFunc: (page: Page, userContext: T) => Promise<void>;
9
+ checkLoggedInFunc: (page: Page, userContext: T) => Promise<boolean>;
10
+ constructor({ page, userContext, loginFunc, checkLoggedInFunc, }: {
11
+ page: Page;
12
+ userContext: T;
13
+ loginFunc: (page: Page, userContext: T) => Promise<void>;
14
+ checkLoggedInFunc: (page: Page, userContext: T) => Promise<boolean>;
15
+ });
11
16
  loginIfRequired(): Promise<void>;
12
17
  static location(userContext: {
13
18
  key: string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE/C,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtC,qBAAa,SAAS,CAAC,CAAC,SAAS,OAAO,CAAC,EAAE,CAAC;IAExC,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,SAAS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CAAC,OAAO,CAAC,CAAC;KACrE;IAOK,eAAe;IAgCrB,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;CAGtD"}
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.AuthStore = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
4
8
  const playwright_core_1 = require("playwright-core");
5
9
  class AuthStore {
6
10
  page;
7
11
  userContext;
8
12
  loginFunc;
9
13
  checkLoggedInFunc;
10
- constructor(page, userContext, loginFunc, checkLoggedInFunc) {
14
+ constructor({ page, userContext, loginFunc, checkLoggedInFunc, }) {
11
15
  this.page = page;
12
16
  this.userContext = userContext;
13
17
  this.loginFunc = loginFunc;
@@ -15,21 +19,31 @@ class AuthStore {
15
19
  }
16
20
  async loginIfRequired() {
17
21
  let shouldDoLogin = false;
18
- try {
19
- const hasLoggedIn = await this.checkLoggedInFunc(this.page, this.userContext);
20
- shouldDoLogin = !hasLoggedIn;
22
+ const storagePath = AuthStore.location(this.userContext);
23
+ if (!fs_1.default.existsSync(storagePath)) {
24
+ shouldDoLogin = true;
21
25
  }
22
- catch (err) {
23
- if (err instanceof playwright_core_1.errors.TimeoutError) {
24
- shouldDoLogin = true;
26
+ else {
27
+ const browser = this.page.context().browser();
28
+ const newContext = await browser.newContext({
29
+ storageState: storagePath,
30
+ });
31
+ const page = await newContext.newPage();
32
+ try {
33
+ const hasLoggedIn = await this.checkLoggedInFunc(page, this.userContext);
34
+ shouldDoLogin = !hasLoggedIn;
25
35
  }
26
- else {
27
- throw err;
36
+ catch (err) {
37
+ if (err instanceof playwright_core_1.errors.TimeoutError) {
38
+ shouldDoLogin = true;
39
+ }
40
+ else {
41
+ throw err;
42
+ }
28
43
  }
29
44
  }
30
45
  if (shouldDoLogin) {
31
46
  await this.loginFunc(this.page, this.userContext);
32
- const storagePath = AuthStore.location(this.userContext);
33
47
  await this.page.context().storageState({ path: storagePath });
34
48
  }
35
49
  }
package/docs/auth.md CHANGED
@@ -4,7 +4,7 @@ This builds around playwright's `storageState` primitive which is used to cache
4
4
  auth related state (in a json file on disk).
5
5
 
6
6
  This requires an object of `UserContext` type, which has a `key: string`. The
7
- rest of the object can be custom.
7
+ rest of the object can be custom. See [section](#set-up-usercontext) on how to do that.
8
8
 
9
9
  ## Usage
10
10
 
@@ -46,3 +46,47 @@ projects: [
46
46
  }
47
47
  ]
48
48
  ```
49
+
50
+ ## Set up UserContext
51
+
52
+ - Create the `UserContext` type and implementations of that type in `test-data/index.ts`
53
+ - Create `TestOptions` in `fixtures.ts` that has all options that can be configured
54
+ - Configure fixtures and `playwright.config.ts` to use `TestOptions` type
55
+ - Set the `userContext` in the project.use field
56
+
57
+ ```ts
58
+ // fixtures.ts
59
+ import { baseTestFixture } from "@empiricalrun/playwright-utils/test";
60
+ import { UserContext } from "./test-data";
61
+
62
+ export type UserContext = {
63
+ key: string;
64
+ // other fields
65
+ }
66
+
67
+ export type TestOptions = {
68
+ userContext: UserContext;
69
+ }
70
+
71
+ export const test = baseTestFixture(base).extend<TestOptions>({
72
+ userContext: [primaryUser, { option: true }],
73
+ });
74
+ ```
75
+
76
+ ```ts
77
+ // playwright.config.ts
78
+ import { defineConfig } from "@playwright/test";
79
+ import { someTypeOfUser } from "./test-data";
80
+
81
+ export default defineConfig<TestOptions>({
82
+ ...baseConfig,
83
+ projects: [
84
+ {
85
+ name: "chromium",
86
+ use: {
87
+ userContext: someTypeOfUser
88
+ },
89
+ },
90
+ ],
91
+ });
92
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@empiricalrun/playwright-utils",
3
- "version": "0.14.2",
3
+ "version": "0.14.3",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -22,20 +22,20 @@
22
22
  },
23
23
  "author": "Empirical Team <hey@empirical.run>",
24
24
  "devDependencies": {
25
- "@playwright/test": "^1.47.1",
26
- "@types/node": "^20.14.9"
25
+ "@playwright/test": "1.47.1",
26
+ "@types/node": "^20.14.9",
27
+ "@types/babel__code-frame": "^7.0.6",
28
+ "@types/md5": "^2.3.5",
29
+ "@types/mime": "3.0.0"
27
30
  },
28
31
  "dependencies": {
29
32
  "@aws-sdk/client-s3": "^3.614.0",
30
33
  "@aws-sdk/s3-request-presigner": "^3.614.0",
31
34
  "@babel/code-frame": "^7.24.7",
32
- "@types/babel__code-frame": "^7.0.6",
33
- "@types/md5": "^2.3.5",
34
- "@types/mime": "3.0.0",
35
35
  "mailosaur": "^8.6.1",
36
36
  "md5": "^2.3.0",
37
37
  "mime": "3.0.0",
38
- "playwright-core": "^1.46.1",
38
+ "playwright-core": "1.47.1",
39
39
  "playwright-extra": "^4.3.6",
40
40
  "puppeteer-extra-plugin-recaptcha": "^3.6.8",
41
41
  "@empiricalrun/llm": "^0.9.4",