frame.simulator 1.0.2 → 1.0.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/batch.ts +140 -4
- package/commandHandler.ts +2 -4
- package/draw.json +25 -0
- package/index.ts +1 -0
- package/main.ts +73 -0
- package/package.json +3 -2
- package/simulator.ts +2 -14
package/batch.ts
CHANGED
|
@@ -1,11 +1,137 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
1
3
|
import { Command } from './command';
|
|
2
4
|
import { TUserAction } from './types';
|
|
3
5
|
|
|
4
6
|
const PORT: number = parseInt(process.env.PORT || '3010', 10);
|
|
5
7
|
|
|
6
8
|
export class Batch {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
private filename: string;
|
|
10
|
+
|
|
11
|
+
constructor(filename: string) {
|
|
12
|
+
const batchFilePath = path.join(process.cwd(), filename);
|
|
13
|
+
this.filename = batchFilePath;
|
|
14
|
+
|
|
15
|
+
// Check if batch.json exists
|
|
16
|
+
if (!fs.existsSync(batchFilePath)) {
|
|
17
|
+
console.error(
|
|
18
|
+
'Error: batch.json file not found in the current directory',
|
|
19
|
+
);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async run(
|
|
25
|
+
handleCommand: (batch: Batch, item: any, index: number) => Promise<void>,
|
|
26
|
+
) {
|
|
27
|
+
try {
|
|
28
|
+
// Read the batch.json file
|
|
29
|
+
const fileContent = fs.readFileSync(this.filename, 'utf-8');
|
|
30
|
+
|
|
31
|
+
// Parse the JSON content
|
|
32
|
+
let batchData;
|
|
33
|
+
try {
|
|
34
|
+
batchData = JSON.parse(fileContent);
|
|
35
|
+
} catch (parseError) {
|
|
36
|
+
console.error('Error: Invalid JSON in batch.json file');
|
|
37
|
+
console.error(parseError);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
console.log('Processing batch.json...');
|
|
41
|
+
console.log('='.repeat(50));
|
|
42
|
+
|
|
43
|
+
// Check if batchData is an array or object
|
|
44
|
+
if (Array.isArray(batchData)) {
|
|
45
|
+
// If it's an array of objects, process each object sequentially
|
|
46
|
+
for (let index = 0; index < batchData.length; index++) {
|
|
47
|
+
// console.log(`Processing item ${index + 1}/${batchData.length}...`);
|
|
48
|
+
const item = batchData[index];
|
|
49
|
+
if (typeof item === 'object' && item !== null) {
|
|
50
|
+
// Print all keys in the object
|
|
51
|
+
await handleCommand(this, item, index + 1);
|
|
52
|
+
} else {
|
|
53
|
+
console.log(` Not an object: ${item}`);
|
|
54
|
+
}
|
|
55
|
+
console.log('');
|
|
56
|
+
}
|
|
57
|
+
} else if (typeof batchData === 'object' && batchData !== null) {
|
|
58
|
+
// If it's a single object, print its keys
|
|
59
|
+
console.log('Root object keys:');
|
|
60
|
+
const keys = Object.keys(batchData);
|
|
61
|
+
keys.forEach((key) => {
|
|
62
|
+
console.log(` Key: ${key}`);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// If any values are also objects, print their keys too
|
|
66
|
+
keys.forEach((key) => {
|
|
67
|
+
const value = batchData[key];
|
|
68
|
+
if (
|
|
69
|
+
typeof value === 'object' &&
|
|
70
|
+
value !== null &&
|
|
71
|
+
!Array.isArray(value)
|
|
72
|
+
) {
|
|
73
|
+
console.log(`\nKeys in "${key}" object:`);
|
|
74
|
+
Object.keys(value).forEach((subKey) => {
|
|
75
|
+
console.log(` Key: ${subKey}`);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
console.log(
|
|
81
|
+
'Error: batch.json should contain an object or array of objects',
|
|
82
|
+
);
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('Error processing batch.json:');
|
|
87
|
+
console.error(error);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async handleDrawFrame(item: any, index: number) {
|
|
93
|
+
const coordinates = item.payload.coordinates;
|
|
94
|
+
console.log(` Command[${index}]: MainDrawFrame`);
|
|
95
|
+
await this.selectButtonSafe('FDOptions.button.draw-frame');
|
|
96
|
+
|
|
97
|
+
const coords: { x: number; y: number }[] = coordinates.points;
|
|
98
|
+
for (const point of coords) {
|
|
99
|
+
console.log(` Drawing point: (${point.x}, ${point.y})`);
|
|
100
|
+
// Small delay between points to simulate drawing
|
|
101
|
+
// await new Promise(resolve => setTimeout(resolve, 500));
|
|
102
|
+
await this.sampleClickCanvasDrawAt('canvas.draw', point.x, point.y); // Example click to trigger drawing (replace with actual coordinates if needed)
|
|
103
|
+
}
|
|
104
|
+
await this.selectButtonSafe('Finish.button.start-transformation'); // Example button click after drawing (replace with actual testId if needed)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async handleScaleFrame(item: any, index: number) {
|
|
108
|
+
const length = item.payload.length;
|
|
109
|
+
const height = item.payload.height;
|
|
110
|
+
console.log(` Command[${index}]: MainScaleFrame`);
|
|
111
|
+
console.log(
|
|
112
|
+
` Simulating scale frame with length: ${length}, height: ${height}`,
|
|
113
|
+
);
|
|
114
|
+
await this.selectInput('ScaleInput.input.length', length.toString());
|
|
115
|
+
await this.selectInput('ScaleInput.input.height', height.toString());
|
|
116
|
+
await this.selectButtonSafe('ShowOptions.cbx.scaledBox');
|
|
117
|
+
await this.selectButtonSafe('ScaleForm.button.apply');
|
|
118
|
+
await this.selectButtonSafe('ShowOptions.cbx.points');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async handleDefineTrademark(item: any, index: number) {
|
|
122
|
+
const name = item.payload.name;
|
|
123
|
+
console.log(` Command[${index}]: MainDefineTrademark`);
|
|
124
|
+
console.log(` Simulating trademark definition with name: ${name}`);
|
|
125
|
+
await this.selectInput('TrademarkInput.input.name', name);
|
|
126
|
+
await this.selectButtonSafe('TrademarkForm.button.apply');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async selectButtonSafe(testId: string): Promise<void> {
|
|
130
|
+
// Small delay to ensure element is fully ready
|
|
131
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
132
|
+
|
|
133
|
+
// Now click the button
|
|
134
|
+
await this.selectButton(testId);
|
|
9
135
|
}
|
|
10
136
|
|
|
11
137
|
private async sendRequest(
|
|
@@ -69,10 +195,20 @@ export class Batch {
|
|
|
69
195
|
}
|
|
70
196
|
}
|
|
71
197
|
|
|
72
|
-
async
|
|
198
|
+
async selectInput(testId: string, value: string): Promise<void> {
|
|
199
|
+
const status = await this.sendRequest(Command.SimulateInput, {
|
|
200
|
+
testId,
|
|
201
|
+
value,
|
|
202
|
+
});
|
|
203
|
+
if (status !== 'ok') {
|
|
204
|
+
console.log('Failed to send input simulation command');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async sampleClickCanvasDrawAt(testId: string, x: number, y: number) {
|
|
73
209
|
console.log(`sampleClickCanvasDrawAt(${x}, ${y})`);
|
|
74
210
|
await this.sendRequest(Command.SimulateClickOnCanvas, {
|
|
75
|
-
testId
|
|
211
|
+
testId,
|
|
76
212
|
x,
|
|
77
213
|
y,
|
|
78
214
|
});
|
package/commandHandler.ts
CHANGED
|
@@ -8,13 +8,11 @@ export function useCommandHandler() {
|
|
|
8
8
|
|
|
9
9
|
switch (command) {
|
|
10
10
|
case Command.SimulateClick:
|
|
11
|
-
return simulator.
|
|
11
|
+
return simulator.simulateClick(data as TClick);
|
|
12
12
|
case Command.SimulateClickOnCanvas:
|
|
13
13
|
return simulator.simulateClickOnCanvas(data as TClickOnCanvas);
|
|
14
|
-
// case Command.MainScaleFrame:
|
|
15
|
-
// return simulator.simulateScaleFrame(data as TScaleFrame);
|
|
16
14
|
case Command.SimulateInput:
|
|
17
|
-
return simulator.
|
|
15
|
+
return simulator.simulateInput(data as TInput);
|
|
18
16
|
case Command.SimulateCheck:
|
|
19
17
|
return simulator.simulateUserCheck(data as TClick);
|
|
20
18
|
default:
|
package/draw.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"command": "main-draw-frame",
|
|
4
|
+
"payload": {
|
|
5
|
+
"coordinates": {
|
|
6
|
+
"points": [
|
|
7
|
+
{ "x": 200, "y": 100 },
|
|
8
|
+
{ "x": -100, "y": 100 },
|
|
9
|
+
{ "x": -60, "y": 0 },
|
|
10
|
+
{ "x": -100, "y": -100 },
|
|
11
|
+
{ "x": 200, "y": -100 }
|
|
12
|
+
],
|
|
13
|
+
"universeCenter": { "x": 300, "y": 200 }
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"command": "main-scale-frame",
|
|
19
|
+
"payload": { "length": 500, "height": 250 }
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"command": "main-define-trademark",
|
|
23
|
+
"payload": { "x": -95, "y": 62 }
|
|
24
|
+
}
|
|
25
|
+
]
|
package/index.ts
CHANGED
package/main.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Batch } from './batch';
|
|
2
|
+
|
|
3
|
+
async function handleDrawFrame(batch: Batch, item: any, index: number) {
|
|
4
|
+
const coordinates = item.payload.coordinates;
|
|
5
|
+
console.log(` Command[${index}]: MainDrawFrame`);
|
|
6
|
+
await batch.selectButtonSafe('FDOptions.button.draw-frame');
|
|
7
|
+
|
|
8
|
+
const coords: { x: number; y: number }[] = coordinates.points;
|
|
9
|
+
for (const point of coords) {
|
|
10
|
+
console.log(` Drawing point: (${point.x}, ${point.y})`);
|
|
11
|
+
// Small delay between points to simulate drawing
|
|
12
|
+
// await new Promise(resolve => setTimeout(resolve, 500));
|
|
13
|
+
await batch.sampleClickCanvasDrawAt('canvas.draw', point.x, point.y); // Example click to trigger drawing (replace with actual coordinates if needed)
|
|
14
|
+
}
|
|
15
|
+
await batch.selectButtonSafe('Finish.button.start-transformation'); // Example button click after drawing (replace with actual testId if needed)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function handleScaleFrame(batch: Batch, item: any, index: number) {
|
|
19
|
+
const length = item.payload.length;
|
|
20
|
+
const height = item.payload.height;
|
|
21
|
+
console.log(` Command[${index}]: MainScaleFrame`);
|
|
22
|
+
console.log(
|
|
23
|
+
` Simulating scale frame with length: ${length}, height: ${height}`,
|
|
24
|
+
);
|
|
25
|
+
await batch.selectInput('ScaleInput.input.length', length.toString());
|
|
26
|
+
await batch.selectInput('ScaleInput.input.height', height.toString());
|
|
27
|
+
await batch.selectButtonSafe('ShowOptions.cbx.scaledBox');
|
|
28
|
+
await batch.selectButtonSafe('ScaleForm.button.apply');
|
|
29
|
+
await batch.selectButtonSafe('ShowOptions.cbx.points');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function handleDefineTrademark(batch: Batch, item: any, index: number) {
|
|
33
|
+
const name = item.payload.name;
|
|
34
|
+
console.log(` Command[${index}]: MainDefineTrademark`);
|
|
35
|
+
console.log(` Simulating trademark definition with name: ${name}`);
|
|
36
|
+
await batch.selectInput('TrademarkInput.input.name', name);
|
|
37
|
+
await batch.selectButtonSafe('TrademarkForm.button.apply');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function handleCommand(batch: Batch, item: any, index: number) {
|
|
41
|
+
// console.log(JSON.stringify(item, null, 2));
|
|
42
|
+
if (item.command) {
|
|
43
|
+
switch (item.command) {
|
|
44
|
+
case 'main-draw-frame':
|
|
45
|
+
await handleDrawFrame(batch, item, index);
|
|
46
|
+
break;
|
|
47
|
+
case 'main-scale-frame':
|
|
48
|
+
await handleScaleFrame(batch, item, index);
|
|
49
|
+
break;
|
|
50
|
+
case 'main-define-trademark':
|
|
51
|
+
await handleDefineTrademark(batch, item, index);
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unknown command: ${item.command}`);
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
console.log('No command found in this item.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function main(batchFile: string) {
|
|
62
|
+
try {
|
|
63
|
+
const batch = new Batch(batchFile);
|
|
64
|
+
await batch.run(handleCommand);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error('Error in main:', error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Accept batch file path as command-line argument
|
|
71
|
+
const batchFileArg = process.argv[2] || 'batch.json';
|
|
72
|
+
console.log(`Starting batch processing with file: ${batchFileArg}`);
|
|
73
|
+
main(batchFileArg);
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "frame.simulator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A simulator for testing form interactions in a controlled environment.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -b",
|
|
9
|
-
"command-server": "tsx command-server.ts"
|
|
9
|
+
"command-server": "tsx command-server.ts",
|
|
10
|
+
"draw": "tsx main.ts draw.json"
|
|
10
11
|
},
|
|
11
12
|
"peerDependencies": {
|
|
12
13
|
"frame.constants": "^1.0.5"
|
package/simulator.ts
CHANGED
|
@@ -55,7 +55,7 @@ export class Simulator {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
simulateClick(data: TClick) {
|
|
59
59
|
const { testId } = data;
|
|
60
60
|
const element = this.findHTMLElement(testId, HTMLButtonElement, 'click');
|
|
61
61
|
if (element) {
|
|
@@ -77,7 +77,7 @@ export class Simulator {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
simulateInput(data: TInput) {
|
|
81
81
|
const { testId, value } = data;
|
|
82
82
|
const element = this.findHTMLElement(testId, HTMLDivElement, 'input');
|
|
83
83
|
|
|
@@ -95,16 +95,4 @@ export class Simulator {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
|
|
99
|
-
// simulateScaleFrame(scaleValues: TScaleFrame) {
|
|
100
|
-
// const { length, height } = scaleValues;
|
|
101
|
-
// console.log(
|
|
102
|
-
// `${red}Simulating scale frame with length: ${length}, height: ${height}${reset}`,
|
|
103
|
-
// );
|
|
104
|
-
// this.simulateUserInput('ScaleInput.input.length', length.toString());
|
|
105
|
-
// this.simulateUserInput('ScaleInput.input.height', height.toString());
|
|
106
|
-
// this.simulateUserCheck({ testId: 'ShowOptions.cbx.scaledBox' });
|
|
107
|
-
// this.simulateUserClick({ testId: 'ScaleForm.button.apply' });
|
|
108
|
-
// this.simulateUserCheck({ testId: 'ShowOptions.cbx.points' });
|
|
109
|
-
// }
|
|
110
98
|
}
|