@browserbasehq/sdk 1.4.2 → 1.4.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/dist/index.d.ts +31 -6
- package/dist/index.js +3 -1
- package/dist/recorder/call-metadata.d.ts +66 -0
- package/dist/recorder/call-metadata.js +2 -0
- package/dist/recorder/crypto.d.ts +1 -0
- package/dist/recorder/crypto.js +10 -0
- package/dist/recorder/css-utils.d.ts +2 -0
- package/dist/recorder/css-utils.js +34 -0
- package/dist/recorder/generator.d.ts +31 -0
- package/dist/recorder/generator.js +154 -0
- package/dist/recorder/javascript-generator.d.ts +29 -0
- package/dist/recorder/javascript-generator.js +256 -0
- package/dist/recorder/language.d.ts +127 -0
- package/dist/recorder/language.js +21 -0
- package/dist/recorder/recorder-types.d.ts +108 -0
- package/dist/recorder/recorder-types.js +2 -0
- package/dist/recorder/recorder.d.ts +35 -0
- package/dist/recorder/recorder.js +333 -0
- package/dist/recorder/time.d.ts +1 -0
- package/dist/recorder/time.js +42 -0
- package/dist/recorder/timeout-runner.d.ts +6 -0
- package/dist/recorder/timeout-runner.js +19 -0
- package/dist/recorder/utils/isomorphic/css-parser.d.ts +41 -0
- package/dist/recorder/utils/isomorphic/css-parser.js +291 -0
- package/dist/recorder/utils/isomorphic/css-tokenizer.d.ts +171 -0
- package/dist/recorder/utils/isomorphic/css-tokenizer.js +986 -0
- package/dist/recorder/utils/isomorphic/locator-generators.d.ts +79 -0
- package/dist/recorder/utils/isomorphic/locator-generators.js +666 -0
- package/dist/recorder/utils/isomorphic/locator-parser.d.ts +17 -0
- package/dist/recorder/utils/isomorphic/locator-parser.js +244 -0
- package/dist/recorder/utils/isomorphic/locator-utils.d.ts +43 -0
- package/dist/recorder/utils/isomorphic/locator-utils.js +69 -0
- package/dist/recorder/utils/isomorphic/selector-parser.d.ts +48 -0
- package/dist/recorder/utils/isomorphic/selector-parser.js +483 -0
- package/dist/recorder/utils/isomorphic/string-utils.d.ts +28 -0
- package/dist/recorder/utils/isomorphic/string-utils.js +123 -0
- package/dist/recorder/utils.d.ts +20 -0
- package/dist/recorder/utils.js +32 -0
- package/package.json +6 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import BrowserbaseAISDK from './integrations/ai-sdk.js';
|
|
2
|
+
import { BrowserbaseCodeGenerator, type GeneratorOptions, type ActionsEvent } from './recorder/recorder';
|
|
2
3
|
export type ClientOptions = {
|
|
3
4
|
apiKey?: string;
|
|
4
5
|
projectId?: string;
|
|
@@ -41,9 +42,28 @@ export type CreateSessionOptions = {
|
|
|
41
42
|
id: string;
|
|
42
43
|
persist: boolean;
|
|
43
44
|
};
|
|
45
|
+
blockAds?: boolean;
|
|
46
|
+
solveCaptchas?: boolean;
|
|
47
|
+
recordSession?: boolean;
|
|
48
|
+
logSession?: boolean;
|
|
44
49
|
};
|
|
45
50
|
keepAlive?: boolean;
|
|
46
51
|
timeout?: number;
|
|
52
|
+
proxies?: boolean | {
|
|
53
|
+
type: 'browserbase';
|
|
54
|
+
geolocation?: {
|
|
55
|
+
country: string;
|
|
56
|
+
state?: string;
|
|
57
|
+
city?: string;
|
|
58
|
+
};
|
|
59
|
+
domainPattern?: string;
|
|
60
|
+
} | {
|
|
61
|
+
type: 'external';
|
|
62
|
+
server: string;
|
|
63
|
+
domainPattern?: string;
|
|
64
|
+
username?: string;
|
|
65
|
+
password?: string;
|
|
66
|
+
};
|
|
47
67
|
};
|
|
48
68
|
export type Session = {
|
|
49
69
|
id: string;
|
|
@@ -53,12 +73,9 @@ export type Session = {
|
|
|
53
73
|
expiresAt: string;
|
|
54
74
|
projectId: string;
|
|
55
75
|
status: 'RUNNING' | 'COMPLETED' | 'ERROR' | 'TIMED_OUT';
|
|
56
|
-
taskId?: string;
|
|
57
76
|
proxyBytes?: number;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
details?: string;
|
|
61
|
-
logs?: string;
|
|
77
|
+
keepAlive: boolean;
|
|
78
|
+
contextId?: string;
|
|
62
79
|
};
|
|
63
80
|
export type SessionRecording = {
|
|
64
81
|
type?: number;
|
|
@@ -69,6 +86,14 @@ export type DebugConnectionURLs = {
|
|
|
69
86
|
debuggerFullscreenUrl?: string;
|
|
70
87
|
debuggerUrl?: string;
|
|
71
88
|
wsUrl?: string;
|
|
89
|
+
pages?: DebugConnectionPage;
|
|
90
|
+
};
|
|
91
|
+
export type DebugConnectionPage = {
|
|
92
|
+
id: string;
|
|
93
|
+
url: string;
|
|
94
|
+
title: string;
|
|
95
|
+
debuggerUrl: string;
|
|
96
|
+
debuggerFullscreenUrl: string;
|
|
72
97
|
};
|
|
73
98
|
export type SessionLog = {
|
|
74
99
|
sessionId?: string;
|
|
@@ -110,4 +135,4 @@ export default class Browserbase {
|
|
|
110
135
|
id: string;
|
|
111
136
|
}>;
|
|
112
137
|
}
|
|
113
|
-
export { Browserbase, BrowserbaseAISDK };
|
|
138
|
+
export { Browserbase, BrowserbaseAISDK, BrowserbaseCodeGenerator, type GeneratorOptions, type ActionsEvent, };
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.BrowserbaseAISDK = exports.Browserbase = void 0;
|
|
6
|
+
exports.BrowserbaseCodeGenerator = exports.BrowserbaseAISDK = exports.Browserbase = void 0;
|
|
7
7
|
const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
|
|
8
8
|
const ai_sdk_js_1 = __importDefault(require("./integrations/ai-sdk.js"));
|
|
9
9
|
exports.BrowserbaseAISDK = ai_sdk_js_1.default;
|
|
10
|
+
const recorder_1 = require("./recorder/recorder");
|
|
11
|
+
Object.defineProperty(exports, "BrowserbaseCodeGenerator", { enumerable: true, get: function () { return recorder_1.BrowserbaseCodeGenerator; } });
|
|
10
12
|
class Browserbase {
|
|
11
13
|
apiKey;
|
|
12
14
|
projectId;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Point } from './recorder-types';
|
|
2
|
+
export type SerializedValue = {
|
|
3
|
+
n?: number;
|
|
4
|
+
b?: boolean;
|
|
5
|
+
s?: string;
|
|
6
|
+
v?: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0';
|
|
7
|
+
d?: string;
|
|
8
|
+
u?: string;
|
|
9
|
+
bi?: string;
|
|
10
|
+
r?: {
|
|
11
|
+
p: string;
|
|
12
|
+
f: string;
|
|
13
|
+
};
|
|
14
|
+
a?: SerializedValue[];
|
|
15
|
+
o?: {
|
|
16
|
+
k: string;
|
|
17
|
+
v: SerializedValue;
|
|
18
|
+
}[];
|
|
19
|
+
h?: number;
|
|
20
|
+
id?: number;
|
|
21
|
+
ref?: number;
|
|
22
|
+
};
|
|
23
|
+
export type SerializedError = {
|
|
24
|
+
error?: {
|
|
25
|
+
message: string;
|
|
26
|
+
name: string;
|
|
27
|
+
stack?: string;
|
|
28
|
+
};
|
|
29
|
+
value?: SerializedValue;
|
|
30
|
+
};
|
|
31
|
+
export type CallMetadata = {
|
|
32
|
+
id: string;
|
|
33
|
+
startTime: number;
|
|
34
|
+
endTime: number;
|
|
35
|
+
pauseStartTime?: number;
|
|
36
|
+
pauseEndTime?: number;
|
|
37
|
+
type: string;
|
|
38
|
+
method: string;
|
|
39
|
+
params: any;
|
|
40
|
+
apiName?: string;
|
|
41
|
+
internal?: boolean;
|
|
42
|
+
isServerSide?: boolean;
|
|
43
|
+
stepId?: string;
|
|
44
|
+
location?: {
|
|
45
|
+
file: string;
|
|
46
|
+
line?: number;
|
|
47
|
+
column?: number;
|
|
48
|
+
};
|
|
49
|
+
log: string[];
|
|
50
|
+
error?: SerializedError;
|
|
51
|
+
result?: any;
|
|
52
|
+
point?: Point;
|
|
53
|
+
objectId?: string;
|
|
54
|
+
pageId?: string;
|
|
55
|
+
frameId?: string;
|
|
56
|
+
potentiallyClosesScope?: boolean;
|
|
57
|
+
};
|
|
58
|
+
export type ActionMetadata = {
|
|
59
|
+
apiName?: string;
|
|
60
|
+
objectId?: string;
|
|
61
|
+
pageId?: string;
|
|
62
|
+
frameId?: string;
|
|
63
|
+
params: any;
|
|
64
|
+
method: string;
|
|
65
|
+
type: string;
|
|
66
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createGuid(): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
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;
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
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
|
+
}
|