@browserbasehq/sdk 1.5.0 → 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 -76
  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,10 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createGuid = createGuid;
7
- const crypto_1 = __importDefault(require("crypto"));
8
- function createGuid() {
9
- return crypto_1.default.randomBytes(16).toString('hex');
10
- }
@@ -1,2 +0,0 @@
1
- export declare function cssEscape(s: string): string;
2
- export declare function quoteCSSAttributeValue(text: string): string;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cssEscape = cssEscape;
4
- exports.quoteCSSAttributeValue = quoteCSSAttributeValue;
5
- function cssEscape(s) {
6
- let result = '';
7
- for (let i = 0; i < s.length; i++)
8
- result += cssEscapeOne(s, i);
9
- return result;
10
- }
11
- function quoteCSSAttributeValue(text) {
12
- return `"${cssEscape(text).replace(/\\ /g, ' ')}"`;
13
- }
14
- function cssEscapeOne(s, i) {
15
- // https://drafts.csswg.org/cssom/#serialize-an-identifier
16
- const c = s.charCodeAt(i);
17
- if (c === 0x0000)
18
- return '\uFFFD';
19
- if ((c >= 0x0001 && c <= 0x001f) ||
20
- (c >= 0x0030 &&
21
- c <= 0x0039 &&
22
- (i === 0 || (i === 1 && s.charCodeAt(0) === 0x002d))))
23
- return '\\' + c.toString(16) + ' ';
24
- if (i === 0 && c === 0x002d && s.length === 1)
25
- return '\\' + s.charAt(i);
26
- if (c >= 0x0080 ||
27
- c === 0x002d ||
28
- c === 0x005f ||
29
- (c >= 0x0030 && c <= 0x0039) ||
30
- (c >= 0x0041 && c <= 0x005a) ||
31
- (c >= 0x0061 && c <= 0x007a))
32
- return s.charAt(i);
33
- return '\\' + s.charAt(i);
34
- }
@@ -1,31 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- import type { BrowserContextOptions, LaunchOptions, Frame } from 'playwright-core';
3
- import type { LanguageGenerator } from './language';
4
- import type { Action, Signal, FrameDescription } from './recorder-types';
5
- export type ActionInContext = {
6
- frame: FrameDescription;
7
- action: Action;
8
- committed?: boolean;
9
- };
10
- export declare class CodeGenerator extends EventEmitter {
11
- private _currentAction;
12
- private _lastAction;
13
- private _actions;
14
- private _enabled;
15
- private _options;
16
- constructor(browserName: string, enabled: boolean, launchOptions: LaunchOptions, contextOptions: BrowserContextOptions, deviceName: string | undefined, saveStorage: string | undefined);
17
- restart(): void;
18
- setEnabled(enabled: boolean): void;
19
- addAction(action: ActionInContext): void;
20
- willPerformAction(action: ActionInContext): void;
21
- performedActionFailed(action: ActionInContext): void;
22
- didPerformAction(actionInContext: ActionInContext): void;
23
- commitLastAction(): void;
24
- signal(pageAlias: string, frame: Frame, signal: Signal): void;
25
- generateStructure(languageGenerator: LanguageGenerator): {
26
- header: string;
27
- footer: string;
28
- actions: string[];
29
- text: string;
30
- };
31
- }
@@ -1,154 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CodeGenerator = void 0;
4
- const events_1 = require("events");
5
- class CodeGenerator extends events_1.EventEmitter {
6
- _currentAction = null;
7
- _lastAction = null;
8
- _actions = [];
9
- _enabled;
10
- _options;
11
- constructor(browserName, enabled, launchOptions, contextOptions, deviceName, saveStorage) {
12
- super();
13
- // Make a copy of options to modify them later.
14
- launchOptions = { headless: false, ...launchOptions };
15
- contextOptions = { ...contextOptions };
16
- this._enabled = enabled;
17
- this._options = {
18
- browserName,
19
- launchOptions,
20
- contextOptions,
21
- deviceName,
22
- saveStorage,
23
- };
24
- this.restart();
25
- }
26
- restart() {
27
- this._currentAction = null;
28
- this._lastAction = null;
29
- this._actions = [];
30
- this.emit('change');
31
- }
32
- setEnabled(enabled) {
33
- this._enabled = enabled;
34
- }
35
- addAction(action) {
36
- if (!this._enabled)
37
- return;
38
- this.willPerformAction(action);
39
- this.didPerformAction(action);
40
- }
41
- willPerformAction(action) {
42
- if (!this._enabled)
43
- return;
44
- this._currentAction = action;
45
- }
46
- performedActionFailed(action) {
47
- if (!this._enabled)
48
- return;
49
- if (this._currentAction === action)
50
- this._currentAction = null;
51
- }
52
- didPerformAction(actionInContext) {
53
- if (!this._enabled)
54
- return;
55
- const action = actionInContext.action;
56
- let eraseLastAction = false;
57
- if (this._lastAction &&
58
- this._lastAction.frame.pageAlias === actionInContext.frame.pageAlias) {
59
- const lastAction = this._lastAction.action;
60
- // We augment last action based on the type.
61
- if (this._lastAction &&
62
- action.name === 'fill' &&
63
- lastAction.name === 'fill') {
64
- if (action.selector === lastAction.selector)
65
- eraseLastAction = true;
66
- }
67
- if (lastAction &&
68
- action.name === 'click' &&
69
- lastAction.name === 'click') {
70
- if (action.selector === lastAction.selector &&
71
- action.clickCount > lastAction.clickCount)
72
- eraseLastAction = true;
73
- }
74
- if (lastAction &&
75
- action.name === 'navigate' &&
76
- lastAction.name === 'navigate') {
77
- if (action.url === lastAction.url) {
78
- // Already at a target URL.
79
- this._currentAction = null;
80
- return;
81
- }
82
- }
83
- // Check and uncheck erase click.
84
- if (lastAction &&
85
- (action.name === 'check' || action.name === 'uncheck') &&
86
- lastAction.name === 'click') {
87
- if (action.selector === lastAction.selector)
88
- eraseLastAction = true;
89
- }
90
- }
91
- this._lastAction = actionInContext;
92
- this._currentAction = null;
93
- if (eraseLastAction)
94
- this._actions.pop();
95
- this._actions.push(actionInContext);
96
- this.emit('change');
97
- }
98
- commitLastAction() {
99
- if (!this._enabled)
100
- return;
101
- const action = this._lastAction;
102
- if (action)
103
- action.committed = true;
104
- }
105
- signal(pageAlias, frame, signal) {
106
- if (!this._enabled)
107
- return;
108
- // Signal either arrives while action is being performed or shortly after.
109
- if (this._currentAction) {
110
- this._currentAction.action.signals.push(signal);
111
- return;
112
- }
113
- if (this._lastAction &&
114
- (!this._lastAction.committed || signal.name !== 'navigation')) {
115
- const signals = this._lastAction.action.signals;
116
- if (signal.name === 'navigation' &&
117
- signals.length &&
118
- signals[signals.length - 1].name === 'download')
119
- return;
120
- if (signal.name === 'download' &&
121
- signals.length &&
122
- signals[signals.length - 1].name === 'navigation')
123
- signals.length = signals.length - 1;
124
- this._lastAction.action.signals.push(signal);
125
- this.emit('change');
126
- return;
127
- }
128
- // @ts-ignore
129
- if (signal.name === 'navigation' && frame.page().mainFrame() === frame) {
130
- this.addAction({
131
- frame: {
132
- pageAlias,
133
- isMainFrame: true,
134
- },
135
- committed: true,
136
- action: {
137
- name: 'navigate',
138
- url: frame.url(),
139
- signals: [],
140
- },
141
- });
142
- }
143
- }
144
- generateStructure(languageGenerator) {
145
- const header = languageGenerator.generateHeader(this._options);
146
- const footer = languageGenerator.generateFooter(this._options.saveStorage);
147
- const actions = this._actions
148
- .map((a) => languageGenerator.generateAction(a))
149
- .filter(Boolean);
150
- const text = [header, ...actions, footer].join('\n');
151
- return { header, footer, actions, text };
152
- }
153
- }
154
- exports.CodeGenerator = CodeGenerator;
@@ -1,29 +0,0 @@
1
- import type { Language, LanguageGenerator, LanguageGeneratorOptions } from './language';
2
- import type { ActionInContext } from './generator';
3
- export declare class JavaScriptLanguageGenerator implements LanguageGenerator {
4
- id: string;
5
- groupName: string;
6
- name: string;
7
- highlighter: Language;
8
- private _isTest;
9
- constructor(isTest: boolean);
10
- generateAction(actionInContext: ActionInContext): string;
11
- private _generateActionCall;
12
- private _asLocator;
13
- generateHeader(options: LanguageGeneratorOptions): string;
14
- generateFooter(saveStorage: string | undefined): string;
15
- generateTestHeader(options: LanguageGeneratorOptions): string;
16
- generateTestFooter(saveStorage: string | undefined): string;
17
- generateStandaloneHeader(options: LanguageGeneratorOptions): string;
18
- generateStandaloneFooter(saveStorage: string | undefined): string;
19
- }
20
- export declare class JavaScriptFormatter {
21
- private _baseIndent;
22
- private _baseOffset;
23
- private _lines;
24
- constructor(offset?: number);
25
- prepend(text: string): void;
26
- add(text: string): void;
27
- newLine(): void;
28
- format(): string;
29
- }
@@ -1,256 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JavaScriptFormatter = exports.JavaScriptLanguageGenerator = void 0;
4
- const language_1 = require("./language");
5
- const utils_1 = require("./utils");
6
- // TODO: rename to files
7
- const locator_generators_1 = require("./utils/isomorphic/locator-generators");
8
- const string_utils_1 = require("./utils/isomorphic/string-utils");
9
- class JavaScriptLanguageGenerator {
10
- id;
11
- groupName = 'Node.js';
12
- name;
13
- highlighter = 'javascript';
14
- _isTest;
15
- constructor(isTest) {
16
- this.id = isTest ? 'playwright-test' : 'javascript';
17
- this.name = isTest ? 'Test Runner' : 'Library';
18
- this._isTest = isTest;
19
- }
20
- generateAction(actionInContext) {
21
- const action = actionInContext.action;
22
- if (this._isTest &&
23
- (action.name === 'openPage' || action.name === 'closePage'))
24
- return '';
25
- const pageAlias = actionInContext.frame.pageAlias;
26
- const formatter = new JavaScriptFormatter(2);
27
- if (action.name === 'openPage') {
28
- formatter.add(`const ${pageAlias} = await context.newPage();`);
29
- if (action.url &&
30
- action.url !== 'about:blank' &&
31
- action.url !== 'chrome://newtab/')
32
- formatter.add(`await ${pageAlias}.goto(${quote(action.url)});`);
33
- return formatter.format();
34
- }
35
- let subject;
36
- if (actionInContext.frame.isMainFrame) {
37
- subject = pageAlias;
38
- }
39
- else {
40
- // @ts-expect-error
41
- const locators = actionInContext.frame.selectorsChain.map((selector) => `.frameLocator(${quote(selector)})`);
42
- subject = `${pageAlias}${locators.join('')}`;
43
- }
44
- const signals = (0, language_1.toSignalMap)(action);
45
- if (signals.dialog) {
46
- formatter.add(` ${pageAlias}.once('dialog', dialog => {
47
- console.log(\`Dialog message: $\{dialog.message()}\`);
48
- dialog.dismiss().catch(() => {});
49
- });`);
50
- }
51
- if (signals.popup)
52
- formatter.add(`const ${signals.popup.popupAlias}Promise = ${pageAlias}.waitForEvent('popup');`);
53
- if (signals.download)
54
- formatter.add(`const download${signals.download.downloadAlias}Promise = ${pageAlias}.waitForEvent('download');`);
55
- formatter.add(this._generateActionCall(subject, action));
56
- if (signals.popup)
57
- formatter.add(`const ${signals.popup.popupAlias} = await ${signals.popup.popupAlias}Promise;`);
58
- if (signals.download)
59
- formatter.add(`const download${signals.download.downloadAlias} = await download${signals.download.downloadAlias}Promise;`);
60
- return formatter.format();
61
- }
62
- _generateActionCall(subject, action) {
63
- switch (action.name) {
64
- case 'openPage':
65
- throw Error('Not reached');
66
- case 'closePage':
67
- return `await ${subject}.close();`;
68
- case 'click': {
69
- let method = 'click';
70
- if (action.clickCount === 2)
71
- method = 'dblclick';
72
- const modifiers = (0, utils_1.toModifiers)(action.modifiers);
73
- const options = {};
74
- if (action.button !== 'left')
75
- options.button = action.button;
76
- if (modifiers.length)
77
- options.modifiers = modifiers;
78
- if (action.clickCount > 2)
79
- options.clickCount = action.clickCount;
80
- if (action.position)
81
- options.position = action.position;
82
- const optionsString = formatOptions(options, false);
83
- return `await ${subject}.${this._asLocator(action.selector)}.${method}(${optionsString});`;
84
- }
85
- case 'check':
86
- return `await ${subject}.${this._asLocator(action.selector)}.check();`;
87
- case 'uncheck':
88
- return `await ${subject}.${this._asLocator(action.selector)}.uncheck();`;
89
- case 'fill':
90
- return `await ${subject}.${this._asLocator(action.selector)}.fill(${quote(action.text)});`;
91
- case 'setInputFiles':
92
- return `await ${subject}.${this._asLocator(action.selector)}.setInputFiles(${formatObject(action.files.length === 1 ? action.files[0] : action.files)});`;
93
- case 'press': {
94
- const modifiers = (0, utils_1.toModifiers)(action.modifiers);
95
- const shortcut = [...modifiers, action.key].join('+');
96
- return `await ${subject}.${this._asLocator(action.selector)}.press(${quote(shortcut)});`;
97
- }
98
- case 'navigate':
99
- return `await ${subject}.goto(${quote(action.url)});`;
100
- case 'select':
101
- return `await ${subject}.${this._asLocator(action.selector)}.selectOption(${formatObject(action.options.length > 1 ? action.options : action.options[0])});`;
102
- case 'assertText':
103
- return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${action.substring ? 'toContainText' : 'toHaveText'}(${quote(action.text)});`;
104
- case 'assertChecked':
105
- return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)})${action.checked ? '' : '.not'}.toBeChecked();`;
106
- case 'assertVisible':
107
- return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).toBeVisible();`;
108
- case 'assertValue': {
109
- const assertion = action.value
110
- ? `toHaveValue(${quote(action.value)})`
111
- : `toBeEmpty()`;
112
- return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${assertion};`;
113
- }
114
- }
115
- }
116
- _asLocator(selector) {
117
- return (0, locator_generators_1.asLocator)('javascript', selector);
118
- }
119
- generateHeader(options) {
120
- if (this._isTest)
121
- return this.generateTestHeader(options);
122
- return this.generateStandaloneHeader(options);
123
- }
124
- generateFooter(saveStorage) {
125
- if (this._isTest)
126
- return this.generateTestFooter(saveStorage);
127
- return this.generateStandaloneFooter(saveStorage);
128
- }
129
- generateTestHeader(options) {
130
- const formatter = new JavaScriptFormatter();
131
- const useText = formatContextOptions(options.contextOptions, options.deviceName);
132
- formatter.add(`
133
- import { test, expect${options.deviceName ? ', devices' : ''} } from '@playwright/test';
134
- ${useText ? '\ntest.use(' + useText + ');\n' : ''}
135
- test('test', async ({ page }) => {`);
136
- return formatter.format();
137
- }
138
- generateTestFooter(saveStorage) {
139
- return `});`;
140
- }
141
- // TODO: fix header
142
- generateStandaloneHeader(options) {
143
- const formatter = new JavaScriptFormatter();
144
- formatter.add(`
145
- const { ${options.browserName}${options.deviceName ? ', devices' : ''} } = require('playwright');
146
-
147
- (async () => {
148
- const browser = await ${options.browserName}.launch(${formatObjectOrVoid(options.launchOptions)});
149
- const context = await browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName)});`);
150
- return formatter.format();
151
- }
152
- generateStandaloneFooter(saveStorage) {
153
- const storageStateLine = saveStorage
154
- ? `\n await context.storageState({ path: ${quote(saveStorage)} });`
155
- : '';
156
- return `\n // ---------------------${storageStateLine}
157
- await context.close();
158
- await browser.close();
159
- })();`;
160
- }
161
- }
162
- exports.JavaScriptLanguageGenerator = JavaScriptLanguageGenerator;
163
- function formatOptions(value, hasArguments) {
164
- const keys = Object.keys(value);
165
- if (!keys.length)
166
- return '';
167
- return (hasArguments ? ', ' : '') + formatObject(value);
168
- }
169
- function formatObject(value, indent = ' ') {
170
- if (typeof value === 'string')
171
- return quote(value);
172
- if (Array.isArray(value))
173
- return `[${value.map((o) => formatObject(o)).join(', ')}]`;
174
- if (typeof value === 'object') {
175
- const keys = Object.keys(value)
176
- .filter((key) => value[key] !== undefined)
177
- .sort();
178
- if (!keys.length)
179
- return '{}';
180
- const tokens = [];
181
- for (const key of keys)
182
- tokens.push(`${key}: ${formatObject(value[key])}`);
183
- return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
184
- }
185
- return String(value);
186
- }
187
- function formatObjectOrVoid(value, indent = ' ') {
188
- const result = formatObject(value, indent);
189
- return result === '{}' ? '' : result;
190
- }
191
- // NOTE: disabled device for now to simplify build
192
- function formatContextOptions(options, deviceName) {
193
- // const device = deviceName && deviceDescriptors[deviceName]
194
- // if (!device) return formatObjectOrVoid(options)
195
- return formatObjectOrVoid(options);
196
- // Filter out all the properties from the device descriptor.
197
- // let serializedObject = formatObjectOrVoid(
198
- // sanitizeDeviceOptions(device, options)
199
- // )
200
- // // When there are no additional context options, we still want to spread the device inside.
201
- // if (!serializedObject) serializedObject = '{\n}'
202
- // const lines = serializedObject.split('\n')
203
- // lines.splice(1, 0, `...devices[${quote(deviceName!)}],`)
204
- // return lines.join('\n')
205
- }
206
- class JavaScriptFormatter {
207
- _baseIndent;
208
- _baseOffset;
209
- _lines = [];
210
- constructor(offset = 0) {
211
- this._baseIndent = ' '.repeat(2);
212
- this._baseOffset = ' '.repeat(offset);
213
- }
214
- prepend(text) {
215
- this._lines = text
216
- .trim()
217
- .split('\n')
218
- .map((line) => line.trim())
219
- .concat(this._lines);
220
- }
221
- add(text) {
222
- this._lines.push(...text
223
- .trim()
224
- .split('\n')
225
- .map((line) => line.trim()));
226
- }
227
- newLine() {
228
- this._lines.push('');
229
- }
230
- format() {
231
- let spaces = '';
232
- let previousLine = '';
233
- return this._lines
234
- .map((line) => {
235
- if (line === '')
236
- return line;
237
- if (line.startsWith('}') || line.startsWith(']'))
238
- spaces = spaces.substring(this._baseIndent.length);
239
- const extraSpaces = /^(for|while|if|try).*\(.*\)$/.test(previousLine)
240
- ? this._baseIndent
241
- : '';
242
- previousLine = line;
243
- const callCarryOver = line.startsWith('.set');
244
- line =
245
- spaces + extraSpaces + (callCarryOver ? this._baseIndent : '') + line;
246
- if (line.endsWith('{') || line.endsWith('['))
247
- spaces += this._baseIndent;
248
- return this._baseOffset + line;
249
- })
250
- .join('\n');
251
- }
252
- }
253
- exports.JavaScriptFormatter = JavaScriptFormatter;
254
- function quote(text) {
255
- return (0, string_utils_1.escapeWithQuotes)(text, "'");
256
- }
@@ -1,127 +0,0 @@
1
- import type { BrowserContextOptions, LaunchOptions } from 'playwright-core';
2
- import type { ActionInContext } from './generator';
3
- import type { Point } from './recorder-types';
4
- export type Signal = NavigationSignal | PopupSignal | DownloadSignal | DialogSignal;
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 Language = 'javascript' | 'python' | 'java' | 'csharp' | 'jsonl';
98
- export type LanguageGeneratorOptions = {
99
- browserName: string;
100
- launchOptions: LaunchOptions;
101
- contextOptions: BrowserContextOptions;
102
- deviceName?: string;
103
- saveStorage?: string;
104
- };
105
- export interface LanguageGenerator {
106
- id: string;
107
- groupName: string;
108
- name: string;
109
- highlighter: Language;
110
- generateHeader(options: LanguageGeneratorOptions): string;
111
- generateAction(actionInContext: ActionInContext): string;
112
- generateFooter(saveStorage: string | undefined): string;
113
- }
114
- export declare function toSignalMap(action: Action): {
115
- popup: {
116
- name: "popup";
117
- popupAlias: string;
118
- };
119
- download: {
120
- name: "download";
121
- downloadAlias: string;
122
- };
123
- dialog: {
124
- name: "dialog";
125
- dialogAlias: string;
126
- };
127
- };
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toSignalMap = toSignalMap;
4
- function toSignalMap(action) {
5
- let popup;
6
- let download;
7
- let dialog;
8
- for (const signal of action.signals) {
9
- if (signal.name === 'popup')
10
- popup = signal;
11
- else if (signal.name === 'download')
12
- download = signal;
13
- else if (signal.name === 'dialog')
14
- dialog = signal;
15
- }
16
- return {
17
- popup,
18
- download,
19
- dialog,
20
- };
21
- }