@browserbasehq/sdk 1.4.4 → 2.0.0-alpha.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.
Files changed (268) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +198 -4
  3. package/README.md +266 -81
  4. package/_shims/MultipartBody.d.ts +9 -0
  5. package/_shims/MultipartBody.d.ts.map +1 -0
  6. package/_shims/MultipartBody.js +16 -0
  7. package/_shims/MultipartBody.js.map +1 -0
  8. package/_shims/MultipartBody.mjs +12 -0
  9. package/_shims/MultipartBody.mjs.map +1 -0
  10. package/_shims/README.md +46 -0
  11. package/_shims/auto/runtime-bun.d.ts +5 -0
  12. package/_shims/auto/runtime-bun.d.ts.map +1 -0
  13. package/_shims/auto/runtime-bun.js +21 -0
  14. package/_shims/auto/runtime-bun.js.map +1 -0
  15. package/_shims/auto/runtime-bun.mjs +2 -0
  16. package/_shims/auto/runtime-bun.mjs.map +1 -0
  17. package/_shims/auto/runtime-node.d.ts +5 -0
  18. package/_shims/auto/runtime-node.d.ts.map +1 -0
  19. package/_shims/auto/runtime-node.js +21 -0
  20. package/_shims/auto/runtime-node.js.map +1 -0
  21. package/_shims/auto/runtime-node.mjs +2 -0
  22. package/_shims/auto/runtime-node.mjs.map +1 -0
  23. package/_shims/auto/runtime.d.ts +5 -0
  24. package/_shims/auto/runtime.d.ts.map +1 -0
  25. package/_shims/auto/runtime.js +21 -0
  26. package/_shims/auto/runtime.js.map +1 -0
  27. package/_shims/auto/runtime.mjs +2 -0
  28. package/_shims/auto/runtime.mjs.map +1 -0
  29. package/_shims/auto/types-node.d.ts +5 -0
  30. package/_shims/auto/types-node.d.ts.map +1 -0
  31. package/_shims/auto/types-node.js +21 -0
  32. package/_shims/auto/types-node.js.map +1 -0
  33. package/_shims/auto/types-node.mjs +2 -0
  34. package/_shims/auto/types-node.mjs.map +1 -0
  35. package/_shims/auto/types.d.ts +101 -0
  36. package/_shims/auto/types.js +3 -0
  37. package/_shims/auto/types.mjs +3 -0
  38. package/_shims/bun-runtime.d.ts +6 -0
  39. package/_shims/bun-runtime.d.ts.map +1 -0
  40. package/_shims/bun-runtime.js +13 -0
  41. package/_shims/bun-runtime.js.map +1 -0
  42. package/_shims/bun-runtime.mjs +10 -0
  43. package/_shims/bun-runtime.mjs.map +1 -0
  44. package/_shims/index.d.ts +81 -0
  45. package/_shims/index.js +13 -0
  46. package/_shims/index.mjs +7 -0
  47. package/_shims/manual-types.d.ts +12 -0
  48. package/_shims/manual-types.js +3 -0
  49. package/_shims/manual-types.mjs +3 -0
  50. package/_shims/node-runtime.d.ts +3 -0
  51. package/_shims/node-runtime.d.ts.map +1 -0
  52. package/_shims/node-runtime.js +88 -0
  53. package/_shims/node-runtime.js.map +1 -0
  54. package/_shims/node-runtime.mjs +56 -0
  55. package/_shims/node-runtime.mjs.map +1 -0
  56. package/_shims/node-types.d.ts +42 -0
  57. package/_shims/node-types.js +3 -0
  58. package/_shims/node-types.mjs +3 -0
  59. package/_shims/registry.d.ts +37 -0
  60. package/_shims/registry.d.ts.map +1 -0
  61. package/_shims/registry.js +41 -0
  62. package/_shims/registry.js.map +1 -0
  63. package/_shims/registry.mjs +37 -0
  64. package/_shims/registry.mjs.map +1 -0
  65. package/_shims/web-runtime.d.ts +5 -0
  66. package/_shims/web-runtime.d.ts.map +1 -0
  67. package/_shims/web-runtime.js +77 -0
  68. package/_shims/web-runtime.js.map +1 -0
  69. package/_shims/web-runtime.mjs +71 -0
  70. package/_shims/web-runtime.mjs.map +1 -0
  71. package/_shims/web-types.d.ts +83 -0
  72. package/_shims/web-types.js +3 -0
  73. package/_shims/web-types.mjs +3 -0
  74. package/core.d.ts +240 -0
  75. package/core.d.ts.map +1 -0
  76. package/core.js +903 -0
  77. package/core.js.map +1 -0
  78. package/core.mjs +871 -0
  79. package/core.mjs.map +1 -0
  80. package/error.d.ts +53 -0
  81. package/error.d.ts.map +1 -0
  82. package/error.js +143 -0
  83. package/error.js.map +1 -0
  84. package/error.mjs +127 -0
  85. package/error.mjs.map +1 -0
  86. package/index.d.mts +132 -0
  87. package/index.d.ts +132 -0
  88. package/index.d.ts.map +1 -0
  89. package/index.js +115 -0
  90. package/index.js.map +1 -0
  91. package/index.mjs +87 -0
  92. package/index.mjs.map +1 -0
  93. package/package.json +93 -31
  94. package/resource.d.ts +6 -0
  95. package/resource.d.ts.map +1 -0
  96. package/resource.js +11 -0
  97. package/resource.js.map +1 -0
  98. package/resource.mjs +7 -0
  99. package/resource.mjs.map +1 -0
  100. package/resources/contexts.d.ts +82 -0
  101. package/resources/contexts.d.ts.map +1 -0
  102. package/resources/contexts.js +29 -0
  103. package/resources/contexts.js.map +1 -0
  104. package/resources/contexts.mjs +25 -0
  105. package/resources/contexts.mjs.map +1 -0
  106. package/resources/extensions.d.ts +35 -0
  107. package/resources/extensions.d.ts.map +1 -0
  108. package/resources/extensions.js +56 -0
  109. package/resources/extensions.js.map +1 -0
  110. package/resources/extensions.mjs +29 -0
  111. package/resources/extensions.mjs.map +1 -0
  112. package/resources/index.d.ts +5 -0
  113. package/resources/index.d.ts.map +1 -0
  114. package/resources/index.js +13 -0
  115. package/resources/index.js.map +1 -0
  116. package/resources/index.mjs +6 -0
  117. package/resources/index.mjs.map +1 -0
  118. package/resources/projects.d.ts +36 -0
  119. package/resources/projects.d.ts.map +1 -0
  120. package/resources/projects.js +29 -0
  121. package/resources/projects.js.map +1 -0
  122. package/resources/projects.mjs +25 -0
  123. package/resources/projects.mjs.map +1 -0
  124. package/resources/sessions/downloads.d.ts +10 -0
  125. package/resources/sessions/downloads.d.ts.map +1 -0
  126. package/resources/sessions/downloads.js +15 -0
  127. package/resources/sessions/downloads.js.map +1 -0
  128. package/resources/sessions/downloads.mjs +11 -0
  129. package/resources/sessions/downloads.mjs.map +1 -0
  130. package/resources/sessions/index.d.ts +6 -0
  131. package/resources/sessions/index.d.ts.map +1 -0
  132. package/resources/sessions/index.js +15 -0
  133. package/resources/sessions/index.js.map +1 -0
  134. package/resources/sessions/index.mjs +7 -0
  135. package/resources/sessions/index.mjs.map +1 -0
  136. package/resources/sessions/logs.d.ts +47 -0
  137. package/resources/sessions/logs.d.ts.map +1 -0
  138. package/resources/sessions/logs.js +17 -0
  139. package/resources/sessions/logs.js.map +1 -0
  140. package/resources/sessions/logs.mjs +13 -0
  141. package/resources/sessions/logs.mjs.map +1 -0
  142. package/resources/sessions/recording.d.ts +29 -0
  143. package/resources/sessions/recording.d.ts.map +1 -0
  144. package/resources/sessions/recording.js +17 -0
  145. package/resources/sessions/recording.js.map +1 -0
  146. package/resources/sessions/recording.mjs +13 -0
  147. package/resources/sessions/recording.mjs.map +1 -0
  148. package/resources/sessions/sessions.d.ts +338 -0
  149. package/resources/sessions/sessions.d.ts.map +1 -0
  150. package/resources/sessions/sessions.js +80 -0
  151. package/resources/sessions/sessions.js.map +1 -0
  152. package/resources/sessions/sessions.mjs +53 -0
  153. package/resources/sessions/sessions.mjs.map +1 -0
  154. package/resources/sessions/uploads.d.ts +20 -0
  155. package/resources/sessions/uploads.d.ts.map +1 -0
  156. package/resources/sessions/uploads.js +41 -0
  157. package/resources/sessions/uploads.js.map +1 -0
  158. package/resources/sessions/uploads.mjs +14 -0
  159. package/resources/sessions/uploads.mjs.map +1 -0
  160. package/shims/node.d.ts +26 -0
  161. package/shims/node.d.ts.map +1 -0
  162. package/shims/node.js +6 -0
  163. package/shims/node.js.map +1 -0
  164. package/shims/node.mjs +4 -0
  165. package/shims/node.mjs.map +1 -0
  166. package/shims/web.d.ts +26 -0
  167. package/shims/web.d.ts.map +1 -0
  168. package/shims/web.js +6 -0
  169. package/shims/web.js.map +1 -0
  170. package/shims/web.mjs +4 -0
  171. package/shims/web.mjs.map +1 -0
  172. package/src/_shims/MultipartBody.ts +9 -0
  173. package/src/_shims/README.md +46 -0
  174. package/src/_shims/auto/runtime-bun.ts +4 -0
  175. package/src/_shims/auto/runtime-node.ts +4 -0
  176. package/src/_shims/auto/runtime.ts +4 -0
  177. package/src/_shims/auto/types-node.ts +4 -0
  178. package/src/_shims/auto/types.d.ts +101 -0
  179. package/src/_shims/auto/types.js +3 -0
  180. package/src/_shims/auto/types.mjs +3 -0
  181. package/src/_shims/bun-runtime.ts +14 -0
  182. package/src/_shims/index.d.ts +81 -0
  183. package/src/_shims/index.js +13 -0
  184. package/src/_shims/index.mjs +7 -0
  185. package/src/_shims/manual-types.d.ts +12 -0
  186. package/src/_shims/manual-types.js +3 -0
  187. package/src/_shims/manual-types.mjs +3 -0
  188. package/src/_shims/node-runtime.ts +81 -0
  189. package/src/_shims/node-types.d.ts +42 -0
  190. package/src/_shims/node-types.js +3 -0
  191. package/src/_shims/node-types.mjs +3 -0
  192. package/src/_shims/registry.ts +67 -0
  193. package/src/_shims/web-runtime.ts +103 -0
  194. package/src/_shims/web-types.d.ts +83 -0
  195. package/src/_shims/web-types.js +3 -0
  196. package/src/_shims/web-types.mjs +3 -0
  197. package/src/core.ts +1197 -0
  198. package/src/error.ts +146 -0
  199. package/src/index.ts +210 -0
  200. package/src/lib/.keep +4 -0
  201. package/src/resource.ts +11 -0
  202. package/src/resources/contexts.ts +108 -0
  203. package/src/resources/extensions.ts +55 -0
  204. package/src/resources/index.ts +21 -0
  205. package/src/resources/projects.ts +56 -0
  206. package/src/resources/sessions/downloads.ts +14 -0
  207. package/src/resources/sessions/index.ts +16 -0
  208. package/src/resources/sessions/logs.ts +68 -0
  209. package/src/resources/sessions/recording.ts +40 -0
  210. package/src/resources/sessions/sessions.ts +451 -0
  211. package/src/resources/sessions/uploads.ts +34 -0
  212. package/src/shims/node.ts +50 -0
  213. package/src/shims/web.ts +50 -0
  214. package/src/tsconfig.json +11 -0
  215. package/src/uploads.ts +256 -0
  216. package/src/version.ts +1 -0
  217. package/uploads.d.ts +75 -0
  218. package/uploads.d.ts.map +1 -0
  219. package/uploads.js +170 -0
  220. package/uploads.js.map +1 -0
  221. package/uploads.mjs +157 -0
  222. package/uploads.mjs.map +1 -0
  223. package/version.d.ts +2 -0
  224. package/version.d.ts.map +1 -0
  225. package/version.js +5 -0
  226. package/version.js.map +1 -0
  227. package/version.mjs +2 -0
  228. package/version.mjs.map +1 -0
  229. package/dist/index.d.ts +0 -138
  230. package/dist/index.js +0 -228
  231. package/dist/integrations/ai-sdk.d.ts +0 -18
  232. package/dist/integrations/ai-sdk.js +0 -19
  233. package/dist/recorder/call-metadata.d.ts +0 -66
  234. package/dist/recorder/call-metadata.js +0 -2
  235. package/dist/recorder/crypto.d.ts +0 -1
  236. package/dist/recorder/crypto.js +0 -10
  237. package/dist/recorder/css-utils.d.ts +0 -2
  238. package/dist/recorder/css-utils.js +0 -34
  239. package/dist/recorder/generator.d.ts +0 -31
  240. package/dist/recorder/generator.js +0 -154
  241. package/dist/recorder/javascript-generator.d.ts +0 -29
  242. package/dist/recorder/javascript-generator.js +0 -256
  243. package/dist/recorder/language.d.ts +0 -127
  244. package/dist/recorder/language.js +0 -21
  245. package/dist/recorder/recorder-types.d.ts +0 -108
  246. package/dist/recorder/recorder-types.js +0 -2
  247. package/dist/recorder/recorder.d.ts +0 -35
  248. package/dist/recorder/recorder.js +0 -333
  249. package/dist/recorder/time.d.ts +0 -1
  250. package/dist/recorder/time.js +0 -42
  251. package/dist/recorder/timeout-runner.d.ts +0 -6
  252. package/dist/recorder/timeout-runner.js +0 -19
  253. package/dist/recorder/utils/isomorphic/css-parser.d.ts +0 -41
  254. package/dist/recorder/utils/isomorphic/css-parser.js +0 -291
  255. package/dist/recorder/utils/isomorphic/css-tokenizer.d.ts +0 -171
  256. package/dist/recorder/utils/isomorphic/css-tokenizer.js +0 -986
  257. package/dist/recorder/utils/isomorphic/locator-generators.d.ts +0 -79
  258. package/dist/recorder/utils/isomorphic/locator-generators.js +0 -666
  259. package/dist/recorder/utils/isomorphic/locator-parser.d.ts +0 -17
  260. package/dist/recorder/utils/isomorphic/locator-parser.js +0 -244
  261. package/dist/recorder/utils/isomorphic/locator-utils.d.ts +0 -43
  262. package/dist/recorder/utils/isomorphic/locator-utils.js +0 -69
  263. package/dist/recorder/utils/isomorphic/selector-parser.d.ts +0 -48
  264. package/dist/recorder/utils/isomorphic/selector-parser.js +0 -483
  265. package/dist/recorder/utils/isomorphic/string-utils.d.ts +0 -28
  266. package/dist/recorder/utils/isomorphic/string-utils.js +0 -123
  267. package/dist/recorder/utils.d.ts +0 -20
  268. package/dist/recorder/utils.js +0 -32
@@ -1,108 +0,0 @@
1
- export type Point = {
2
- x: number;
3
- y: number;
4
- };
5
- export type ActionName = 'check' | 'click' | 'closePage' | 'fill' | 'navigate' | 'openPage' | 'press' | 'select' | 'uncheck' | 'setInputFiles' | 'assertText' | 'assertValue' | 'assertChecked' | 'assertVisible';
6
- export type ActionBase = {
7
- name: ActionName;
8
- signals: Signal[];
9
- };
10
- export type ClickAction = ActionBase & {
11
- name: 'click';
12
- selector: string;
13
- button: 'left' | 'middle' | 'right';
14
- modifiers: number;
15
- clickCount: number;
16
- position?: Point;
17
- };
18
- export type CheckAction = ActionBase & {
19
- name: 'check';
20
- selector: string;
21
- };
22
- export type UncheckAction = ActionBase & {
23
- name: 'uncheck';
24
- selector: string;
25
- };
26
- export type FillAction = ActionBase & {
27
- name: 'fill';
28
- selector: string;
29
- text: string;
30
- };
31
- export type NavigateAction = ActionBase & {
32
- name: 'navigate';
33
- url: string;
34
- };
35
- export type OpenPageAction = ActionBase & {
36
- name: 'openPage';
37
- url: string;
38
- };
39
- export type ClosesPageAction = ActionBase & {
40
- name: 'closePage';
41
- };
42
- export type PressAction = ActionBase & {
43
- name: 'press';
44
- selector: string;
45
- key: string;
46
- modifiers: number;
47
- };
48
- export type SelectAction = ActionBase & {
49
- name: 'select';
50
- selector: string;
51
- options: string[];
52
- };
53
- export type SetInputFilesAction = ActionBase & {
54
- name: 'setInputFiles';
55
- selector: string;
56
- files: string[];
57
- };
58
- export type AssertTextAction = ActionBase & {
59
- name: 'assertText';
60
- selector: string;
61
- text: string;
62
- substring: boolean;
63
- };
64
- export type AssertValueAction = ActionBase & {
65
- name: 'assertValue';
66
- selector: string;
67
- value: string;
68
- };
69
- export type AssertCheckedAction = ActionBase & {
70
- name: 'assertChecked';
71
- selector: string;
72
- checked: boolean;
73
- };
74
- export type AssertVisibleAction = ActionBase & {
75
- name: 'assertVisible';
76
- selector: string;
77
- };
78
- export type Action = ClickAction | CheckAction | ClosesPageAction | OpenPageAction | UncheckAction | FillAction | NavigateAction | PressAction | SelectAction | SetInputFilesAction | AssertTextAction | AssertValueAction | AssertCheckedAction | AssertVisibleAction;
79
- export type AssertAction = AssertCheckedAction | AssertValueAction | AssertTextAction | AssertVisibleAction;
80
- export type BaseSignal = {};
81
- export type NavigationSignal = BaseSignal & {
82
- name: 'navigation';
83
- url: string;
84
- };
85
- export type PopupSignal = BaseSignal & {
86
- name: 'popup';
87
- popupAlias: string;
88
- };
89
- export type DownloadSignal = BaseSignal & {
90
- name: 'download';
91
- downloadAlias: string;
92
- };
93
- export type DialogSignal = BaseSignal & {
94
- name: 'dialog';
95
- dialogAlias: string;
96
- };
97
- export type Signal = NavigationSignal | PopupSignal | DownloadSignal | DialogSignal;
98
- type FrameDescriptionMainFrame = {
99
- isMainFrame: true;
100
- };
101
- type FrameDescriptionChildFrame = {
102
- isMainFrame: false;
103
- selectorsChain: string[];
104
- };
105
- export type FrameDescription = {
106
- pageAlias: string;
107
- } & (FrameDescriptionMainFrame | FrameDescriptionChildFrame);
108
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,35 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- import type { BrowserContext } from 'playwright-core';
3
- export type GeneratorOptions = {
4
- enabled?: boolean;
5
- };
6
- export type ActionsEvent = {
7
- actions: string[];
8
- };
9
- export declare class BrowserbaseCodeGenerator extends EventEmitter {
10
- private _context;
11
- private _skipVerify;
12
- private _enabled;
13
- private _generator;
14
- private _pageAliases;
15
- private _lastPopupOrdinal;
16
- private _lastDialogOrdinal;
17
- private _lastDownloadOrdinal;
18
- private _timers;
19
- private _javascriptGenerator;
20
- constructor(context: BrowserContext, options?: GeneratorOptions);
21
- static init(context: BrowserContext, options?: GeneratorOptions): Promise<BrowserbaseCodeGenerator>;
22
- setEnabled(enabled: boolean): void;
23
- install(): Promise<void>;
24
- private _onDialog;
25
- private _ensureCodegenEnabled;
26
- private _onPage;
27
- private _describeMainFrame;
28
- private _describeFrame;
29
- private _performAction;
30
- private _recordAction;
31
- private _setCommittedAfterTimeout;
32
- private _onFrameNavigated;
33
- private _onPopup;
34
- private _onDownload;
35
- }
@@ -1,333 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BrowserbaseCodeGenerator = void 0;
4
- const events_1 = require("events");
5
- const generator_1 = require("./generator");
6
- const time_1 = require("./time");
7
- const timeout_runner_1 = require("./timeout-runner");
8
- const css_utils_1 = require("./css-utils");
9
- const crypto_1 = require("./crypto");
10
- const utils_1 = require("./utils");
11
- const javascript_generator_1 = require("./javascript-generator");
12
- class BrowserbaseCodeGenerator extends events_1.EventEmitter {
13
- _context;
14
- // This is used in the playground to bypass the verification step since it doesnt work
15
- // in the browser
16
- _skipVerify = false;
17
- _enabled;
18
- _generator;
19
- _pageAliases = new Map();
20
- _lastPopupOrdinal = 0;
21
- _lastDialogOrdinal = -1;
22
- _lastDownloadOrdinal = -1;
23
- _timers = new Set();
24
- _javascriptGenerator;
25
- constructor(context, options) {
26
- super();
27
- this._context = context;
28
- this._enabled = options?.enabled ?? false;
29
- this._generator = new generator_1.CodeGenerator('chromium', this._enabled, {}, {}, undefined, undefined);
30
- this._javascriptGenerator = new javascript_generator_1.JavaScriptLanguageGenerator(false);
31
- this._generator.on('change', () => {
32
- const { header, footer, actions, text } = this._generator.generateStructure(this._javascriptGenerator);
33
- this.emit('action', { actions });
34
- });
35
- }
36
- static async init(context, options) {
37
- const browserbaseCodeGenerator = new BrowserbaseCodeGenerator(context, options);
38
- await browserbaseCodeGenerator.install();
39
- return browserbaseCodeGenerator;
40
- }
41
- setEnabled(enabled) {
42
- this._enabled = enabled;
43
- this._generator.setEnabled(enabled);
44
- }
45
- async install() {
46
- if (!this._skipVerify) {
47
- const codegenMode = await this._ensureCodegenEnabled();
48
- if (!codegenMode) {
49
- throw new Error('Failed to install generator: codegenMode not enabled on this session');
50
- }
51
- }
52
- this._context.on('page', (page) => {
53
- this._onPage(page);
54
- });
55
- for (const page of this._context.pages()) {
56
- this._onPage(page);
57
- }
58
- this._context.on('dialog', (dialog) => {
59
- this._onDialog(dialog.page());
60
- });
61
- await this._context.exposeBinding('__bb_recorderPerformAction', (source, action) => this._performAction(source.frame, action));
62
- await this._context.exposeBinding('__bb_recorderRecordAction', (source, action) => this._recordAction(source.frame, action));
63
- await this._context.exposeBinding('__bb_recorderEnabled', (_source) => true);
64
- }
65
- _onDialog(page) {
66
- const pageAlias = this._pageAliases.get(page);
67
- ++this._lastDialogOrdinal;
68
- this._generator.signal(pageAlias, page.mainFrame(), {
69
- name: 'dialog',
70
- dialogAlias: this._lastDialogOrdinal
71
- ? String(this._lastDialogOrdinal)
72
- : '',
73
- });
74
- }
75
- // when a browser is created with record mode it will add a global variable to the page
76
- // this function checks if the global variable is present and returns true if it is
77
- async _ensureCodegenEnabled() {
78
- const enabledState = this._enabled;
79
- // disable generator so we dont capture these actions
80
- if (enabledState) {
81
- this.setEnabled(false);
82
- }
83
- const page = await this._context.newPage();
84
- await page.waitForLoadState('domcontentloaded');
85
- const codegenMode = await page.evaluate(() => {
86
- // @ts-expect-error
87
- if (window.__bb_codegenEnabled) {
88
- // @ts-expect-error
89
- return window.__bb_codegenEnabled();
90
- }
91
- return false;
92
- });
93
- await page.close();
94
- // re-enable generator
95
- if (enabledState) {
96
- this.setEnabled(true);
97
- }
98
- return codegenMode;
99
- }
100
- async _onPage(page) {
101
- const frame = page.mainFrame();
102
- page.on('close', () => {
103
- this._generator.addAction({
104
- frame: this._describeMainFrame(page),
105
- committed: true,
106
- action: {
107
- name: 'closePage',
108
- signals: [],
109
- },
110
- });
111
- this._pageAliases.delete(page);
112
- });
113
- // @ts-ignore
114
- frame._eventEmitter.on('navigated', (event) => {
115
- this._onFrameNavigated(frame, page);
116
- });
117
- page.on('download', () => this._onDownload(page));
118
- const suffix = this._pageAliases.size
119
- ? String(++this._lastPopupOrdinal)
120
- : '';
121
- const pageAlias = 'page' + suffix;
122
- this._pageAliases.set(page, pageAlias);
123
- const opener = await page.opener();
124
- if (opener) {
125
- this._onPopup(opener, page);
126
- }
127
- else {
128
- this._generator.addAction({
129
- frame: this._describeMainFrame(page),
130
- committed: true,
131
- action: {
132
- name: 'openPage',
133
- url: page.mainFrame().url(),
134
- signals: [],
135
- },
136
- });
137
- }
138
- }
139
- _describeMainFrame(page) {
140
- return {
141
- pageAlias: this._pageAliases.get(page),
142
- isMainFrame: true,
143
- };
144
- }
145
- async _describeFrame(frame) {
146
- // @ts-expect-error
147
- const page = frame._page;
148
- const pageAlias = this._pageAliases.get(page);
149
- const chain = [];
150
- for (let ancestor = frame; ancestor; ancestor = ancestor.parentFrame())
151
- chain.push(ancestor);
152
- chain.reverse();
153
- if (chain.length === 1)
154
- return this._describeMainFrame(page);
155
- const selectorPromises = [];
156
- for (let i = 0; i < chain.length - 1; i++)
157
- selectorPromises.push(findFrameSelector(chain[i + 1]));
158
- const result = await (0, timeout_runner_1.raceAgainstDeadline)(() => Promise.all(selectorPromises), (0, time_1.monotonicTime)() + 2000);
159
- if (!result.timedOut &&
160
- // @ts-expect-error
161
- result.result.every((selector) => !!selector)) {
162
- return {
163
- pageAlias,
164
- isMainFrame: false,
165
- // @ts-expect-error
166
- selectorsChain: result.result,
167
- };
168
- }
169
- // Best effort to find a selector for the frame.
170
- const selectorsChain = [];
171
- for (let i = 0; i < chain.length - 1; i++) {
172
- if (chain[i].name())
173
- selectorsChain.push(`iframe[name=${(0, css_utils_1.quoteCSSAttributeValue)(chain[i].name())}]`);
174
- else
175
- selectorsChain.push(`iframe[src=${(0, css_utils_1.quoteCSSAttributeValue)(chain[i].url())}]`);
176
- }
177
- return {
178
- pageAlias,
179
- isMainFrame: false,
180
- selectorsChain,
181
- };
182
- }
183
- async _performAction(frame, action) {
184
- // console.log('perform action', action)
185
- // Commit last action so that no further signals are added to it.
186
- this._generator.commitLastAction();
187
- const frameDescription = await this._describeFrame(frame);
188
- const actionInContext = {
189
- frame: frameDescription,
190
- action,
191
- };
192
- const perform = async (action, params, cb) => {
193
- const callMetadata = {
194
- id: `call@${(0, crypto_1.createGuid)()}`,
195
- apiName: 'frame.' + action,
196
- // @ts-expect-error
197
- objectId: frame._guid,
198
- // @ts-expect-error
199
- pageId: frame._page._guid,
200
- // @ts-expect-error
201
- frameId: frame._guid,
202
- startTime: (0, time_1.monotonicTime)(),
203
- endTime: 0,
204
- type: 'Frame',
205
- method: action,
206
- params,
207
- log: [],
208
- };
209
- this._generator.willPerformAction(actionInContext);
210
- //// console.log('will perform action', actionInContext)
211
- try {
212
- //await frame.instrumentation.onBeforeCall(frame, callMetadata)
213
- await cb(callMetadata);
214
- }
215
- catch (e) {
216
- console.error('error performing action', e);
217
- callMetadata.endTime = (0, time_1.monotonicTime)();
218
- //await frame.instrumentation.onAfterCall(frame, callMetadata)
219
- this._generator.performedActionFailed(actionInContext);
220
- return;
221
- }
222
- callMetadata.endTime = (0, time_1.monotonicTime)();
223
- //await frame.instrumentation.onAfterCall(frame, callMetadata)
224
- this._setCommittedAfterTimeout(actionInContext);
225
- this._generator.didPerformAction(actionInContext);
226
- //console.log('did perform action', actionInContext)
227
- };
228
- const kActionTimeout = 5000;
229
- if (action.name === 'click') {
230
- const { options } = (0, utils_1.toClickOptions)(action);
231
- await perform('click', { selector: action.selector }, (callMetadata) => {
232
- return frame.click(action.selector, {
233
- ...options,
234
- force: true,
235
- timeout: kActionTimeout,
236
- strict: true,
237
- });
238
- });
239
- }
240
- if (action.name === 'press') {
241
- const modifiers = (0, utils_1.toModifiers)(action.modifiers);
242
- const shortcut = [...modifiers, action.key].join('+');
243
- await perform('press', { selector: action.selector, key: shortcut }, (callMetadata) => frame.press(action.selector, shortcut, {
244
- timeout: kActionTimeout,
245
- strict: true,
246
- }));
247
- }
248
- if (action.name === 'check')
249
- await perform('check', { selector: action.selector }, (callMetadata) => frame.check(action.selector, {
250
- timeout: kActionTimeout,
251
- strict: true,
252
- }));
253
- if (action.name === 'uncheck')
254
- await perform('uncheck', { selector: action.selector }, (callMetadata) => frame.uncheck(action.selector, {
255
- timeout: kActionTimeout,
256
- strict: true,
257
- }));
258
- if (action.name === 'select') {
259
- const values = action.options.map((value) => ({ value }));
260
- await perform('selectOption', { selector: action.selector, values }, (callMetadata) => frame.selectOption(action.selector, values, {
261
- timeout: kActionTimeout,
262
- strict: true,
263
- }));
264
- }
265
- }
266
- async _recordAction(frame, action) {
267
- // console.log('record action', action)
268
- // Commit last action so that no further signals are added to it.
269
- this._generator.commitLastAction();
270
- // console.log('committed action')
271
- const frameDescription = await this._describeFrame(frame);
272
- // console.log('described frame')
273
- const actionInContext = {
274
- frame: frameDescription,
275
- action,
276
- };
277
- this._setCommittedAfterTimeout(actionInContext);
278
- // console.log('committed after timeout')
279
- this._generator.addAction(actionInContext);
280
- // console.log('action added')
281
- }
282
- _setCommittedAfterTimeout(actionInContext) {
283
- const timer = setTimeout(() => {
284
- // Commit the action after 5 seconds so that no further signals are added to it.
285
- actionInContext.committed = true;
286
- this._timers.delete(timer);
287
- }, 5000);
288
- this._timers.add(timer);
289
- }
290
- _onFrameNavigated(frame, page) {
291
- const pageAlias = this._pageAliases.get(page);
292
- this._generator.signal(pageAlias, frame, {
293
- name: 'navigation',
294
- url: frame.url(),
295
- });
296
- }
297
- _onPopup(page, popup) {
298
- const pageAlias = this._pageAliases.get(page);
299
- const popupAlias = this._pageAliases.get(popup);
300
- this._generator.signal(pageAlias, page.mainFrame(), {
301
- name: 'popup',
302
- popupAlias,
303
- });
304
- }
305
- _onDownload(page) {
306
- const pageAlias = this._pageAliases.get(page);
307
- ++this._lastDownloadOrdinal;
308
- this._generator.signal(pageAlias, page.mainFrame(), {
309
- name: 'download',
310
- downloadAlias: this._lastDownloadOrdinal
311
- ? String(this._lastDownloadOrdinal)
312
- : '',
313
- });
314
- }
315
- }
316
- exports.BrowserbaseCodeGenerator = BrowserbaseCodeGenerator;
317
- async function findFrameSelector(frame) {
318
- try {
319
- const parent = frame.parentFrame();
320
- const frameElement = await frame.frameElement();
321
- if (!frameElement || !parent)
322
- return;
323
- const selector = await parent.evaluate((element) => {
324
- //@ts-expect-error
325
- return window.__bb_injectedScript.generateSelectorSimple(element, {
326
- testIdAttributeName: '',
327
- omitInternalEngines: true,
328
- });
329
- }, frameElement);
330
- return selector;
331
- }
332
- catch (e) { }
333
- }
@@ -1 +0,0 @@
1
- export declare function monotonicTime(): number;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.monotonicTime = monotonicTime;
4
- // polyfil for window.performance.now
5
- var performance = global.performance || {};
6
- var performanceNow =
7
- // @ts-expect-error
8
- performance.now ||
9
- // @ts-expect-error
10
- performance.mozNow ||
11
- // @ts-expect-error
12
- performance.msNow ||
13
- // @ts-expect-error
14
- performance.oNow ||
15
- // @ts-expect-error
16
- performance.webkitNow ||
17
- function () {
18
- return new Date().getTime();
19
- };
20
- // generate timestamp or delta
21
- // see http://nodejs.org/api/process.html#process_process_hrtime
22
- function _hrtime(previousTimestamp) {
23
- var clocktime = performanceNow.call(performance) * 1e-3;
24
- var seconds = Math.floor(clocktime);
25
- var nanoseconds = Math.floor((clocktime % 1) * 1e9);
26
- if (previousTimestamp) {
27
- seconds = seconds - previousTimestamp[0];
28
- nanoseconds = nanoseconds - previousTimestamp[1];
29
- if (nanoseconds < 0) {
30
- seconds--;
31
- nanoseconds += 1e9;
32
- }
33
- }
34
- return [seconds, nanoseconds];
35
- }
36
- const hrtime = process.hrtime || _hrtime;
37
- // @ts-expect-error
38
- const initialTime = hrtime();
39
- function monotonicTime() {
40
- const [seconds, nanoseconds] = hrtime(initialTime);
41
- return seconds * 1000 + ((nanoseconds / 1000) | 0) / 1000;
42
- }
@@ -1,6 +0,0 @@
1
- export declare function raceAgainstDeadline<T>(cb: () => Promise<T>, deadline: number): Promise<{
2
- result: T;
3
- timedOut: false;
4
- } | {
5
- timedOut: true;
6
- }>;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.raceAgainstDeadline = raceAgainstDeadline;
4
- const time_1 = require("./time");
5
- async function raceAgainstDeadline(cb, deadline) {
6
- let timer;
7
- return Promise.race([
8
- cb().then((result) => {
9
- return { result, timedOut: false };
10
- }),
11
- new Promise((resolve) => {
12
- const kMaxDeadline = 2147483647; // 2^31-1
13
- const timeout = (deadline || kMaxDeadline) - (0, time_1.monotonicTime)();
14
- timer = setTimeout(() => resolve({ timedOut: true }), timeout);
15
- }),
16
- ]).finally(() => {
17
- clearTimeout(timer);
18
- });
19
- }
@@ -1,41 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- export declare class InvalidSelectorError extends Error {
17
- }
18
- export declare function isInvalidSelectorError(error: Error): error is InvalidSelectorError;
19
- type ClauseCombinator = '' | '>' | '+' | '~' | '>=';
20
- export type CSSFunctionArgument = CSSComplexSelector | number | string;
21
- export type CSSFunction = {
22
- name: string;
23
- args: CSSFunctionArgument[];
24
- };
25
- export type CSSSimpleSelector = {
26
- css?: string;
27
- functions: CSSFunction[];
28
- };
29
- export type CSSComplexSelector = {
30
- simples: {
31
- selector: CSSSimpleSelector;
32
- combinator: ClauseCombinator;
33
- }[];
34
- };
35
- export type CSSComplexSelectorList = CSSComplexSelector[];
36
- export declare function parseCSS(selector: string, customNames: Set<string>): {
37
- selector: CSSComplexSelectorList;
38
- names: string[];
39
- };
40
- export declare function serializeSelector(args: CSSFunctionArgument[]): string;
41
- export {};