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
| const puppeteer = require('puppeteer');
class FrontendSmokeTest {
constructor(baseUrl) {
this.baseUrl = baseUrl;
this.browser = null;
this.page = null;
this.testResults = [];
}
async initialize() {
try {
this.browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox']
});
this.page = await this.browser.newPage();
// 페이지 로드 타임아웃 설정
this.page.setDefaultTimeout(10000);
// 콘솔 로그 캡처
this.page.on('console', msg => {
console.log(`Browser Console: ${msg.text()}`);
});
} catch (error) {
console.error('Browser initialization failed:', error);
throw error;
}
}
async logTestResult(testName, success, message) {
const result = {
testName,
success,
message,
timestamp: new Date().toISOString()
};
this.testResults.push(result);
console.log(`${testName}: ${success ? 'PASS' : 'FAIL'} - ${message}`);
}
async testPageLoad() {
try {
const startTime = Date.now();
await this.page.goto(this.baseUrl);
const loadTime = Date.now() - startTime;
const title = await this.page.title();
await this.logTestResult(
'Page Load',
true,
`Page loaded in ${loadTime}ms, Title: ${title}`
);
return true;
} catch (error) {
await this.logTestResult('Page Load', false, error.message);
return false;
}
}
async testNavigation() {
try {
const navLinks = await this.page.$$('nav a');
let success = true;
for (const link of navLinks) {
const href = await link.evaluate(el => el.href);
const text = await link.evaluate(el => el.textContent);
try {
await Promise.all([
this.page.waitForNavigation(),
link.click()
]);
await this.logTestResult(
`Navigation - ${text}`,
true,
`Successfully navigated to ${href}`
);
} catch (error) {
success = false;
await this.logTestResult(
`Navigation - ${text}`,
false,
error.message
);
}
}
return success;
} catch (error) {
await this.logTestResult('Navigation Test', false, error.message);
return false;
}
}
async testFormSubmission() {
try {
// 로그인 폼 테스트
await this.page.goto(`${this.baseUrl}/login`);
await this.page.type('input[name="username"]', 'test_user');
await this.page.type('input[name="password"]', 'test_password');
await Promise.all([
this.page.waitForNavigation(),
this.page.click('button[type="submit"]')
]);
const success = await this.page.evaluate(() => {
return !document.querySelector('.error-message');
});
await this.logTestResult(
'Form Submission',
success,
success ? 'Login form submitted successfully' : 'Login form submission failed'
);
return success;
} catch (error) {
await this.logTestResult('Form Submission', false, error.message);
return false;
}
}
async testAPIIntegration() {
try {
// API 엔드포인트 호출 테스트
const response = await this.page.evaluate(async () => {
const res = await fetch('/api/health');
return res.ok;
});
await this.logTestResult(
'API Integration',
response,
response ? 'API health check passed' : 'API health check failed'
);
return response;
} catch (error) {
await this.logTestResult('API Integration', false, error.message);
return false;
}
}
async runAllTests() {
console.log('Starting Frontend Smoke Tests...');
try {
await this.initialize();
const results = {
pageLoad: await this.testPageLoad(),
navigation: await this.testNavigation(),
formSubmission: await this.testFormSubmission(),
apiIntegration: await this.testAPIIntegration()
};
const allPassed = Object.values(results).every(result => result);
console.log('\nSmoke Test Summary:');
console.log(results);
console.log(`Overall Status: ${allPassed ? 'PASS' : 'FAIL'}`);
return results;
} catch (error) {
console.error('Smoke test failed:', error);
throw error;
} finally {
if (this.browser) {
await this.browser.close();
}
}
}
async generateReport() {
const successCount = this.testResults.filter(r => r.success).length;
const failCount = this.testResults.length - successCount;
const report = {
timestamp: new Date().toISOString(),
totalTests: this.testResults.length,
successCount,
failCount,
successRate: `${((successCount / this.testResults.length) * 100).toFixed(2)}%`,
results: this.testResults
};
console.log('\nTest Report:', JSON.stringify(report, null, 2));
return report;
}
}
// 사용 예시
async function runSmokeTests() {
const smokeTest = new FrontendSmokeTest('http://example.com');
try {
await smokeTest.runAllTests();
await smokeTest.generateReport();
} catch (error) {
console.error('Smoke test execution failed:', error);
}
}
runSmokeTests();
|