forked from paufau/react-native-multiple-modals-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-run.js
More file actions
228 lines (196 loc) · 7.01 KB
/
e2e-run.js
File metadata and controls
228 lines (196 loc) · 7.01 KB
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
import { execSync } from "child_process";
import fs from "fs";
import * as Jimp from "jimp";
import yaml from "js-yaml";
import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import modalsConfig from './demo-components/src/modals.config.json' assert { type: 'json' };
import { Logger } from "./utils.js";
// Constants
const SCREENSHOTS_PATH = './e2e/screenshots/';
const EXPECTED_SCREENSHOTS_PATH = './e2e/expected-screenshots/';
const GENERATED_FLOW_NAME = "generated_flow.yaml";
// Initialize logger
const logger = new Logger();
logger.isVerbose = true;
// Parse command line arguments
const argv = yargs(hideBin(process.argv))
.option('project', {
demandOption: true,
describe: 'Path to the React Native project',
type: 'string',
})
.option('device', {
describe: 'Run the project on a specific device',
type: 'string',
})
.option('platform', {
demandOption: true,
describe: 'Specify the platform for the build',
type: 'string',
choices: ['ios', 'android'],
})
.option('architecture', {
demandOption: true,
alias: 'arch',
describe: 'Specify the architecture for the build',
choices: ['new', 'old'],
default: 'new'
})
.option('run-steps', {
alias: 'steps',
describe: 'Run specific steps in the E2E process',
type: 'array',
choices: ['generation', 'tests', 'images'],
default: ['generation', 'tests', 'images']
})
.option('update-screenshots', {
alias: 'u',
describe: 'Update the expected screenshots with the current ones',
type: 'boolean',
default: false
})
.option('silent', {
alias: 's',
describe: 'Enable silent output',
type: 'boolean',
default: false
})
.help()
.alias('help', 'h')
.parse();
// Setup project variables
const { project, platform, runSteps, architecture, silent, updateScreenshots, device: specificDevice } = argv;
logger.isVerbose = !silent;
const projectName = path.basename(project);
const appId = `com.modals.${projectName}`;
const generatedFlowPath = path.join(process.cwd(), 'e2e', 'flows', GENERATED_FLOW_NAME);
const baseFlowPath = path.join(process.cwd(), 'e2e', 'flows', 'base.yaml');
const getBootedDevice = () => {
if (platform === 'ios') {
if (specificDevice) {
return specificDevice
}
const devices = execSync('xcrun simctl list devices booted', { encoding: 'utf8' });
const bootedDevice = devices.match(/\(([A-F0-9\-]+)\)\s+\(Booted\)/);
return bootedDevice ? bootedDevice[1] : null;
} else if (platform === 'android') {
const devices = execSync('adb devices', { encoding: 'utf8' });
const bootedDevice = devices.match(/(emulator-\d+)\s+device/);
return bootedDevice ? bootedDevice[1] : null;
}
}
const device = getBootedDevice();
// Generate flow configurations for E2E tests
const generateFlowConfigs = () => {
const flowConfigs = [];
modalsConfig.modals.forEach(modalConfig => {
modalConfig.e2e.scenarios.forEach(scenario => {
const testFileName = `${scenario}.yaml`;
const flowConfig = {
runFlow: {
file: testFileName,
env: {
APP_ID: appId,
MODAL_ID: modalConfig.id,
SCREENSHOTS_PATH,
DEVICE: device,
ARCHITECTURE: architecture
}
}
};
flowConfigs.push(flowConfig);
});
});
return flowConfigs;
};
// Generate the combined flow file
const generateFlow = () => {
const baseFlowString = execSync(`cat ${baseFlowPath}`, { encoding: 'utf8' });
const generatedFlowContent = `${baseFlowString}\n${yaml.dump(generateFlowConfigs(), { indent: 2 })}`;
fs.mkdirSync(path.dirname(generatedFlowPath), { recursive: true });
fs.writeFileSync(generatedFlowPath, generatedFlowContent, { encoding: 'utf8' });
};
// Run E2E tests using Maestro
const runE2ETests = () => {
logger.log(`Running E2E tests for project: ${projectName} on device: ${device}`);
try {
execSync(`maestro --device ${device} test ${generatedFlowPath} -e APP_ID=${appId}`, { stdio: 'inherit' });
logger.log('E2E tests completed successfully.');
} catch (error) {
logger.raiseException('E2E tests failed:', error.message);
}
};
// Compare two images with extremely strict criteria
const compareImages = async (actualPath, expectedPath, imageName) => {
try {
const actualImage = await Jimp.Jimp.read(actualPath);
const expectedImage = await Jimp.Jimp.read(expectedPath);
const distance = Jimp.distance(actualImage, expectedImage);
const pixelDiff = Jimp.diff(actualImage, expectedImage);
// Use provided strictness: match if distance <= 0.015625 and diff <= 0.0397%
const isMatch = distance <= 0.02 && pixelDiff.percent <= 0.001;
if (isMatch) {
logger.log(`✅ Screenshot ${imageName} matches expected (distance: ${distance.toFixed(6)}, diff: ${(pixelDiff.percent * 100).toFixed(4)}%)`);
} else {
logger.raiseException(`❌ Screenshot ${imageName} does not match expected (distance: ${distance.toFixed(6)}, diff: ${(pixelDiff.percent * 100).toFixed(4)}%)`);
}
return isMatch;
} catch (error) {
logger.raiseException(`❌ Error comparing images for ${imageName}:`, error.message);
return false;
}
};
// Compare all screenshots with their expected versions
const runImageComparison = async () => {
if (updateScreenshots) {
// Move screenshots to expected-screenshots
logger.log('Updating expected screenshots...');
fs.mkdirSync(EXPECTED_SCREENSHOTS_PATH, { recursive: true });
const screenshots = fs.readdirSync(SCREENSHOTS_PATH);
screenshots.forEach(screenshot => {
const src = path.join(SCREENSHOTS_PATH, screenshot);
const dest = path.join(EXPECTED_SCREENSHOTS_PATH, screenshot);
fs.copyFileSync(src, dest);
});
logger.log('Screenshots copied to expected-screenshots.');
return;
}
const screenshots = fs.readdirSync(SCREENSHOTS_PATH);
let allMatched = true;
for (const screenshot of screenshots) {
const expectedScreenshotPath = path.join(EXPECTED_SCREENSHOTS_PATH, screenshot);
const actualScreenshotPath = path.join(SCREENSHOTS_PATH, screenshot);
if (fs.existsSync(expectedScreenshotPath)) {
const matched = await compareImages(actualScreenshotPath, expectedScreenshotPath, screenshot);
if (!matched) allMatched = false;
} else {
console.warn(`⚠️ Expected screenshot ${screenshot} not found.`);
allMatched = false;
}
}
if (allMatched) {
logger.log('\n🎉 All screenshots match the expected results!');
} else {
logger.log('\n❌ Some screenshots do not match the expected results.');
process.exit(1);
}
};
const cleanScreenshots = () => {
if (fs.existsSync(SCREENSHOTS_PATH)) {
fs.rmSync(SCREENSHOTS_PATH, { recursive: true, force: true });
logger.log('Cleaned up screenshots folder.');
}
};
// Main execution
cleanScreenshots();
if (runSteps.includes('generation')) {
generateFlow();
}
if (runSteps.includes('tests')) {
runE2ETests();
}
if (runSteps.includes('images')) {
runImageComparison();
}