1
+ #!/usr/bin/env node
2
+
3
+ const puppeteer = require ( 'puppeteer' )
4
+ const path = require ( 'path' )
5
+ const fs = require ( 'fs' ) . promises
6
+
7
+ const SERVER_URL = 'http://127.0.0.1:8080'
8
+ const SCREENSHOTS_DIR = path . join ( __dirname , 'screenshots' )
9
+ const EXAMPLES_DIR = path . join ( __dirname , '..' , 'examples' )
10
+
11
+ const PAGES_TO_CAPTURE = [
12
+ {
13
+ url : 'index.html' ,
14
+ name : 'main-demo' ,
15
+ selectors : [ '#root' ] ,
16
+ description : 'Main demo with interactive controls' ,
17
+ } ,
18
+ {
19
+ url : 'advanced-demo.html' ,
20
+ name : 'advanced-demo' ,
21
+ selectors : [ 'body' ] ,
22
+ viewport : { width : 1920 , height : 1080 } ,
23
+ description : 'Advanced customization examples' ,
24
+ } ,
25
+ {
26
+ url : 'simple-usage.html' ,
27
+ name : 'simple-usage' ,
28
+ selectors : [ 'body' ] ,
29
+ description : 'Simple usage examples' ,
30
+ } ,
31
+ {
32
+ url : 'qr-test.html' ,
33
+ name : 'qr-test' ,
34
+ selectors : [ 'body' ] ,
35
+ description : 'QR code detectability test' ,
36
+ } ,
37
+ {
38
+ url : 'api-docs.html' ,
39
+ name : 'api-docs' ,
40
+ selectors : [ 'body' ] ,
41
+ viewport : { width : 1920 , height : 1080 } ,
42
+ description : 'API documentation' ,
43
+ } ,
44
+ ]
45
+
46
+ async function captureScreenshots ( ) {
47
+ console . log ( '📸 E2E Screenshot Capture Tool' )
48
+ console . log ( '==============================\n' )
49
+
50
+ // Create screenshots directory in e2e folder
51
+ await fs . mkdir ( SCREENSHOTS_DIR , { recursive : true } )
52
+ console . log ( `📁 Created screenshots directory: ${ SCREENSHOTS_DIR } \n` )
53
+
54
+ const browser = await puppeteer . launch ( {
55
+ headless : 'new' ,
56
+ args : [ '--no-sandbox' , '--disable-setuid-sandbox' ] ,
57
+ } )
58
+
59
+ try {
60
+ // Start a local server from examples directory
61
+ console . log ( '🌐 Starting local server...' )
62
+ const { exec } = require ( 'child_process' )
63
+ const serverProcess = exec (
64
+ `cd ${ EXAMPLES_DIR } && npx http-server . -p 8080` ,
65
+ ( error , stdout , stderr ) => {
66
+ if ( error ) {
67
+ console . error ( `Server error: ${ error } ` )
68
+ }
69
+ }
70
+ )
71
+
72
+ // Wait for server to start
73
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
74
+ console . log ( '✅ Server started on http://127.0.0.1:8080\n' )
75
+
76
+ // Capture each page
77
+ for ( const pageConfig of PAGES_TO_CAPTURE ) {
78
+ const page = await browser . newPage ( )
79
+
80
+ if ( pageConfig . viewport ) {
81
+ await page . setViewport ( pageConfig . viewport )
82
+ } else {
83
+ await page . setViewport ( { width : 1280 , height : 800 } )
84
+ }
85
+
86
+ const url = `${ SERVER_URL } /${ pageConfig . url } `
87
+ console . log ( `📸 Capturing: ${ pageConfig . name } ` )
88
+ console . log ( ` URL: ${ url } ` )
89
+ console . log ( ` Description: ${ pageConfig . description } ` )
90
+
91
+ await page . goto ( url , { waitUntil : 'networkidle2' } )
92
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
93
+
94
+ // Capture full page
95
+ const screenshotPath = path . join (
96
+ SCREENSHOTS_DIR ,
97
+ `${ pageConfig . name } .png`
98
+ )
99
+ await page . screenshot ( { path : screenshotPath , fullPage : true } )
100
+ console . log ( ` ✅ Full page saved: ${ screenshotPath } ` )
101
+
102
+ // Also capture specific elements if provided
103
+ if ( pageConfig . selectors ) {
104
+ for ( const selector of pageConfig . selectors ) {
105
+ try {
106
+ const element = await page . $ ( selector )
107
+ if ( element ) {
108
+ const elementScreenshotPath = path . join (
109
+ SCREENSHOTS_DIR ,
110
+ `${ pageConfig . name } -element.png`
111
+ )
112
+ await element . screenshot ( { path : elementScreenshotPath } )
113
+ console . log ( ` ✅ Element saved: ${ elementScreenshotPath } ` )
114
+ }
115
+ } catch ( err ) {
116
+ console . log ( ` ⚠️ Could not capture ${ selector } ` )
117
+ }
118
+ }
119
+ }
120
+
121
+ console . log ( '' )
122
+ await page . close ( )
123
+ }
124
+
125
+ // Capture individual QR code examples
126
+ console . log ( '📸 Capturing individual QR code examples...' )
127
+ const page = await browser . newPage ( )
128
+ await page . setViewport ( { width : 1920 , height : 1080 } )
129
+ await page . goto ( `${ SERVER_URL } /advanced-demo.html` , {
130
+ waitUntil : 'networkidle2' ,
131
+ } )
132
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) )
133
+
134
+ // Capture specific QR code examples
135
+ const examples = [
136
+ { selector : '#basic-example svg' , name : 'qr-basic' } ,
137
+ { selector : '#gradient-example svg' , name : 'qr-gradient' } ,
138
+ { selector : '#circle-modules svg' , name : 'qr-circle' } ,
139
+ { selector : '#diamond-modules svg' , name : 'qr-diamond' } ,
140
+ { selector : '#rounded-modules svg' , name : 'qr-rounded' } ,
141
+ { selector : '#logo-example svg' , name : 'qr-logo' } ,
142
+ { selector : '#neon-example svg' , name : 'qr-neon' } ,
143
+ { selector : '#ocean-example svg' , name : 'qr-ocean' } ,
144
+ ]
145
+
146
+ for ( const example of examples ) {
147
+ try {
148
+ const element = await page . $ ( example . selector )
149
+ if ( element ) {
150
+ const screenshotPath = path . join (
151
+ SCREENSHOTS_DIR ,
152
+ `${ example . name } .png`
153
+ )
154
+ await element . screenshot ( { path : screenshotPath } )
155
+ console . log ( ` ✅ Saved QR: ${ screenshotPath } ` )
156
+ }
157
+ } catch ( err ) {
158
+ console . log ( ` ⚠️ Could not capture ${ example . name } ` )
159
+ }
160
+ }
161
+
162
+ await page . close ( )
163
+
164
+ // Kill the server process
165
+ serverProcess . kill ( )
166
+
167
+ console . log ( '\n✨ Screenshots captured successfully!' )
168
+ console . log ( `📁 All screenshots saved to: ${ SCREENSHOTS_DIR } ` )
169
+
170
+ // Generate summary
171
+ const files = await fs . readdir ( SCREENSHOTS_DIR )
172
+ console . log ( `\n📊 Summary:` )
173
+ console . log ( ` Total files: ${ files . length } ` )
174
+ console . log ( ` Pages captured: ${ PAGES_TO_CAPTURE . length } ` )
175
+ console . log ( ` QR examples: ${ examples . length } ` )
176
+ } finally {
177
+ await browser . close ( )
178
+ }
179
+ }
180
+
181
+ // Run if called directly
182
+ if ( require . main === module ) {
183
+ captureScreenshots ( ) . catch ( ( err ) => {
184
+ console . error ( '❌ Error:' , err )
185
+ process . exit ( 1 )
186
+ } )
187
+ }
188
+
189
+ module . exports = { captureScreenshots }
0 commit comments