-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
453 lines (438 loc) · 13.8 KB
/
App.tsx
1
2
3
4
5
6
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { useEffect, useReducer, useState } from 'react';
import classNames from 'classnames';
import {
VSCodeButton,
VSCodeDropdown,
VSCodeLink,
VSCodeOption,
VSCodeProgressRing,
VSCodeRadio,
VSCodeTextArea,
VSCodeTextField,
} from '@vscode/webview-ui-toolkit/react';
import './App.scss';
import { initialState, reducer } from './state';
import { vscode } from './utilities/vscode';
import { TestStep } from './TestStep';
import { PostedMessage } from './types';
import { AssertionInput } from './AssertionInput';
import { Preview } from './Preview';
import { AbstractBaseGenerator, AppiumPython, AppiumJava } from './codegen';
import { AppInfo } from '../../../src/types';
function App() {
const [state, dispatch] = useReducer(reducer, initialState);
const [showAdditionalSettings, setShowAdditionalSettings] = useState(false);
useEffect(() => {
function handler(event: any) {
const message = event.data as PostedMessage; // The json data that the extension sent
switch (message.action) {
case 'update-test-progress':
dispatch({
type: 'setStatus',
value: message.data.status_message,
});
break;
case 'show-video': {
dispatch({
type: 'showVideo',
value: {
sessionId: message.data.session_id,
status: message.data.status_message,
credentials: message.credentials,
},
});
break;
}
case 'show-test-record':
dispatch({
type: 'showTestRecord',
value: {
testRecord: message.data.testRecord,
votes: message.data.votes,
},
});
break;
case 'show-new-test-record':
vscode.postMessage({
action: 'save-steps',
data: message.data,
});
dispatch({
type: 'loadNewRecord',
value: message.data,
});
break;
case 'load-app-names':
dispatch({
type: 'loadAppNames',
value: message.data,
});
break;
case 'recover-from-error':
dispatch({
type: 'setGenerationState',
value: 'errored',
});
// Retain the previous state, which may contain a useful error message
// or snapshot of the video.
vscode.postMessage({
action: 'enable-test-record-navigation',
});
break;
case 'finalize':
dispatch({
type: 'finish',
});
vscode.postMessage({
action: 'enable-test-record-navigation',
});
break;
case 'clear':
dispatch({
type: 'clear',
});
break;
}
}
window.addEventListener('message', handler);
return () => {
window.removeEventListener('message', handler);
};
}, [dispatch]);
useEffect(() => {
vscode.postMessage({
action: 'ready',
});
}, []);
const {
appName,
assertions,
testGoal,
maxSteps,
platform,
status,
steps,
tunnel,
apps,
} = state;
return (
<main>
<section className="inputs">
<h2>What do you want to test?</h2>
<section className="with-label">
<label>App Name</label>
<VSCodeDropdown
onInput={(e) => {
if (e.target && 'value' in e.target) {
const appName: string = e.target.value as string;
const appInfo = apps.find(
(app) => app.name === appName,
) as AppInfo;
dispatch({
type: 'setAppName',
value: {
appName: appName as string,
platformName: appInfo.kind as 'iOS' | 'Android',
},
});
}
}}
value={appName}
className="app-list"
>
{appName !== '' &&
!apps.some((appInfo) => appInfo.name === appName) &&
apps.length > 0 &&
!apps.some((appInfo) => appInfo.name === appName) ? (
<VSCodeOption className="app-not-loaded">{appName}</VSCodeOption>
) : null}
{apps.map((appInfo) => (
<VSCodeOption>{appInfo.name}</VSCodeOption>
))}
</VSCodeDropdown>
</section>
<VSCodeTextArea
resize="both"
value={testGoal}
placeholder="e.g. skip login and add an item into shopping cart"
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setTestGoal',
value: e.target.value as string,
});
}
}}
>
Test Goal
</VSCodeTextArea>
<section className="with-label">
<label>Assert Inputs (optional)</label>
<div className="assertions">
{assertions.map((assertion) => (
<AssertionInput assertion={assertion} dispatch={dispatch} />
))}
</div>
</section>
<div className="additional-settings">
<VSCodeLink
onClick={() => setShowAdditionalSettings(!showAdditionalSettings)}
>
Additional Settings
</VSCodeLink>
<span
className={classNames('codicon', {
'codicon-chevron-down': showAdditionalSettings,
'codicon-chevron-right': !showAdditionalSettings,
})}
/>
</div>
{showAdditionalSettings && (
<section className="inputs">
<VSCodeTextField
placeholder="Sauce Connect tunnel name"
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setTunnelName',
value: e.target.value as string,
});
}
}}
value={state.tunnel?.name ?? ''}
>
Tunnel Name (optional)
</VSCodeTextField>
<VSCodeTextField
placeholder="Sauce Connect tunnel owner"
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setTunnelOwner',
value: e.target.value as string,
});
}
}}
value={state.tunnel?.owner ?? ''}
>
Tunnel Owner (optional)
</VSCodeTextField>
<VSCodeTextField
value={maxSteps.toString()}
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setMaxSteps',
value: e.target.value as string,
});
}
}}
onChange={(e) => {
if (e.target && 'value' in e.target && e.target.value === '') {
dispatch({
type: 'setMaxSteps',
value: '10',
});
}
}}
>
Cut off steps at
</VSCodeTextField>
<VSCodeTextField
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setPlatformVersion',
value: e.target.value as string,
});
}
}}
value={platform.version}
>
Platform Version (optional)
</VSCodeTextField>
<VSCodeTextField
placeholder="e.g. Google.*, iPhone.*"
onInput={(e) => {
if (e.target && 'value' in e.target) {
dispatch({
type: 'setDevice',
value: e.target.value as string,
});
}
}}
value={state.device ?? ''}
>
Device Name (optional)
</VSCodeTextField>
<div>
<VSCodeLink href="https://docs.saucelabs.com/mobile-apps/supported-devices/#dynamic-device-allocation">
Check the docs
</VSCodeLink>{' '}
to learn about device selection
</div>
</section>
)}
<section className="buttons">
<VSCodeButton
disabled={state.generationState === 'generating'}
onClick={() => {
dispatch({
type: 'startGeneration',
});
vscode.postMessage({
action: 'generate-test',
data: {
goal: testGoal,
app_name: appName,
assertions: assertions
.filter((a) => a.value.replaceAll(/\s/g, '') !== '')
.map((a) => a.value),
max_test_steps: maxSteps,
devices: state.device ? [state.device] : [],
platform: platform.name,
platform_version: platform.version,
tunnel_name: tunnel?.name ?? '',
tunnel_owner: tunnel?.owner ?? '',
},
});
}}
>
Generate Test
</VSCodeButton>
<VSCodeButton
appearance="secondary"
disabled={state.generationState !== 'generating'}
onClick={() => {
dispatch({
type: 'stopGeneration',
});
vscode.postMessage({
action: 'stop-generation',
data: {},
});
}}
>
Cancel
</VSCodeButton>
<VSCodeButton
appearance="secondary"
onClick={() => {
dispatch({
type: 'clear',
});
}}
>
Clear
</VSCodeButton>
</section>
</section>
{state.generationState === 'generating' ||
state.generationState === 'errored' ||
state.generationState === 'finishing' ? (
<section className="updates">
<div className="status">
{status}
{state.generationState === 'generating' && <VSCodeProgressRing />}
</div>
{state.credentials && state.sessionId && (
<Preview
credentials={state.credentials}
isLive={state.generationState === 'generating'}
sessionId={state.sessionId}
/>
)}
</section>
) : (
steps && (
<>
<section className="steps">
<h3>Test Steps</h3>
<div>
<VSCodeRadio
checked={state.language === 'python'}
onClick={() =>
dispatch({ type: 'setLanguage', value: 'python' })
}
>
Python
</VSCodeRadio>
<VSCodeRadio
checked={state.language === 'java'}
onClick={() =>
dispatch({ type: 'setLanguage', value: 'java' })
}
>
Java
</VSCodeRadio>
</div>
{steps.map((step) => (
<TestStep
key={`${step.testRecordId}-${step.index}`}
dispatch={dispatch}
language={state.language}
step={step}
/>
))}
</section>
<footer>
<section className="controls">
{state.job?.id && (
<VSCodeButton
appearance="primary"
onClick={() => {
vscode.postMessage({
action: 'open-job-url',
data: {
jobId: state.job?.id,
},
});
}}
>
View Test on Sauce
</VSCodeButton>
)}
{state.job &&
state.job.device &&
state.job.platform.version && (
<VSCodeButton
appearance="secondary"
onClick={() => {
let generator: AbstractBaseGenerator;
if (state.language === 'python') {
generator = new AppiumPython();
} else {
generator = new AppiumJava();
}
const code = generator.generateFullScript(
state.testGoal,
state.appName,
state.job?.device ?? '',
state.job?.platform.version ?? '',
state.region ?? '',
state.platform.name,
state.tunnel?.name ?? '',
state.tunnel?.owner ?? '',
steps,
);
vscode.postMessage({
action: 'show-test-code',
data: {
content: code,
language: state.language,
},
});
}}
>
Open Test Script
</VSCodeButton>
)}
</section>
</footer>
</>
)
)}
</main>
);
}
export default App;