gpt-driver-node 1.0.0-alpha.12 → 1.0.0-alpha.14
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.cjs +134 -80
- package/dist/index.d.cts +5 -0
- package/dist/index.mjs +134 -80
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var axios = require('axios');
|
|
4
|
+
var webdriverio = require('webdriverio');
|
|
4
5
|
var sharp = require('sharp');
|
|
5
6
|
|
|
6
7
|
const delay = async (milliseconds) => {
|
|
@@ -24,6 +25,9 @@ class GptDriver {
|
|
|
24
25
|
appiumSessionConfig;
|
|
25
26
|
driver;
|
|
26
27
|
appiumSessionStarted;
|
|
28
|
+
useGptDriverCloud;
|
|
29
|
+
gptDriverCloudPlatform;
|
|
30
|
+
buildId;
|
|
27
31
|
/**
|
|
28
32
|
* Creates an instance of the GptDriver class.
|
|
29
33
|
*
|
|
@@ -44,9 +48,18 @@ class GptDriver {
|
|
|
44
48
|
*/
|
|
45
49
|
constructor(config) {
|
|
46
50
|
this.apiKey = config.apiKey;
|
|
51
|
+
this.buildId = config.buildId;
|
|
52
|
+
this.useGptDriverCloud = config.useGptDriverCloud;
|
|
47
53
|
this.gptDriverBaseUrl = "https://api.mobileboost.io";
|
|
48
|
-
|
|
49
|
-
|
|
54
|
+
if (config.useGptDriverCloud) {
|
|
55
|
+
if (config.serverConfig?.device?.platform == null) {
|
|
56
|
+
throw new Error("Platform is missing. Please specify the platform when using GPTDriver Cloud.");
|
|
57
|
+
}
|
|
58
|
+
this.gptDriverCloudPlatform = config.serverConfig.device?.platform;
|
|
59
|
+
} else {
|
|
60
|
+
this.initializeDriver(config);
|
|
61
|
+
this.initializeAppiumConfig(config);
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
initializeDriver(config) {
|
|
52
65
|
if (config.driver) {
|
|
@@ -78,46 +91,48 @@ class GptDriver {
|
|
|
78
91
|
*/
|
|
79
92
|
async startSession() {
|
|
80
93
|
console.log(">> Starting session...");
|
|
81
|
-
if (this.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
if (!this.useGptDriverCloud) {
|
|
95
|
+
if (this.driver) {
|
|
96
|
+
let platform;
|
|
97
|
+
let platformVersion;
|
|
98
|
+
let deviceName;
|
|
99
|
+
let sessionId;
|
|
100
|
+
if (this.driver.sessionId == null) {
|
|
101
|
+
const driver = this.driver;
|
|
102
|
+
const capabilities = await driver.getCapabilities();
|
|
103
|
+
platform = capabilities.get("platformName");
|
|
104
|
+
platformVersion = capabilities.get("platformVersion") ?? this.appiumSessionConfig?.platformVersion;
|
|
105
|
+
deviceName = this.appiumSessionConfig?.deviceName ?? capabilities.get("deviceName");
|
|
106
|
+
const session = await driver.getSession();
|
|
107
|
+
sessionId = session.getId();
|
|
108
|
+
} else {
|
|
109
|
+
const driver = this.driver;
|
|
110
|
+
platform = driver.capabilities["appium:platformName"] ?? driver.capabilities["platformName"];
|
|
111
|
+
platformVersion = driver.capabilities["appium:platformVersion"] ?? driver.capabilities["platformVersion"];
|
|
112
|
+
deviceName = this.appiumSessionConfig?.deviceName ?? driver.capabilities["appium:deviceName"] ?? driver.capabilities["deviceName"];
|
|
113
|
+
sessionId = driver.sessionId;
|
|
114
|
+
}
|
|
115
|
+
this.appiumSessionConfig = {
|
|
116
|
+
...this.appiumSessionConfig,
|
|
117
|
+
id: sessionId,
|
|
118
|
+
platform,
|
|
119
|
+
platformVersion,
|
|
120
|
+
deviceName
|
|
121
|
+
};
|
|
94
122
|
} else {
|
|
95
|
-
|
|
96
|
-
platform = driver.capabilities["appium:platformName"] ?? driver.capabilities["platformName"];
|
|
97
|
-
platformVersion = driver.capabilities["appium:platformVersion"] ?? driver.capabilities["platformVersion"];
|
|
98
|
-
deviceName = this.appiumSessionConfig?.deviceName ?? driver.capabilities["appium:deviceName"] ?? driver.capabilities["deviceName"];
|
|
99
|
-
sessionId = driver.sessionId;
|
|
123
|
+
this.appiumSessionConfig.id = await this.createSession();
|
|
100
124
|
}
|
|
101
|
-
this.appiumSessionConfig
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
const url = buildUrl(this.appiumSessionConfig.serverUrl, `/session/${this.appiumSessionConfig.id}/window/rect`);
|
|
126
|
+
const rectResponse = await axios.get(
|
|
127
|
+
url
|
|
128
|
+
);
|
|
129
|
+
this.appiumSessionConfig.size = {
|
|
130
|
+
width: rectResponse.data.value.width,
|
|
131
|
+
height: rectResponse.data.value.height
|
|
107
132
|
};
|
|
108
|
-
|
|
109
|
-
this.appiumSessionConfig.id = await this.createSession();
|
|
133
|
+
this.appiumSessionStarted = true;
|
|
110
134
|
}
|
|
111
135
|
await this.createGptDriverSession();
|
|
112
|
-
const url = buildUrl(this.appiumSessionConfig.serverUrl, `/session/${this.appiumSessionConfig.id}/window/rect`);
|
|
113
|
-
const rectResponse = await axios.get(
|
|
114
|
-
url
|
|
115
|
-
);
|
|
116
|
-
this.appiumSessionConfig.size = {
|
|
117
|
-
width: rectResponse.data.value.width,
|
|
118
|
-
height: rectResponse.data.value.height
|
|
119
|
-
};
|
|
120
|
-
this.appiumSessionStarted = true;
|
|
121
136
|
console.log(`>> Session created. Monitor execution at: ${this.getSessionLink()}`);
|
|
122
137
|
}
|
|
123
138
|
async createSession() {
|
|
@@ -143,15 +158,28 @@ class GptDriver {
|
|
|
143
158
|
`${this.gptDriverBaseUrl}/sessions/create`,
|
|
144
159
|
{
|
|
145
160
|
api_key: this.apiKey,
|
|
146
|
-
appium_session_id: this.appiumSessionConfig
|
|
161
|
+
appium_session_id: this.appiumSessionConfig?.id,
|
|
147
162
|
device_config: {
|
|
148
|
-
platform: this.appiumSessionConfig.
|
|
149
|
-
device: this.appiumSessionConfig
|
|
150
|
-
os: this.appiumSessionConfig
|
|
151
|
-
}
|
|
163
|
+
platform: this.appiumSessionConfig?.platform ?? this.gptDriverCloudPlatform,
|
|
164
|
+
device: this.appiumSessionConfig?.deviceName,
|
|
165
|
+
os: this.appiumSessionConfig?.platformVersion
|
|
166
|
+
},
|
|
167
|
+
use_internal_virtual_device: this.useGptDriverCloud,
|
|
168
|
+
build_id: this.buildId
|
|
152
169
|
}
|
|
153
170
|
);
|
|
154
171
|
this.gptDriverSessionId = response.data.sessionId;
|
|
172
|
+
if (this.useGptDriverCloud) {
|
|
173
|
+
const parsedUrl = new URL(response.data.appiumServerUrl);
|
|
174
|
+
this.driver = await webdriverio.attach({
|
|
175
|
+
options: {
|
|
176
|
+
hostname: parsedUrl.hostname,
|
|
177
|
+
path: parsedUrl.pathname
|
|
178
|
+
},
|
|
179
|
+
sessionId: response.data.appiumSessionId
|
|
180
|
+
});
|
|
181
|
+
this.appiumSessionStarted = true;
|
|
182
|
+
}
|
|
155
183
|
}
|
|
156
184
|
getSessionLink() {
|
|
157
185
|
return `https://app.mobileboost.io/gpt-driver/sessions/${this.gptDriverSessionId}`;
|
|
@@ -168,17 +196,19 @@ class GptDriver {
|
|
|
168
196
|
* @throws {Error} If the request to stop the session fails.
|
|
169
197
|
*/
|
|
170
198
|
async setSessionStatus(status) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
199
|
+
if (this.gptDriverSessionId) {
|
|
200
|
+
console.log(">> Stopping session...");
|
|
201
|
+
await axios.post(
|
|
202
|
+
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/stop`,
|
|
203
|
+
{
|
|
204
|
+
api_key: this.apiKey,
|
|
205
|
+
status
|
|
206
|
+
}
|
|
207
|
+
);
|
|
208
|
+
console.log(">> Session stopped.");
|
|
209
|
+
this.appiumSessionStarted = false;
|
|
210
|
+
this.gptDriverSessionId = void 0;
|
|
211
|
+
}
|
|
182
212
|
}
|
|
183
213
|
/**
|
|
184
214
|
* Executes a specified command within the WebDriver session, optionally using an Appium handler.
|
|
@@ -228,10 +258,15 @@ class GptDriver {
|
|
|
228
258
|
if (!this.appiumSessionStarted) {
|
|
229
259
|
await this.startSession();
|
|
230
260
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
261
|
+
try {
|
|
262
|
+
const results = await this.checkBulk([assertion]);
|
|
263
|
+
if (!Object.values(results).at(0)) {
|
|
264
|
+
await this.setSessionStatus("failed");
|
|
265
|
+
throw new Error(`Failed assertion: ${assertion}`);
|
|
266
|
+
}
|
|
267
|
+
} catch (e) {
|
|
268
|
+
await this.setSessionStatus("failed");
|
|
269
|
+
throw e;
|
|
235
270
|
}
|
|
236
271
|
}
|
|
237
272
|
/**
|
|
@@ -247,16 +282,21 @@ class GptDriver {
|
|
|
247
282
|
if (!this.appiumSessionStarted) {
|
|
248
283
|
await this.startSession();
|
|
249
284
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
285
|
+
try {
|
|
286
|
+
const results = await this.checkBulk(assertions);
|
|
287
|
+
const failedAssertions = Object.values(results).reduce((prev, current, currentIndex) => {
|
|
288
|
+
if (!current) {
|
|
289
|
+
return [...prev, assertions.at(currentIndex)];
|
|
290
|
+
}
|
|
291
|
+
return prev;
|
|
292
|
+
}, []);
|
|
293
|
+
if (failedAssertions.length > 0) {
|
|
294
|
+
await this.setSessionStatus("failed");
|
|
295
|
+
throw new Error(`Failed assertions: ${failedAssertions.join(", ")}`);
|
|
255
296
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
throw new Error(`Failed assertions: ${failedAssertions.join(", ")}`);
|
|
297
|
+
} catch (e) {
|
|
298
|
+
await this.setSessionStatus("failed");
|
|
299
|
+
throw e;
|
|
260
300
|
}
|
|
261
301
|
}
|
|
262
302
|
/**
|
|
@@ -273,17 +313,25 @@ class GptDriver {
|
|
|
273
313
|
await this.startSession();
|
|
274
314
|
}
|
|
275
315
|
console.log(">> Checking:", conditions);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
api_key: this.apiKey,
|
|
281
|
-
base64_screenshot: screenshot,
|
|
282
|
-
assertions: conditions,
|
|
283
|
-
command: `Assert: ${JSON.stringify(conditions)}`
|
|
316
|
+
try {
|
|
317
|
+
let screenshot;
|
|
318
|
+
if (!this.useGptDriverCloud) {
|
|
319
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
284
320
|
}
|
|
285
|
-
|
|
286
|
-
|
|
321
|
+
const response = await axios.post(
|
|
322
|
+
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/assert`,
|
|
323
|
+
{
|
|
324
|
+
api_key: this.apiKey,
|
|
325
|
+
base64_screenshot: screenshot,
|
|
326
|
+
assertions: conditions,
|
|
327
|
+
command: `Assert: ${JSON.stringify(conditions)}`
|
|
328
|
+
}
|
|
329
|
+
);
|
|
330
|
+
return response.data.results;
|
|
331
|
+
} catch (e) {
|
|
332
|
+
await this.setSessionStatus("failed");
|
|
333
|
+
throw e;
|
|
334
|
+
}
|
|
287
335
|
}
|
|
288
336
|
/**
|
|
289
337
|
* Extracts specified information using the GPTDriver.
|
|
@@ -302,7 +350,10 @@ class GptDriver {
|
|
|
302
350
|
await this.startSession();
|
|
303
351
|
}
|
|
304
352
|
console.log(">> Extracting:", extractions);
|
|
305
|
-
|
|
353
|
+
let screenshot;
|
|
354
|
+
if (!this.useGptDriverCloud) {
|
|
355
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
356
|
+
}
|
|
306
357
|
const response = await axios.post(
|
|
307
358
|
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/extract`,
|
|
308
359
|
{
|
|
@@ -318,7 +369,10 @@ class GptDriver {
|
|
|
318
369
|
try {
|
|
319
370
|
let conditionSucceeded = false;
|
|
320
371
|
while (!conditionSucceeded) {
|
|
321
|
-
|
|
372
|
+
let screenshot;
|
|
373
|
+
if (!this.useGptDriverCloud) {
|
|
374
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
375
|
+
}
|
|
322
376
|
console.log(">> Asking GTP Driver for next action...");
|
|
323
377
|
const response = await axios.request(
|
|
324
378
|
{
|
|
@@ -351,10 +405,10 @@ class GptDriver {
|
|
|
351
405
|
}
|
|
352
406
|
}
|
|
353
407
|
async executeCommand(command) {
|
|
354
|
-
const firstAction = command
|
|
408
|
+
const firstAction = command?.data?.actions?.at(0);
|
|
355
409
|
if (firstAction?.type === "pause" && firstAction.duration != null) {
|
|
356
410
|
await delay(firstAction * 1e3);
|
|
357
|
-
} else {
|
|
411
|
+
} else if (!this.useGptDriverCloud) {
|
|
358
412
|
const parsedUrl = new URL(command.url);
|
|
359
413
|
parsedUrl.protocol = this.appiumSessionConfig.serverUrl.protocol;
|
|
360
414
|
parsedUrl.host = this.appiumSessionConfig.serverUrl.host;
|
package/dist/index.d.cts
CHANGED
|
@@ -17,6 +17,8 @@ interface GptDriverConfig {
|
|
|
17
17
|
device?: ServerSessionInitConfig;
|
|
18
18
|
url?: URL | string;
|
|
19
19
|
};
|
|
20
|
+
useGptDriverCloud?: boolean;
|
|
21
|
+
buildId?: string;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
declare class GptDriver {
|
|
@@ -26,6 +28,9 @@ declare class GptDriver {
|
|
|
26
28
|
private appiumSessionConfig?;
|
|
27
29
|
private driver?;
|
|
28
30
|
private appiumSessionStarted?;
|
|
31
|
+
private useGptDriverCloud?;
|
|
32
|
+
private gptDriverCloudPlatform?;
|
|
33
|
+
private buildId?;
|
|
29
34
|
/**
|
|
30
35
|
* Creates an instance of the GptDriver class.
|
|
31
36
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
+
import { attach } from 'webdriverio';
|
|
2
3
|
import sharp from 'sharp';
|
|
3
4
|
|
|
4
5
|
const delay = async (milliseconds) => {
|
|
@@ -22,6 +23,9 @@ class GptDriver {
|
|
|
22
23
|
appiumSessionConfig;
|
|
23
24
|
driver;
|
|
24
25
|
appiumSessionStarted;
|
|
26
|
+
useGptDriverCloud;
|
|
27
|
+
gptDriverCloudPlatform;
|
|
28
|
+
buildId;
|
|
25
29
|
/**
|
|
26
30
|
* Creates an instance of the GptDriver class.
|
|
27
31
|
*
|
|
@@ -42,9 +46,18 @@ class GptDriver {
|
|
|
42
46
|
*/
|
|
43
47
|
constructor(config) {
|
|
44
48
|
this.apiKey = config.apiKey;
|
|
49
|
+
this.buildId = config.buildId;
|
|
50
|
+
this.useGptDriverCloud = config.useGptDriverCloud;
|
|
45
51
|
this.gptDriverBaseUrl = "https://api.mobileboost.io";
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
if (config.useGptDriverCloud) {
|
|
53
|
+
if (config.serverConfig?.device?.platform == null) {
|
|
54
|
+
throw new Error("Platform is missing. Please specify the platform when using GPTDriver Cloud.");
|
|
55
|
+
}
|
|
56
|
+
this.gptDriverCloudPlatform = config.serverConfig.device?.platform;
|
|
57
|
+
} else {
|
|
58
|
+
this.initializeDriver(config);
|
|
59
|
+
this.initializeAppiumConfig(config);
|
|
60
|
+
}
|
|
48
61
|
}
|
|
49
62
|
initializeDriver(config) {
|
|
50
63
|
if (config.driver) {
|
|
@@ -76,46 +89,48 @@ class GptDriver {
|
|
|
76
89
|
*/
|
|
77
90
|
async startSession() {
|
|
78
91
|
console.log(">> Starting session...");
|
|
79
|
-
if (this.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
if (!this.useGptDriverCloud) {
|
|
93
|
+
if (this.driver) {
|
|
94
|
+
let platform;
|
|
95
|
+
let platformVersion;
|
|
96
|
+
let deviceName;
|
|
97
|
+
let sessionId;
|
|
98
|
+
if (this.driver.sessionId == null) {
|
|
99
|
+
const driver = this.driver;
|
|
100
|
+
const capabilities = await driver.getCapabilities();
|
|
101
|
+
platform = capabilities.get("platformName");
|
|
102
|
+
platformVersion = capabilities.get("platformVersion") ?? this.appiumSessionConfig?.platformVersion;
|
|
103
|
+
deviceName = this.appiumSessionConfig?.deviceName ?? capabilities.get("deviceName");
|
|
104
|
+
const session = await driver.getSession();
|
|
105
|
+
sessionId = session.getId();
|
|
106
|
+
} else {
|
|
107
|
+
const driver = this.driver;
|
|
108
|
+
platform = driver.capabilities["appium:platformName"] ?? driver.capabilities["platformName"];
|
|
109
|
+
platformVersion = driver.capabilities["appium:platformVersion"] ?? driver.capabilities["platformVersion"];
|
|
110
|
+
deviceName = this.appiumSessionConfig?.deviceName ?? driver.capabilities["appium:deviceName"] ?? driver.capabilities["deviceName"];
|
|
111
|
+
sessionId = driver.sessionId;
|
|
112
|
+
}
|
|
113
|
+
this.appiumSessionConfig = {
|
|
114
|
+
...this.appiumSessionConfig,
|
|
115
|
+
id: sessionId,
|
|
116
|
+
platform,
|
|
117
|
+
platformVersion,
|
|
118
|
+
deviceName
|
|
119
|
+
};
|
|
92
120
|
} else {
|
|
93
|
-
|
|
94
|
-
platform = driver.capabilities["appium:platformName"] ?? driver.capabilities["platformName"];
|
|
95
|
-
platformVersion = driver.capabilities["appium:platformVersion"] ?? driver.capabilities["platformVersion"];
|
|
96
|
-
deviceName = this.appiumSessionConfig?.deviceName ?? driver.capabilities["appium:deviceName"] ?? driver.capabilities["deviceName"];
|
|
97
|
-
sessionId = driver.sessionId;
|
|
121
|
+
this.appiumSessionConfig.id = await this.createSession();
|
|
98
122
|
}
|
|
99
|
-
this.appiumSessionConfig
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
123
|
+
const url = buildUrl(this.appiumSessionConfig.serverUrl, `/session/${this.appiumSessionConfig.id}/window/rect`);
|
|
124
|
+
const rectResponse = await axios.get(
|
|
125
|
+
url
|
|
126
|
+
);
|
|
127
|
+
this.appiumSessionConfig.size = {
|
|
128
|
+
width: rectResponse.data.value.width,
|
|
129
|
+
height: rectResponse.data.value.height
|
|
105
130
|
};
|
|
106
|
-
|
|
107
|
-
this.appiumSessionConfig.id = await this.createSession();
|
|
131
|
+
this.appiumSessionStarted = true;
|
|
108
132
|
}
|
|
109
133
|
await this.createGptDriverSession();
|
|
110
|
-
const url = buildUrl(this.appiumSessionConfig.serverUrl, `/session/${this.appiumSessionConfig.id}/window/rect`);
|
|
111
|
-
const rectResponse = await axios.get(
|
|
112
|
-
url
|
|
113
|
-
);
|
|
114
|
-
this.appiumSessionConfig.size = {
|
|
115
|
-
width: rectResponse.data.value.width,
|
|
116
|
-
height: rectResponse.data.value.height
|
|
117
|
-
};
|
|
118
|
-
this.appiumSessionStarted = true;
|
|
119
134
|
console.log(`>> Session created. Monitor execution at: ${this.getSessionLink()}`);
|
|
120
135
|
}
|
|
121
136
|
async createSession() {
|
|
@@ -141,15 +156,28 @@ class GptDriver {
|
|
|
141
156
|
`${this.gptDriverBaseUrl}/sessions/create`,
|
|
142
157
|
{
|
|
143
158
|
api_key: this.apiKey,
|
|
144
|
-
appium_session_id: this.appiumSessionConfig
|
|
159
|
+
appium_session_id: this.appiumSessionConfig?.id,
|
|
145
160
|
device_config: {
|
|
146
|
-
platform: this.appiumSessionConfig.
|
|
147
|
-
device: this.appiumSessionConfig
|
|
148
|
-
os: this.appiumSessionConfig
|
|
149
|
-
}
|
|
161
|
+
platform: this.appiumSessionConfig?.platform ?? this.gptDriverCloudPlatform,
|
|
162
|
+
device: this.appiumSessionConfig?.deviceName,
|
|
163
|
+
os: this.appiumSessionConfig?.platformVersion
|
|
164
|
+
},
|
|
165
|
+
use_internal_virtual_device: this.useGptDriverCloud,
|
|
166
|
+
build_id: this.buildId
|
|
150
167
|
}
|
|
151
168
|
);
|
|
152
169
|
this.gptDriverSessionId = response.data.sessionId;
|
|
170
|
+
if (this.useGptDriverCloud) {
|
|
171
|
+
const parsedUrl = new URL(response.data.appiumServerUrl);
|
|
172
|
+
this.driver = await attach({
|
|
173
|
+
options: {
|
|
174
|
+
hostname: parsedUrl.hostname,
|
|
175
|
+
path: parsedUrl.pathname
|
|
176
|
+
},
|
|
177
|
+
sessionId: response.data.appiumSessionId
|
|
178
|
+
});
|
|
179
|
+
this.appiumSessionStarted = true;
|
|
180
|
+
}
|
|
153
181
|
}
|
|
154
182
|
getSessionLink() {
|
|
155
183
|
return `https://app.mobileboost.io/gpt-driver/sessions/${this.gptDriverSessionId}`;
|
|
@@ -166,17 +194,19 @@ class GptDriver {
|
|
|
166
194
|
* @throws {Error} If the request to stop the session fails.
|
|
167
195
|
*/
|
|
168
196
|
async setSessionStatus(status) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
197
|
+
if (this.gptDriverSessionId) {
|
|
198
|
+
console.log(">> Stopping session...");
|
|
199
|
+
await axios.post(
|
|
200
|
+
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/stop`,
|
|
201
|
+
{
|
|
202
|
+
api_key: this.apiKey,
|
|
203
|
+
status
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
console.log(">> Session stopped.");
|
|
207
|
+
this.appiumSessionStarted = false;
|
|
208
|
+
this.gptDriverSessionId = void 0;
|
|
209
|
+
}
|
|
180
210
|
}
|
|
181
211
|
/**
|
|
182
212
|
* Executes a specified command within the WebDriver session, optionally using an Appium handler.
|
|
@@ -226,10 +256,15 @@ class GptDriver {
|
|
|
226
256
|
if (!this.appiumSessionStarted) {
|
|
227
257
|
await this.startSession();
|
|
228
258
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
259
|
+
try {
|
|
260
|
+
const results = await this.checkBulk([assertion]);
|
|
261
|
+
if (!Object.values(results).at(0)) {
|
|
262
|
+
await this.setSessionStatus("failed");
|
|
263
|
+
throw new Error(`Failed assertion: ${assertion}`);
|
|
264
|
+
}
|
|
265
|
+
} catch (e) {
|
|
266
|
+
await this.setSessionStatus("failed");
|
|
267
|
+
throw e;
|
|
233
268
|
}
|
|
234
269
|
}
|
|
235
270
|
/**
|
|
@@ -245,16 +280,21 @@ class GptDriver {
|
|
|
245
280
|
if (!this.appiumSessionStarted) {
|
|
246
281
|
await this.startSession();
|
|
247
282
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
283
|
+
try {
|
|
284
|
+
const results = await this.checkBulk(assertions);
|
|
285
|
+
const failedAssertions = Object.values(results).reduce((prev, current, currentIndex) => {
|
|
286
|
+
if (!current) {
|
|
287
|
+
return [...prev, assertions.at(currentIndex)];
|
|
288
|
+
}
|
|
289
|
+
return prev;
|
|
290
|
+
}, []);
|
|
291
|
+
if (failedAssertions.length > 0) {
|
|
292
|
+
await this.setSessionStatus("failed");
|
|
293
|
+
throw new Error(`Failed assertions: ${failedAssertions.join(", ")}`);
|
|
253
294
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
throw new Error(`Failed assertions: ${failedAssertions.join(", ")}`);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
await this.setSessionStatus("failed");
|
|
297
|
+
throw e;
|
|
258
298
|
}
|
|
259
299
|
}
|
|
260
300
|
/**
|
|
@@ -271,17 +311,25 @@ class GptDriver {
|
|
|
271
311
|
await this.startSession();
|
|
272
312
|
}
|
|
273
313
|
console.log(">> Checking:", conditions);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
api_key: this.apiKey,
|
|
279
|
-
base64_screenshot: screenshot,
|
|
280
|
-
assertions: conditions,
|
|
281
|
-
command: `Assert: ${JSON.stringify(conditions)}`
|
|
314
|
+
try {
|
|
315
|
+
let screenshot;
|
|
316
|
+
if (!this.useGptDriverCloud) {
|
|
317
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
282
318
|
}
|
|
283
|
-
|
|
284
|
-
|
|
319
|
+
const response = await axios.post(
|
|
320
|
+
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/assert`,
|
|
321
|
+
{
|
|
322
|
+
api_key: this.apiKey,
|
|
323
|
+
base64_screenshot: screenshot,
|
|
324
|
+
assertions: conditions,
|
|
325
|
+
command: `Assert: ${JSON.stringify(conditions)}`
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
return response.data.results;
|
|
329
|
+
} catch (e) {
|
|
330
|
+
await this.setSessionStatus("failed");
|
|
331
|
+
throw e;
|
|
332
|
+
}
|
|
285
333
|
}
|
|
286
334
|
/**
|
|
287
335
|
* Extracts specified information using the GPTDriver.
|
|
@@ -300,7 +348,10 @@ class GptDriver {
|
|
|
300
348
|
await this.startSession();
|
|
301
349
|
}
|
|
302
350
|
console.log(">> Extracting:", extractions);
|
|
303
|
-
|
|
351
|
+
let screenshot;
|
|
352
|
+
if (!this.useGptDriverCloud) {
|
|
353
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
354
|
+
}
|
|
304
355
|
const response = await axios.post(
|
|
305
356
|
`${this.gptDriverBaseUrl}/sessions/${this.gptDriverSessionId}/extract`,
|
|
306
357
|
{
|
|
@@ -316,7 +367,10 @@ class GptDriver {
|
|
|
316
367
|
try {
|
|
317
368
|
let conditionSucceeded = false;
|
|
318
369
|
while (!conditionSucceeded) {
|
|
319
|
-
|
|
370
|
+
let screenshot;
|
|
371
|
+
if (!this.useGptDriverCloud) {
|
|
372
|
+
screenshot = await this.getScreenshot(this.appiumSessionConfig);
|
|
373
|
+
}
|
|
320
374
|
console.log(">> Asking GTP Driver for next action...");
|
|
321
375
|
const response = await axios.request(
|
|
322
376
|
{
|
|
@@ -349,10 +403,10 @@ class GptDriver {
|
|
|
349
403
|
}
|
|
350
404
|
}
|
|
351
405
|
async executeCommand(command) {
|
|
352
|
-
const firstAction = command
|
|
406
|
+
const firstAction = command?.data?.actions?.at(0);
|
|
353
407
|
if (firstAction?.type === "pause" && firstAction.duration != null) {
|
|
354
408
|
await delay(firstAction * 1e3);
|
|
355
|
-
} else {
|
|
409
|
+
} else if (!this.useGptDriverCloud) {
|
|
356
410
|
const parsedUrl = new URL(command.url);
|
|
357
411
|
parsedUrl.protocol = this.appiumSessionConfig.serverUrl.protocol;
|
|
358
412
|
parsedUrl.host = this.appiumSessionConfig.serverUrl.host;
|