@dev-blinq/cucumber_client 1.0.1375-dev โ 1.0.1375-stage
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/bin/assets/bundled_scripts/recorder.js +107 -107
- package/bin/assets/preload/css_gen.js +10 -10
- package/bin/assets/preload/toolbar.js +27 -29
- package/bin/assets/preload/unique_locators.js +1 -1
- package/bin/assets/preload/yaml.js +288 -275
- package/bin/assets/scripts/aria_snapshot.js +223 -220
- package/bin/assets/scripts/dom_attr.js +329 -329
- package/bin/assets/scripts/dom_parent.js +169 -174
- package/bin/assets/scripts/event_utils.js +94 -94
- package/bin/assets/scripts/pw.js +2050 -1949
- package/bin/assets/scripts/recorder.js +13 -23
- package/bin/assets/scripts/snapshot_capturer.js +147 -147
- package/bin/assets/scripts/unique_locators.js +163 -44
- package/bin/assets/scripts/yaml.js +796 -783
- package/bin/assets/templates/_hooks_template.txt +41 -0
- package/bin/assets/templates/utils_template.txt +2 -45
- package/bin/client/apiTest/apiTest.js +6 -0
- package/bin/client/code_cleanup/utils.js +5 -1
- package/bin/client/code_gen/api_codegen.js +2 -2
- package/bin/client/code_gen/code_inversion.js +107 -2
- package/bin/client/code_gen/function_signature.js +4 -0
- package/bin/client/code_gen/page_reflection.js +846 -906
- package/bin/client/code_gen/playwright_codeget.js +32 -3
- package/bin/client/cucumber/feature.js +4 -0
- package/bin/client/cucumber/feature_data.js +2 -2
- package/bin/client/cucumber/project_to_document.js +8 -2
- package/bin/client/cucumber/steps_definitions.js +6 -3
- package/bin/client/cucumber_selector.js +17 -1
- package/bin/client/local_agent.js +3 -2
- package/bin/client/parse_feature_file.js +23 -26
- package/bin/client/playground/projects/env.json +2 -2
- package/bin/client/project.js +186 -196
- package/bin/client/recorderv3/bvt_init.js +325 -0
- package/bin/client/recorderv3/bvt_recorder.js +298 -99
- package/bin/client/recorderv3/implemented_steps.js +8 -0
- package/bin/client/recorderv3/index.js +4 -303
- package/bin/client/recorderv3/scriptTest.js +1 -1
- package/bin/client/recorderv3/services.js +430 -154
- package/bin/client/recorderv3/step_runner.js +315 -206
- package/bin/client/recorderv3/step_utils.js +479 -25
- package/bin/client/recorderv3/update_feature.js +9 -5
- package/bin/client/recorderv3/wbr_entry.js +61 -0
- package/bin/client/recording.js +1 -0
- package/bin/client/upload-service.js +3 -2
- package/bin/client/utils/socket_logger.js +132 -0
- package/bin/index.js +4 -1
- package/bin/logger.js +3 -2
- package/bin/min/consoleApi.min.cjs +2 -3
- package/bin/min/injectedScript.min.cjs +16 -16
- package/package.json +20 -10
|
@@ -2,162 +2,438 @@ import { readdirSync, readFileSync } from "fs";
|
|
|
2
2
|
import { getRunsServiceBaseURL } from "../utils/index.js";
|
|
3
3
|
import { axiosClient } from "../utils/axiosClient.js";
|
|
4
4
|
import path from "path";
|
|
5
|
-
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
|
+
import { v4 as uuidv4 } from "uuid"; // Import uuid
|
|
6
7
|
export class NamesService {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
8
|
+
screenshotMap;
|
|
9
|
+
TOKEN;
|
|
10
|
+
projectDir;
|
|
11
|
+
logger;
|
|
12
|
+
constructor({ screenshotMap, TOKEN, projectDir, logger }) {
|
|
13
|
+
this.screenshotMap = screenshotMap;
|
|
14
|
+
this.TOKEN = TOKEN;
|
|
15
|
+
this.projectDir = projectDir;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
}
|
|
18
|
+
//@ts-expect-error
|
|
19
|
+
async generateStepName({ commands, stepsNames, parameters, map }) {
|
|
20
|
+
let screenshot = this.screenshotMap.get(commands[commands.length - 1].inputID);
|
|
21
|
+
if (!screenshot && commands.length > 1) {
|
|
22
|
+
screenshot = this.screenshotMap.get(commands[commands.length - 2].inputID);
|
|
23
|
+
}
|
|
24
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate`;
|
|
25
|
+
const TIMEOUT = 120; // 2 minutes
|
|
26
|
+
const { data } = await axiosClient({
|
|
27
|
+
url,
|
|
28
|
+
method: "POST",
|
|
29
|
+
data: {
|
|
30
|
+
commands,
|
|
31
|
+
stepsNames,
|
|
32
|
+
parameters,
|
|
33
|
+
//screenshot,
|
|
34
|
+
map,
|
|
35
|
+
},
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
38
|
+
"X-Source": "recorder",
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const actionUrl = `${getRunsServiceBaseURL()}/action/get-action?id=${data.id}`;
|
|
42
|
+
let result = { status: 500 };
|
|
43
|
+
for (let i = 0; i < TIMEOUT; i++) {
|
|
44
|
+
try {
|
|
45
|
+
const action = await axiosClient({
|
|
46
|
+
url: actionUrl,
|
|
47
|
+
method: "GET",
|
|
48
|
+
headers: {
|
|
49
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
50
|
+
"X-Source": "recorder",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
if (action.data.status) {
|
|
54
|
+
result = action;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (i === TIMEOUT - 1) {
|
|
60
|
+
this.logger.error("Timeout while generating step details: ", error instanceof Error ? error : { message: JSON.stringify(error) });
|
|
61
|
+
console.error("Timeout while generating step details: ", error instanceof Error ? error.message : error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
65
|
+
}
|
|
66
|
+
if (result.status !== 200) {
|
|
67
|
+
return { success: false, message: "Error while generating step details" };
|
|
68
|
+
}
|
|
69
|
+
return result.data;
|
|
70
|
+
}
|
|
71
|
+
async generateScenarioAndFeatureNames(scenarioAsText) {
|
|
72
|
+
if (!this.projectDir) {
|
|
73
|
+
throw new Error("Project directory not found");
|
|
74
|
+
}
|
|
75
|
+
// read all files with .feature extension into an object {files:[{name: "", content: ""}]}
|
|
76
|
+
const featureFiles = readdirSync(path.join(this.projectDir, "features"));
|
|
77
|
+
const featureFilesContent = featureFiles
|
|
78
|
+
.filter((file) => file.endsWith(".feature"))
|
|
79
|
+
.map((file) => {
|
|
80
|
+
return {
|
|
81
|
+
name: file,
|
|
82
|
+
content: readFileSync(path.join(this.projectDir, "features", file), "utf8"),
|
|
83
|
+
};
|
|
60
84
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
} catch (error) {
|
|
67
|
-
if (i === TIMEOUT - 1) {
|
|
68
|
-
console.error("Timeout while generating step details: ", error);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (result.status !== 200) {
|
|
76
|
-
return { success: false, message: "Error while generating step details" };
|
|
77
|
-
}
|
|
78
|
-
return result.data;
|
|
79
|
-
}
|
|
80
|
-
async generateScenarioAndFeatureNames(scenarioAsText) {
|
|
81
|
-
if (!this.projectDir) {
|
|
82
|
-
throw new Error("Project directory not found");
|
|
83
|
-
}
|
|
84
|
-
// read all files with .feature extension into an object {files:[{name: "", content: ""}]}
|
|
85
|
-
const featureFiles = readdirSync(path.join(this.projectDir, "features"));
|
|
86
|
-
const featureFilesContent = featureFiles
|
|
87
|
-
.filter((file) => file.endsWith(".feature"))
|
|
88
|
-
.map((file) => {
|
|
89
|
-
return {
|
|
90
|
-
name: file,
|
|
91
|
-
content: readFileSync(path.join(this.projectDir, "features", file), "utf8"),
|
|
85
|
+
const genObject = {
|
|
86
|
+
files: featureFilesContent,
|
|
87
|
+
scenario: scenarioAsText,
|
|
92
88
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
data: {
|
|
107
|
-
...genObject,
|
|
108
|
-
},
|
|
109
|
-
headers: {
|
|
110
|
-
Authorization: `Bearer ${this.TOKEN}`,
|
|
111
|
-
"X-Source": "recorder",
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
const actionUrl = `${getRunsServiceBaseURL()}/action/get-action?id=${data.id}`;
|
|
115
|
-
let result = { status: 500 };
|
|
116
|
-
for (let i = 0; i < TIMEOUT; i++) {
|
|
117
|
-
try {
|
|
118
|
-
const action = await axiosClient({
|
|
119
|
-
url: actionUrl,
|
|
120
|
-
method: "GET",
|
|
121
|
-
headers: {
|
|
122
|
-
Authorization: `Bearer ${this.TOKEN}`,
|
|
123
|
-
"X-Source": "recorder",
|
|
124
|
-
},
|
|
89
|
+
// get screenshot for the last command
|
|
90
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate_scenario_feature`;
|
|
91
|
+
const TIMEOUT = 120; // 2 minutes
|
|
92
|
+
const { data } = await axiosClient({
|
|
93
|
+
url,
|
|
94
|
+
method: "POST",
|
|
95
|
+
data: {
|
|
96
|
+
...genObject,
|
|
97
|
+
},
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
100
|
+
"X-Source": "recorder",
|
|
101
|
+
},
|
|
125
102
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
103
|
+
const actionUrl = `${getRunsServiceBaseURL()}/action/get-action?id=${data.id}`;
|
|
104
|
+
let result = { status: 500 };
|
|
105
|
+
for (let i = 0; i < TIMEOUT; i++) {
|
|
106
|
+
try {
|
|
107
|
+
const action = await axiosClient({
|
|
108
|
+
url: actionUrl,
|
|
109
|
+
method: "GET",
|
|
110
|
+
headers: {
|
|
111
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
112
|
+
"X-Source": "recorder",
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
if (action.data.status) {
|
|
116
|
+
result = action;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
if (i === TIMEOUT - 1) {
|
|
122
|
+
console.error("Timeout while generating step details: ", error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
126
|
+
}
|
|
127
|
+
if (result.status !== 200) {
|
|
128
|
+
return { success: false, message: "Error while generating step details" };
|
|
129
|
+
}
|
|
130
|
+
return result.data;
|
|
131
|
+
}
|
|
132
|
+
//@ts-expect-error
|
|
133
|
+
async generateCommandName({ command }) {
|
|
134
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate-element-name`;
|
|
135
|
+
const screenshot = this.screenshotMap.get(command.inputID);
|
|
136
|
+
const result = await axiosClient({
|
|
137
|
+
url,
|
|
138
|
+
method: "POST",
|
|
139
|
+
data: { ...command, screenshot },
|
|
140
|
+
headers: {
|
|
141
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
142
|
+
"X-Source": "recorder",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
if (result.status !== 200) {
|
|
146
|
+
return { success: false, message: "Error while generating command details" };
|
|
147
|
+
}
|
|
148
|
+
return result.data;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export class PublishService {
|
|
152
|
+
TOKEN;
|
|
153
|
+
constructor(TOKEN) {
|
|
154
|
+
this.TOKEN = TOKEN;
|
|
155
|
+
}
|
|
156
|
+
async saveScenario({ scenario, featureName, override, branch, isEditing, projectId }) {
|
|
157
|
+
const runsURL = getRunsServiceBaseURL();
|
|
158
|
+
const workspaceURL = runsURL.replace("/runs", "/workspace");
|
|
159
|
+
const url = `${workspaceURL}/publish-recording`;
|
|
160
|
+
const result = await axiosClient({
|
|
161
|
+
url,
|
|
162
|
+
method: "POST",
|
|
163
|
+
data: {
|
|
164
|
+
scenario,
|
|
165
|
+
featureName,
|
|
166
|
+
override,
|
|
167
|
+
},
|
|
168
|
+
params: {
|
|
169
|
+
branch,
|
|
170
|
+
},
|
|
171
|
+
headers: {
|
|
172
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
173
|
+
"X-Source": "recorder",
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
if (result.status !== 200) {
|
|
177
|
+
return { success: false, message: "Error while saving scenario" };
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
this.updateProjectMetadata({
|
|
181
|
+
featureName,
|
|
182
|
+
scenarioName: scenario.name,
|
|
183
|
+
projectId,
|
|
184
|
+
branch,
|
|
185
|
+
isEditing: override,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
catch (error) { }
|
|
189
|
+
return { success: true, data: result.data };
|
|
190
|
+
}
|
|
191
|
+
async updateProjectMetadata({ featureName, scenarioName, projectId, branch, isEditing }) {
|
|
192
|
+
try {
|
|
193
|
+
await axiosClient({
|
|
194
|
+
method: "POST",
|
|
195
|
+
url: `${getRunsServiceBaseURL()}/project/updateProjectMetadata`,
|
|
196
|
+
data: {
|
|
197
|
+
featureName,
|
|
198
|
+
scenarioName,
|
|
199
|
+
eventType: isEditing ? "editScenario" : "createScenario",
|
|
200
|
+
projectId,
|
|
201
|
+
branch: branch,
|
|
202
|
+
},
|
|
203
|
+
headers: {
|
|
204
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
205
|
+
"X-Source": "recorder",
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
// logger.error("Failed to update project metadata: " + error.message);
|
|
211
|
+
// @ts-ignore
|
|
212
|
+
console.error("run_recorder", `Failed to update project metadata: ${error.message ?? error}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export class RemoteBrowserService extends EventEmitter {
|
|
217
|
+
CDP_CONNECT_URL;
|
|
218
|
+
context;
|
|
219
|
+
pages = new Map();
|
|
220
|
+
_selectedPageId = null;
|
|
221
|
+
wsUrlBase; // Store the base URL
|
|
222
|
+
constructor({ CDP_CONNECT_URL, context }) {
|
|
223
|
+
super();
|
|
224
|
+
this.CDP_CONNECT_URL = CDP_CONNECT_URL;
|
|
225
|
+
this.context = context;
|
|
226
|
+
this.wsUrlBase = this.CDP_CONNECT_URL.replace(/^http/, "ws") + "/devtools/page/";
|
|
227
|
+
this.log("๐ RemoteBrowserService initialized", { CDP_CONNECT_URL });
|
|
228
|
+
this.initializeListeners();
|
|
229
|
+
}
|
|
230
|
+
log(message, data) {
|
|
231
|
+
const timestamp = new Date().toISOString();
|
|
232
|
+
console.log(`[${timestamp}] [RemoteBrowserService] ${message}`, data ? JSON.stringify(data, null, 2) : "");
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Gets the CDP Target ID for a page *directly* from the page.
|
|
236
|
+
* This is the only reliable method during restarts.
|
|
237
|
+
*/
|
|
238
|
+
async getCdpTargetId(page) {
|
|
239
|
+
try {
|
|
240
|
+
if (page.isClosed()) {
|
|
241
|
+
this.log("โ ๏ธ Attempted to get CDP ID from a closed page");
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const cdpSession = await page.context().newCDPSession(page);
|
|
245
|
+
const { targetInfo } = await cdpSession.send("Target.getTargetInfo");
|
|
246
|
+
this.log("๐ Retrieved TargetInfo via CDP session", targetInfo);
|
|
247
|
+
await cdpSession.detach();
|
|
248
|
+
if (targetInfo && targetInfo.targetId) {
|
|
249
|
+
this.log("โ
Found CDP ID by session", { id: targetInfo.targetId, url: page.url() });
|
|
250
|
+
return targetInfo.targetId;
|
|
251
|
+
}
|
|
252
|
+
throw new Error("Target.getTargetInfo did not return a targetId");
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
this.log("โ Error getting CDP ID by session", { url: page.url(), error });
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async initializeListeners() {
|
|
260
|
+
this.log("๐ก Initializing listeners");
|
|
261
|
+
this.context.on("page", async (page) => {
|
|
262
|
+
const stableTabId = uuidv4();
|
|
263
|
+
this.log("๐ New page event triggered", { stableTabId, url: page.url() });
|
|
264
|
+
// We get the ID immediately, but it might be null if the page is too new
|
|
265
|
+
const cdpTargetId = await this.getCdpTargetId(page);
|
|
266
|
+
this.pages.set(stableTabId, { page, cdpTargetId });
|
|
267
|
+
if (cdpTargetId) {
|
|
268
|
+
this.log("โ
Page mapped to CDP ID", { stableTabId, cdpTargetId });
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
this.log("โ ๏ธ Could not find CDP ID for new page yet", { stableTabId });
|
|
272
|
+
}
|
|
273
|
+
if (!this._selectedPageId) {
|
|
274
|
+
// Select the first page that opens
|
|
275
|
+
this._selectedPageId = stableTabId;
|
|
276
|
+
this.log("๐ฏ Initial selected page set", { selectedPageId: this._selectedPageId });
|
|
277
|
+
}
|
|
278
|
+
await this.syncState();
|
|
279
|
+
// Add listeners
|
|
280
|
+
page.on("load", () => this.syncState());
|
|
281
|
+
page.on("framenavigated", () => this.syncState());
|
|
282
|
+
page.on("close", async () => {
|
|
283
|
+
this.log("๐๏ธ Page close event", { stableTabId });
|
|
284
|
+
this.pages.delete(stableTabId);
|
|
285
|
+
if (this._selectedPageId === stableTabId) {
|
|
286
|
+
this._selectedPageId = this.pages.size > 0 ? Array.from(this.pages.keys())[0] : null;
|
|
287
|
+
this.log("๐ Selected page changed after close", { newSelectedId: this._selectedPageId });
|
|
288
|
+
}
|
|
289
|
+
await this.syncState();
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
// Initialize with existing pages
|
|
293
|
+
const existingPages = this.context.pages();
|
|
294
|
+
this.log("๐ Found existing pages", { count: existingPages.length });
|
|
295
|
+
for (const page of existingPages) {
|
|
296
|
+
const stableTabId = uuidv4();
|
|
297
|
+
const cdpTargetId = await this.getCdpTargetId(page);
|
|
298
|
+
this.pages.set(stableTabId, { page, cdpTargetId });
|
|
299
|
+
this.log("โ
Existing page added to map", { stableTabId, cdpTargetId, url: page.url() });
|
|
300
|
+
}
|
|
301
|
+
if (this.pages.size > 0 && !this._selectedPageId) {
|
|
302
|
+
this._selectedPageId = Array.from(this.pages.keys())[0];
|
|
303
|
+
}
|
|
304
|
+
await this.syncState();
|
|
305
|
+
}
|
|
306
|
+
async syncState() {
|
|
307
|
+
try {
|
|
308
|
+
this.log("๐ Starting state sync");
|
|
309
|
+
const state = await this.getState();
|
|
310
|
+
this.log("โ
State sync complete", { pagesCount: state.pages.length, selectedPageId: state.selectedPageId });
|
|
311
|
+
this.emit("BrowserService.stateSync", state);
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
this.log("โ Error syncing state", { error });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async getState() {
|
|
318
|
+
this.log("๐ Getting current state");
|
|
319
|
+
const pagesData = [];
|
|
320
|
+
const pagesToDelete = []; // To clean up closed pages
|
|
321
|
+
for (const [stableTabId, pageInfo] of this.pages.entries()) {
|
|
322
|
+
try {
|
|
323
|
+
if (pageInfo.page.isClosed()) {
|
|
324
|
+
this.log("๐งน Found closed page during getState, marking for deletion", { stableTabId });
|
|
325
|
+
pagesToDelete.push(stableTabId);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
// Get the one, true, live CDP ID
|
|
329
|
+
const currentCdpId = await this.getCdpTargetId(pageInfo.page);
|
|
330
|
+
if (currentCdpId && pageInfo.cdpTargetId !== currentCdpId) {
|
|
331
|
+
this.log("๐ CDP ID changed", { stableTabId, old: pageInfo.cdpTargetId, new: currentCdpId });
|
|
332
|
+
pageInfo.cdpTargetId = currentCdpId; // Update our internal reference
|
|
333
|
+
}
|
|
334
|
+
// Manually construct the WebSocket URL
|
|
335
|
+
const wsDebuggerUrl = currentCdpId ? `${this.wsUrlBase}${currentCdpId}` : "";
|
|
336
|
+
if (!wsDebuggerUrl) {
|
|
337
|
+
this.log("โ ๏ธ Could not get CDP ID, wsDebuggerUrl will be empty", { stableTabId });
|
|
338
|
+
}
|
|
339
|
+
pagesData.push({
|
|
340
|
+
id: stableTabId,
|
|
341
|
+
title: await pageInfo.page.title(),
|
|
342
|
+
url: pageInfo.page.url(),
|
|
343
|
+
wsDebuggerUrl: wsDebuggerUrl, // Use the constructed URL
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
this.log("โ Error getting page data", { stableTabId, error });
|
|
348
|
+
pagesToDelete.push(stableTabId); // Mark for deletion
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
pagesToDelete.forEach((id) => this.pages.delete(id));
|
|
352
|
+
if (this._selectedPageId && !this.pages.has(this._selectedPageId)) {
|
|
353
|
+
this._selectedPageId = pagesData.length > 0 ? pagesData[0].id : null;
|
|
354
|
+
this.log("๐ Corrected selectedPageId", { new: this._selectedPageId });
|
|
355
|
+
}
|
|
356
|
+
if (!this._selectedPageId && pagesData.length > 0) {
|
|
357
|
+
this._selectedPageId = pagesData[0].id;
|
|
358
|
+
this.log("๐ฏ Set default selectedPageId", { new: this._selectedPageId });
|
|
359
|
+
}
|
|
360
|
+
const state = {
|
|
361
|
+
pages: pagesData,
|
|
362
|
+
selectedPageId: this._selectedPageId,
|
|
363
|
+
};
|
|
364
|
+
this.log("๐ฆ Final state", state);
|
|
365
|
+
return state;
|
|
366
|
+
}
|
|
367
|
+
async createTab(url = "about:blank") {
|
|
368
|
+
try {
|
|
369
|
+
this.log("๐ Creating new tab", { url });
|
|
370
|
+
const page = await this.context.newPage(); // This will trigger the 'page' event
|
|
371
|
+
if (url !== "about:blank") {
|
|
372
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
373
|
+
}
|
|
374
|
+
for (const [stableTabId, pageInfo] of this.pages.entries()) {
|
|
375
|
+
if (pageInfo.page === page) {
|
|
376
|
+
this._selectedPageId = stableTabId;
|
|
377
|
+
this.log("โ
New tab created and selected", { stableTabId, url });
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
await this.syncState();
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
this.log("โ Error creating tab", { error });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async closeTab(stableTabId) {
|
|
388
|
+
try {
|
|
389
|
+
this.log("๐๏ธ Closing tab", { stableTabId });
|
|
390
|
+
const pageInfo = this.pages.get(stableTabId);
|
|
391
|
+
if (pageInfo) {
|
|
392
|
+
await pageInfo.page.close(); // This will trigger the 'close' event
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
this.log("โ ๏ธ Page not found for closing", { stableTabId });
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
this.log("โ Error closing tab", { error });
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async selectTab(stableTabId) {
|
|
403
|
+
try {
|
|
404
|
+
this.log("๐ฏ Selecting tab", { stableTabId });
|
|
405
|
+
const pageInfo = this.pages.get(stableTabId);
|
|
406
|
+
if (pageInfo) {
|
|
407
|
+
this._selectedPageId = stableTabId;
|
|
408
|
+
await pageInfo.page.bringToFront();
|
|
409
|
+
this.log("โ
Tab selected successfully", { stableTabId });
|
|
410
|
+
await this.syncState();
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
this.log("โ ๏ธ Page not found for selection", { stableTabId });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
this.log("โ Error selecting tab", { error });
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
getSelectedPage() {
|
|
421
|
+
const pageInfo = this._selectedPageId ? this.pages.get(this._selectedPageId) : null;
|
|
422
|
+
this.log("๐ Getting selected page", {
|
|
423
|
+
selectedPageId: this._selectedPageId,
|
|
424
|
+
found: !!pageInfo,
|
|
425
|
+
url: pageInfo?.page.url(),
|
|
426
|
+
});
|
|
427
|
+
return pageInfo?.page || null;
|
|
428
|
+
}
|
|
429
|
+
destroy() {
|
|
430
|
+
this.log("๐ฅ Destroying RemoteBrowserService");
|
|
431
|
+
this.context.removeAllListeners("page");
|
|
432
|
+
for (const [, pageInfo] of this.pages.entries()) {
|
|
433
|
+
pageInfo.page.removeAllListeners(); // Remove all listeners from each page
|
|
434
|
+
}
|
|
435
|
+
this.pages.clear();
|
|
436
|
+
this._selectedPageId = null;
|
|
437
|
+
this.removeAllListeners();
|
|
438
|
+
}
|
|
163
439
|
}
|