-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·115 lines (94 loc) · 3.2 KB
/
server.js
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
#!/usr/bin/env node
const express = require('express');
const Playwright = require('codeceptjs/lib/helper/Playwright');
const path = require('path');
const app = express();
const port = 8191;
app.use(express.json());
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
let playwright;
const tests = {};
console.log('Playwright server started, waiting for init command');
app.post('/init', async (req, res) => {
const { arguments } = req.body;
// where we store artifacts
global.output_dir = arguments.output_dir || './tests/_output';
global.codecept_dir = arguments.data_dir || './tests/_data';
delete arguments.output_dir;
delete arguments.data_dir;
delete arguments.pw_server;
console.log('Init: ', arguments);
try {
playwright = new Playwright(arguments);
await playwright._init();
res.status(200).json({ message: 'Playwright initialized successfully' });
} catch(e) {
console.error('Error initializing Playwright: ', e.message);
res.status(400).json({ message: e.message });
}
});
app.get('/test/:id', async (req, res) => {
const id = req.params.id; // Accessing the id value from the URL
res.status(200).json(tests[id]);
});
app.post('/hook', async (req, res) => {
const { command, arguments } = req.body;
const hook = command;
const { id, title } = arguments;
try {
let result;
switch (hook) {
case 'beforeSuite':
case 'afterSuite':
result = await playwright[`_${hook}`]();
break;
case 'before':
if (!tests[id]) tests[id] = { title }
case 'failed':
const fileName = `${id}_failed.png`;
try {
await playwright.saveScreenshot(fileName);
if (!tests[id]) tests[id] = { title }
if (!tests[id].artifacts) tests[id].artifacts = {}
tests[id].artifacts.screenshot = path.join(output_dir, fileName);
} catch (err) {
console.error('Error saving screenshot: ', err);
// not matter
}
default:
result = await playwright[`_${hook}`](tests[id]);
}
const test = tests[id];
res.status(200).json({ result, test });
} catch (error) {
const message = error.inspect ? error.inspect() : error.message;
res.status(500).json({ message });
}
});
app.post('/command', async (req, res) => {
const { command, arguments } = req.body;
console.log('Command: ', command, arguments);
if (!playwright) {
console.error('Playwright is not initialized!');
return res.status(400).json({ message: 'Playwright is not initialized' });
}
if (!command || !Array.isArray(arguments)) {
return res.status(400).json({ message: 'Invalid request body' });
}
try {
const result = await playwright[command](...arguments);
res.status(200).json({ result });
} catch (error) {
const message = error.inspect ? error.inspect() : error.message;
console.error(`Error executing command ${command}:`, message);
res.status(500).json({ message });
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});