-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-tests.js
More file actions
373 lines (317 loc) · 10.3 KB
/
e2e-tests.js
File metadata and controls
373 lines (317 loc) · 10.3 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
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
#!/usr/bin/env node
/**
* E2E Test Suite para Frontend Parking System
* Ejecuta pruebas automatizadas del flujo de usuario principal
* Herramienta: Puppeteer
*/
let puppeteer;
try {
puppeteer = require('puppeteer');
} catch (e) {
// Fallback: usar puppeteer vía npx
puppeteer = require('puppeteer/lib/cjs/puppeteer/launcher/chrome-launcher.js');
}
const fs = require('fs');
const path = require('path');
// Configuración
const TEST_CONFIG = {
baseURL: 'http://localhost:4200',
headless: 'new',
timeout: 30000,
slowMo: 100,
credentials: {
admin: { email: 'admin@parking.com', password: 'admin123' },
operator: { email: 'operator@parking.com', password: 'operator123' }
}
};
// Resultados de pruebas
const testResults = {
totalTests: 0,
passed: 0,
failed: 0,
skipped: 0,
startTime: new Date(),
testCases: []
};
/**
* Registro de prueba
*/
async function logTest(testName, status, error = null) {
testResults.totalTests++;
if (status === 'PASS') {
testResults.passed++;
console.log(`✅ [PASS] ${testName}`);
} else if (status === 'FAIL') {
testResults.failed++;
console.error(`❌ [FAIL] ${testName}`);
if (error) console.error(` Error: ${error.message}`);
} else if (status === 'SKIP') {
testResults.skipped++;
console.log(`⏭️ [SKIP] ${testName}`);
}
testResults.testCases.push({
name: testName,
status,
error: error ? error.message : null,
timestamp: new Date().toISOString()
});
}
/**
* Prueba 1: Frontend accessible
*/
async function testFrontendAccessible(browser) {
try {
const page = await browser.newPage();
const response = await page.goto(`${TEST_CONFIG.baseURL}/`, { waitUntil: 'networkidle2' });
if (response.status() === 200) {
await logTest('Frontend Accessible', 'PASS');
} else {
throw new Error(`Status ${response.status()}`);
}
await page.close();
} catch (error) {
await logTest('Frontend Accessible', 'FAIL', error);
}
}
/**
* Prueba 2: Login page visible
*/
async function testLoginPage(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/login`, { waitUntil: 'networkidle2' });
// Verificar que exista el formulario de login
const loginForm = await page.$('form, [role="presentation"]');
if (loginForm) {
await logTest('Login Page Visible', 'PASS');
} else {
throw new Error('Login form not found');
}
await page.close();
} catch (error) {
await logTest('Login Page Visible', 'FAIL', error);
}
}
/**
* Prueba 3: Login exitoso
*/
async function testLoginSuccess(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/login`, { waitUntil: 'networkidle2' });
// Buscar y completar campos de login
const emailInput = await page.$('input[type="email"], input[name="email"], input[placeholder*="email" i]');
const passwordInput = await page.$('input[type="password"], input[name="password"]');
const submitBtn = await page.$('button[type="submit"], button[aria-label*="login" i]');
if (emailInput && passwordInput && submitBtn) {
await emailInput.type(TEST_CONFIG.credentials.admin.email, { delay: 50 });
await passwordInput.type(TEST_CONFIG.credentials.admin.password, { delay: 50 });
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 }).catch(() => {}),
submitBtn.click()
]);
// Esperar a que se complete la navegación
await page.waitForTimeout(2000);
const url = page.url();
if (url.includes('/dashboard') || !url.includes('/login')) {
await logTest('Login Success', 'PASS');
} else {
throw new Error(`Still on login page: ${url}`);
}
} else {
throw new Error('Login form elements not found');
}
await page.close();
} catch (error) {
await logTest('Login Success', 'FAIL', error);
}
}
/**
* Prueba 4: Dashboard accesible después de login
*/
async function testDashboardAccess(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/dashboard`, { waitUntil: 'networkidle2' });
const url = page.url();
if (url.includes('/dashboard') || url.includes('/')) {
await logTest('Dashboard Access', 'PASS');
} else {
throw new Error(`Redirected from dashboard: ${url}`);
}
await page.close();
} catch (error) {
await logTest('Dashboard Access', 'FAIL', error);
}
}
/**
* Prueba 5: Vehicle Entry page
*/
async function testVehicleEntryPage(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/vehicle-entry`, { waitUntil: 'networkidle2' });
const vehicleForm = await page.$('form, [role="presentation"]');
if (vehicleForm) {
await logTest('Vehicle Entry Page', 'PASS');
} else {
throw new Error('Vehicle entry form not found');
}
await page.close();
} catch (error) {
await logTest('Vehicle Entry Page', 'FAIL', error);
}
}
/**
* Prueba 6: Vehicle list/dashboard
*/
async function testVehiclesList(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/vehicles`, { waitUntil: 'networkidle2' });
await page.waitForTimeout(1000);
const vehicleTable = await page.$('table, [role="grid"], app-vehicle-list');
if (vehicleTable) {
await logTest('Vehicles List', 'PASS');
} else {
throw new Error('Vehicles list not found');
}
await page.close();
} catch (error) {
await logTest('Vehicles List', 'FAIL', error);
}
}
/**
* Prueba 7: Reports page
*/
async function testReportsPage(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/reports`, { waitUntil: 'networkidle2' });
await page.waitForTimeout(1000);
const reportContent = await page.$('[role="main"], app-reports, .reports-container');
if (reportContent) {
await logTest('Reports Page', 'PASS');
} else {
throw new Error('Reports page not found');
}
await page.close();
} catch (error) {
await logTest('Reports Page', 'FAIL', error);
}
}
/**
* Prueba 8: User management page
*/
async function testUserManagementPage(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/users`, { waitUntil: 'networkidle2' });
await page.waitForTimeout(1000);
const usersContent = await page.$('[role="main"], app-users, .users-container');
if (usersContent) {
await logTest('User Management Page', 'PASS');
} else {
throw new Error('User management page not found');
}
await page.close();
} catch (error) {
await logTest('User Management Page', 'FAIL', error);
}
}
/**
* Prueba 9: Navigation menu
*/
async function testNavigationMenu(browser) {
try {
const page = await browser.newPage();
await page.goto(`${TEST_CONFIG.baseURL}/`, { waitUntil: 'networkidle2' });
const navMenu = await page.$('nav, [role="navigation"], .navbar, mat-sidenav');
if (navMenu) {
await logTest('Navigation Menu', 'PASS');
} else {
throw new Error('Navigation menu not found');
}
await page.close();
} catch (error) {
await logTest('Navigation Menu', 'FAIL', error);
}
}
/**
* Prueba 10: Responsive design
*/
async function testResponsiveDesign(browser) {
try {
const viewports = [
{ width: 1920, height: 1080, name: 'Desktop' },
{ width: 768, height: 1024, name: 'Tablet' },
{ width: 375, height: 667, name: 'Mobile' }
];
for (const viewport of viewports) {
const page = await browser.newPage();
await page.setViewport(viewport);
await page.goto(`${TEST_CONFIG.baseURL}/`, { waitUntil: 'networkidle2' });
const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
const bodyWidth = await page.evaluate(() => document.body.scrollWidth);
if (bodyHeight > 0 && bodyWidth > 0) {
console.log(` ✓ ${viewport.name}: ${bodyWidth}x${bodyHeight}`);
}
await page.close();
}
await logTest('Responsive Design', 'PASS');
} catch (error) {
await logTest('Responsive Design', 'FAIL', error);
}
}
/**
* Función principal
*/
async function runTests() {
let browser;
try {
console.log('\n🚀 Iniciando E2E Testing...\n');
console.log(`📍 URL Base: ${TEST_CONFIG.baseURL}`);
console.log(`🕐 Inicio: ${testResults.startTime.toLocaleString()}\n`);
console.log('─'.repeat(60));
browser = await puppeteer.launch({
headless: TEST_CONFIG.headless,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
// Ejecutar todas las pruebas
await testFrontendAccessible(browser);
await testLoginPage(browser);
await testLoginSuccess(browser);
await testDashboardAccess(browser);
await testVehicleEntryPage(browser);
await testVehiclesList(browser);
await testReportsPage(browser);
await testUserManagementPage(browser);
await testNavigationMenu(browser);
await testResponsiveDesign(browser);
console.log('─'.repeat(60));
console.log('\n📊 RESUMEN DE PRUEBAS\n');
console.log(`Total: ${testResults.totalTests}`);
console.log(`✅ Pasadas: ${testResults.passed}`);
console.log(`❌ Fallidas: ${testResults.failed}`);
console.log(`⏭️ Omitidas: ${testResults.skipped}`);
const successRate = testResults.totalTests > 0
? ((testResults.passed / testResults.totalTests) * 100).toFixed(2)
: 0;
console.log(`\n⭐ Tasa de Éxito: ${successRate}%\n`);
// Guardar resultados
testResults.endTime = new Date();
testResults.duration = (testResults.endTime - testResults.startTime) / 1000;
const reportPath = path.join(__dirname, '../TestSprite/e2e-test-results.json');
fs.writeFileSync(reportPath, JSON.stringify(testResults, null, 2));
console.log(`📄 Resultados guardados en: ${reportPath}\n`);
} catch (error) {
console.error('❌ Error ejecutando tests:', error);
process.exit(1);
} finally {
if (browser) {
await browser.close();
}
}
}
// Ejecutar
runTests();