@empiricalrun/playwright-utils 0.22.0 → 0.22.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,13 @@
1
1
  # @empiricalrun/playwright-utils
2
2
 
3
+ ## 0.22.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 8e4bf9d: fix: text content for dismissed overlay is undefined
8
+ - Updated dependencies [0c29798]
9
+ - @empiricalrun/test-gen@0.46.4
10
+
3
11
  ## 0.22.0
4
12
 
5
13
  ### Minor Changes
@@ -1,7 +1,14 @@
1
1
  import type { Page } from "@playwright/test";
2
- import { OverlayElement } from "./types";
3
- export declare function setCodeToCache(pageRef: Page, element: OverlayElement | undefined, code: string): Promise<void>;
4
- export declare function executeFromCache(pageRef: Page, element: OverlayElement | undefined): Promise<{
2
+ export declare function setCodeToCache({ dom, text, code, }: {
3
+ dom: string | undefined;
4
+ text: string | undefined;
5
+ code: string;
6
+ }): Promise<void>;
7
+ export declare function executeFromCache({ page, dom, text, }: {
8
+ page: Page;
9
+ dom: string | undefined;
10
+ text: string | undefined;
11
+ }): Promise<{
5
12
  success: boolean;
6
13
  }>;
7
14
  //# sourceMappingURL=cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../../../src/test/scripts/pw-locator-patch/dismiss-overlays/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAI7C,OAAO,EAA6B,cAAc,EAAE,MAAM,SAAS,CAAC;AAcpE,wBAAsB,cAAc,CAClC,OAAO,EAAE,IAAI,EACb,OAAO,EAAE,cAAc,GAAG,SAAS,EACnC,IAAI,EAAE,MAAM,iBAiCb;AAED,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,IAAI,EACb,OAAO,EAAE,cAAc,GAAG,SAAS,GAClC,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAiC/B"}
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../../../src/test/scripts/pw-locator-patch/dismiss-overlays/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAsB7C,wBAAsB,cAAc,CAAC,EACnC,GAAG,EACH,IAAI,EACJ,IAAI,GACL,EAAE;IACD,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;CACd,iBAiCA;AAED,wBAAsB,gBAAgB,CAAC,EACrC,IAAI,EACJ,GAAG,EACH,IAAI,GACL,EAAE;IACD,IAAI,EAAE,IAAI,CAAC;IACX,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1B,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAkChC"}
@@ -6,14 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.executeFromCache = exports.setCodeToCache = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
- const utils_1 = require("./utils");
10
9
  const CACHE_FILE = path_1.default.join(process.cwd(), ".empiricalrun", `overlay-cache.json`);
11
10
  function isCacheSupported(code) {
12
11
  // Current release only supports caching for code that contains 1 `click` only
13
12
  return code.includes("click") && code.trim().split("\n").length <= 1;
14
13
  }
15
- async function setCodeToCache(pageRef, element, code) {
16
- if (!element || !element.interceptor) {
14
+ function normalizeString(str) {
15
+ // Replace all whitespace sequences with a single space
16
+ return str.replace(/\s+/g, " ").trim();
17
+ }
18
+ async function setCodeToCache({ dom, text, code, }) {
19
+ if (!dom && !text) {
20
+ // At least one of the two is required
17
21
  return;
18
22
  }
19
23
  if (!isCacheSupported(code)) {
@@ -21,8 +25,8 @@ async function setCodeToCache(pageRef, element, code) {
21
25
  }
22
26
  const obj = {
23
27
  element: {
24
- dom: element.interceptor,
25
- textContent: (await (0, utils_1.textContent)(pageRef, element))?.trim() || "",
28
+ dom: dom || "",
29
+ textContent: normalizeString(text || ""),
26
30
  },
27
31
  code,
28
32
  };
@@ -47,29 +51,30 @@ async function setCodeToCache(pageRef, element, code) {
47
51
  fs_1.default.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2));
48
52
  }
49
53
  exports.setCodeToCache = setCodeToCache;
50
- async function executeFromCache(pageRef, element) {
51
- if (!element || !element.interceptor) {
54
+ async function executeFromCache({ page, dom, text, }) {
55
+ if (!dom && !text) {
56
+ // At least one of the two is required
52
57
  return { success: false };
53
58
  }
54
59
  try {
55
60
  const cache = JSON.parse(fs_1.default.readFileSync(CACHE_FILE, "utf8"));
56
- const text = (await (0, utils_1.textContent)(pageRef, element))?.trim() || "";
57
61
  const match = cache.data.find((c) => {
58
- return (c.element.dom === element.interceptor && c.element.textContent === text);
62
+ return (c.element.dom === (dom || "") &&
63
+ c.element.textContent === normalizeString(text || ""));
59
64
  });
60
65
  if (!match) {
61
66
  return { success: false };
62
67
  }
63
68
  try {
64
- console.log(`Executing for element: ${element} and code: ${match.code}`);
69
+ console.log(`Executing "${match.code}" for element: ${dom}`);
65
70
  // Ref: https://davidwalsh.name/async-function-class
66
71
  const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
67
72
  const exec = new AsyncFunction("page", match.code);
68
- await exec(pageRef);
73
+ await exec(page);
69
74
  return { success: true };
70
75
  }
71
76
  catch (err) {
72
- console.log(`Failed to execute ${match.code} for element: ${element}`);
77
+ console.log(`Failed in "${match.code}" for element: ${dom}`);
73
78
  return { success: false };
74
79
  }
75
80
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/test/scripts/pw-locator-patch/dismiss-overlays/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAKzC,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,WAKhE;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,IAAI,EACb,OAAO,EAAE,cAAc,GAAG,SAAS,iBAkDpC;AAED,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,GACnB,cAAc,GAAG,SAAS,CAiC5B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/test/scripts/pw-locator-patch/dismiss-overlays/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAKzC,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,WAKhE;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,IAAI,EACb,OAAO,EAAE,cAAc,GAAG,SAAS,iBAyDpC;AAED,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,GACnB,cAAc,GAAG,SAAS,CAiC5B"}
@@ -48,7 +48,11 @@ Don't reattempt the click on Target element, your job is done after the first cl
48
48
  : ``;
49
49
  task += `${promptAddition}`;
50
50
  }
51
- const { success } = await (0, cache_1.executeFromCache)(pageRef, element);
51
+ const { success } = await (0, cache_1.executeFromCache)({
52
+ page: pageRef,
53
+ dom: element?.interceptor,
54
+ text,
55
+ });
52
56
  if (success) {
53
57
  return;
54
58
  }
@@ -59,7 +63,11 @@ Don't reattempt the click on Target element, your job is done after the first cl
59
63
  useActionSpecificAnnotations: true,
60
64
  },
61
65
  });
62
- await (0, cache_1.setCodeToCache)(pageRef, element, result.code);
66
+ await (0, cache_1.setCodeToCache)({
67
+ dom: element?.interceptor,
68
+ text,
69
+ code: result.code,
70
+ });
63
71
  }
64
72
  exports.runAgentOnOverlay = runAgentOnOverlay;
65
73
  function extractInterceptingElement(errorMessage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@empiricalrun/playwright-utils",
3
- "version": "0.22.0",
3
+ "version": "0.22.1",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -44,7 +44,7 @@
44
44
  "rimraf": "^6.0.1",
45
45
  "@empiricalrun/llm": "^0.9.35",
46
46
  "@empiricalrun/r2-uploader": "^0.3.8",
47
- "@empiricalrun/test-gen": "^0.46.3"
47
+ "@empiricalrun/test-gen": "^0.46.4"
48
48
  },
49
49
  "scripts": {
50
50
  "dev": "tsc --build --watch",