testdriverai 5.7.30 → 5.7.32
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/lib/commands.js +25 -31
- package/lib/sdk.js +41 -29
- package/package.json +1 -1
package/lib/commands.js
CHANGED
|
@@ -33,10 +33,7 @@ if (config.TD_VM) {
|
|
|
33
33
|
keymap = require("./keymaps/robot.js");
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const {
|
|
37
|
-
logger,
|
|
38
|
-
prettyMarkdown
|
|
39
|
-
} = require("./logger");
|
|
36
|
+
const { logger, prettyMarkdown } = require("./logger");
|
|
40
37
|
const { emitter, events } = require("./events.js");
|
|
41
38
|
|
|
42
39
|
const niceSeconds = (ms) => {
|
|
@@ -232,7 +229,6 @@ const scroll = async (direction = "down", amount = 300, method = "mouse") => {
|
|
|
232
229
|
// perform a mouse click
|
|
233
230
|
// click, right-click, double-click, hover
|
|
234
231
|
const click = async (x, y, action = "click") => {
|
|
235
|
-
|
|
236
232
|
emitter.emit(events.interactive, true);
|
|
237
233
|
|
|
238
234
|
await redraw.start();
|
|
@@ -289,10 +285,8 @@ const click = async (x, y, action = "click") => {
|
|
|
289
285
|
} else if (action === "drag-end") {
|
|
290
286
|
robot.mouseToggle("up", button);
|
|
291
287
|
} else {
|
|
292
|
-
|
|
293
288
|
robot.mouseClick(button, double);
|
|
294
289
|
}
|
|
295
|
-
|
|
296
290
|
}
|
|
297
291
|
|
|
298
292
|
emitter.emit(events.mouseClick, { x, y, button, click });
|
|
@@ -357,9 +351,7 @@ let commands = {
|
|
|
357
351
|
},
|
|
358
352
|
(chunk) => {
|
|
359
353
|
if (chunk.type === "data" && chunk.data) {
|
|
360
|
-
|
|
361
354
|
// mdStream.log(chunk.data);
|
|
362
|
-
|
|
363
355
|
} else if (chunk.type === "closeMatches") {
|
|
364
356
|
emitter.emit(events.matches.show, chunk.data);
|
|
365
357
|
}
|
|
@@ -421,7 +413,7 @@ let commands = {
|
|
|
421
413
|
}
|
|
422
414
|
},
|
|
423
415
|
// type a string
|
|
424
|
-
type: async (string, delay =
|
|
416
|
+
type: async (string, delay = 250) => {
|
|
425
417
|
await redraw.start();
|
|
426
418
|
|
|
427
419
|
string = string.toString();
|
|
@@ -442,7 +434,6 @@ let commands = {
|
|
|
442
434
|
// press keys
|
|
443
435
|
// different than `type`, becasue it can press multiple keys at once
|
|
444
436
|
"press-keys": async (inputKeys) => {
|
|
445
|
-
|
|
446
437
|
await redraw.start();
|
|
447
438
|
|
|
448
439
|
// robotjs is finiky with key-up on modifier keys
|
|
@@ -495,10 +486,9 @@ let commands = {
|
|
|
495
486
|
|
|
496
487
|
modsToPress.forEach((key) => {
|
|
497
488
|
robot.keyToggle(key, "down");
|
|
498
|
-
})
|
|
489
|
+
});
|
|
499
490
|
|
|
500
491
|
robot.keyTap(keysPressed[0], modsToPress);
|
|
501
|
-
|
|
502
492
|
} else {
|
|
503
493
|
await sandbox.send({ type: "press", keys: keysPressed });
|
|
504
494
|
}
|
|
@@ -742,9 +732,9 @@ let commands = {
|
|
|
742
732
|
return "The application was focused.";
|
|
743
733
|
},
|
|
744
734
|
remember: async (description) => {
|
|
745
|
-
let result = await sdk.req("remember", {
|
|
735
|
+
let result = await sdk.req("remember", {
|
|
746
736
|
image: await captureScreenBase64(),
|
|
747
|
-
description
|
|
737
|
+
description,
|
|
748
738
|
});
|
|
749
739
|
return result.data;
|
|
750
740
|
},
|
|
@@ -801,7 +791,10 @@ let commands = {
|
|
|
801
791
|
chalk.dim(`running value of \`${plat}\` on this machine...`),
|
|
802
792
|
true,
|
|
803
793
|
);
|
|
804
|
-
result = await exec(windows_code, {
|
|
794
|
+
result = await exec(windows_code, {
|
|
795
|
+
cwd: cwd(),
|
|
796
|
+
shell: "powershell.exe",
|
|
797
|
+
});
|
|
805
798
|
} else if (plat == "mac") {
|
|
806
799
|
logger.info(
|
|
807
800
|
chalk.dim(`running value of \`${plat}\` on this machine...`),
|
|
@@ -816,15 +809,13 @@ let commands = {
|
|
|
816
809
|
true,
|
|
817
810
|
);
|
|
818
811
|
} else {
|
|
819
|
-
|
|
820
812
|
if (!silent) {
|
|
821
813
|
logger.info(chalk.dim(`Command output:`), true);
|
|
822
|
-
logger.info(`${result.stdout}`, true)
|
|
814
|
+
logger.info(`${result.stdout}`, true);
|
|
823
815
|
}
|
|
824
816
|
|
|
825
817
|
return result.stdout?.trim();
|
|
826
818
|
}
|
|
827
|
-
|
|
828
819
|
} else if (language == "js") {
|
|
829
820
|
logger.info(chalk.dim(`running js...`), true);
|
|
830
821
|
|
|
@@ -833,23 +824,26 @@ let commands = {
|
|
|
833
824
|
true,
|
|
834
825
|
);
|
|
835
826
|
|
|
836
|
-
console.log(
|
|
837
|
-
console.log(
|
|
838
|
-
|
|
839
|
-
const context = vm.createContext({
|
|
827
|
+
console.log("");
|
|
828
|
+
console.log("------");
|
|
829
|
+
|
|
830
|
+
const context = vm.createContext({
|
|
831
|
+
require,
|
|
832
|
+
console,
|
|
833
|
+
fs,
|
|
834
|
+
process,
|
|
835
|
+
fetch,
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
scriptCode = "(async function() {\n" + scriptCode + "\n})();";
|
|
840
839
|
|
|
841
|
-
scriptCode = '(async function() {\n' + scriptCode + '\n})();';
|
|
842
|
-
|
|
843
840
|
const script = new vm.Script(scriptCode);
|
|
844
841
|
|
|
845
842
|
try {
|
|
846
843
|
await script.runInNewContext(context);
|
|
847
844
|
} catch (e) {
|
|
848
845
|
console.error(e);
|
|
849
|
-
throw new AiError(
|
|
850
|
-
`Error running script: ${e.message}`,
|
|
851
|
-
true,
|
|
852
|
-
);
|
|
846
|
+
throw new AiError(`Error running script: ${e.message}`, true);
|
|
853
847
|
}
|
|
854
848
|
|
|
855
849
|
// wait for context.result to resolve
|
|
@@ -862,8 +856,8 @@ let commands = {
|
|
|
862
856
|
stepResult = stepResult.toString();
|
|
863
857
|
}
|
|
864
858
|
|
|
865
|
-
console.log(
|
|
866
|
-
console.log(
|
|
859
|
+
console.log("------");
|
|
860
|
+
console.log("");
|
|
867
861
|
|
|
868
862
|
if (!stepResult) {
|
|
869
863
|
logger.info(`No result returned from script`, true);
|
package/lib/sdk.js
CHANGED
|
@@ -5,14 +5,32 @@ const session = require("./session");
|
|
|
5
5
|
// get the version from package.json
|
|
6
6
|
const { version } = require("../package.json");
|
|
7
7
|
const root = config["TD_API_ROOT"];
|
|
8
|
-
const axios = require(
|
|
8
|
+
const axios = require("axios");
|
|
9
9
|
|
|
10
|
-
const { logger } = require(
|
|
10
|
+
const { logger } = require("./logger");
|
|
11
11
|
|
|
12
12
|
let token = null;
|
|
13
13
|
|
|
14
|
-
const outputError =
|
|
15
|
-
logger.
|
|
14
|
+
const outputError = (error) => {
|
|
15
|
+
logger.error(
|
|
16
|
+
"API Error: %s (%s)",
|
|
17
|
+
chalk.red(
|
|
18
|
+
// HTTP status code from Axios
|
|
19
|
+
error.status ||
|
|
20
|
+
// ...or an explicit error `reason` set by Sails
|
|
21
|
+
error.reason ||
|
|
22
|
+
// ...or default to the error message
|
|
23
|
+
error.message,
|
|
24
|
+
),
|
|
25
|
+
chalk.red(
|
|
26
|
+
// e.g. "teamNotExist" from Sails' exits
|
|
27
|
+
error.response?.data?.raw ||
|
|
28
|
+
// ...or the status text
|
|
29
|
+
error.statusText ||
|
|
30
|
+
// ...or the HTTP status code
|
|
31
|
+
error.code,
|
|
32
|
+
),
|
|
33
|
+
);
|
|
16
34
|
};
|
|
17
35
|
|
|
18
36
|
const parseBody = async (response, body) => {
|
|
@@ -62,9 +80,7 @@ const parseBody = async (response, body) => {
|
|
|
62
80
|
};
|
|
63
81
|
|
|
64
82
|
let auth = async () => {
|
|
65
|
-
|
|
66
83
|
if (config["TD_API_KEY"]) {
|
|
67
|
-
|
|
68
84
|
const url = [root, "auth/exchange-api-key"].join("/");
|
|
69
85
|
const c = {
|
|
70
86
|
method: "post",
|
|
@@ -73,21 +89,20 @@ let auth = async () => {
|
|
|
73
89
|
},
|
|
74
90
|
data: {
|
|
75
91
|
apiKey: config["TD_API_KEY"],
|
|
76
|
-
version
|
|
77
|
-
}
|
|
92
|
+
version,
|
|
93
|
+
},
|
|
78
94
|
};
|
|
79
|
-
|
|
95
|
+
|
|
80
96
|
try {
|
|
81
97
|
let res = await axios(url, c);
|
|
82
|
-
|
|
98
|
+
|
|
83
99
|
token = res.data.token;
|
|
84
100
|
return token;
|
|
85
101
|
} catch (error) {
|
|
86
|
-
|
|
102
|
+
outputError(error);
|
|
87
103
|
process.exit(1);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
|
-
|
|
91
106
|
};
|
|
92
107
|
|
|
93
108
|
const req = async (path, data, onChunk) => {
|
|
@@ -104,15 +119,15 @@ const req = async (path, data, onChunk) => {
|
|
|
104
119
|
|
|
105
120
|
const c = {
|
|
106
121
|
method: "post",
|
|
107
|
-
headers: {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
},
|
|
122
|
+
headers: {
|
|
123
|
+
"Content-Type": "application/json",
|
|
124
|
+
...(token && { Authorization: `Bearer ${token}` }), // Add the authorization bearer token only if token is set
|
|
125
|
+
},
|
|
111
126
|
responseType: typeof onChunk === "function" ? "stream" : "json",
|
|
112
127
|
data: {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
128
|
+
...data,
|
|
129
|
+
session: session.get(),
|
|
130
|
+
stream: typeof onChunk === "function",
|
|
116
131
|
},
|
|
117
132
|
};
|
|
118
133
|
|
|
@@ -130,19 +145,17 @@ const req = async (path, data, onChunk) => {
|
|
|
130
145
|
let lastLineIndex = -1;
|
|
131
146
|
|
|
132
147
|
await new Promise((resolve, reject) => {
|
|
133
|
-
|
|
134
148
|
// theres some kind of race condition here that makes things resolve
|
|
135
149
|
// before the stream is done
|
|
136
|
-
|
|
137
|
-
response.data.on(
|
|
138
|
-
|
|
150
|
+
|
|
151
|
+
response.data.on("data", (chunk) => {
|
|
139
152
|
result += chunk.toString();
|
|
140
153
|
const lines = result.split("\n");
|
|
141
154
|
|
|
142
155
|
const events = lines
|
|
143
156
|
.slice(lastLineIndex + 1, lines.length - 1)
|
|
144
157
|
.filter((line) => line.length)
|
|
145
|
-
.map((line) => JSON.parse(line));
|
|
158
|
+
.map((line) => JSON.parse(line));
|
|
146
159
|
|
|
147
160
|
for (const event of events) {
|
|
148
161
|
onChunk(event);
|
|
@@ -151,8 +164,7 @@ const req = async (path, data, onChunk) => {
|
|
|
151
164
|
lastLineIndex = lines.length - 2;
|
|
152
165
|
});
|
|
153
166
|
|
|
154
|
-
response.data.on(
|
|
155
|
-
|
|
167
|
+
response.data.on("end", () => {
|
|
156
168
|
if (isJsonl) {
|
|
157
169
|
const events = result
|
|
158
170
|
.split("\n")
|
|
@@ -164,11 +176,11 @@ const req = async (path, data, onChunk) => {
|
|
|
164
176
|
onChunk(event);
|
|
165
177
|
}
|
|
166
178
|
}
|
|
167
|
-
|
|
179
|
+
|
|
168
180
|
resolve();
|
|
169
181
|
});
|
|
170
182
|
|
|
171
|
-
response.data.on(
|
|
183
|
+
response.data.on("error", (error) => {
|
|
172
184
|
reject(error);
|
|
173
185
|
});
|
|
174
186
|
});
|
|
@@ -178,7 +190,7 @@ const req = async (path, data, onChunk) => {
|
|
|
178
190
|
|
|
179
191
|
return value;
|
|
180
192
|
} catch (error) {
|
|
181
|
-
|
|
193
|
+
outputError(error);
|
|
182
194
|
}
|
|
183
195
|
};
|
|
184
196
|
|