1
1
const fs = require ( "fs" ) ;
2
+ const path = require ( "path" ) ;
2
3
const puppeteer = require ( "puppeteer" ) ;
3
4
4
5
jest . setTimeout ( 20000 ) ;
@@ -11,7 +12,10 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
11
12
beforeAll ( async ( ) => {
12
13
// Setup a headless Chromium browser.
13
14
// Flags allow Puppeteer to run within a container.
14
- browser = await puppeteer . launch ( { headless : true , args : [ "--no-sandbox" , "--disable-setuid-sandbox" , "--disable-dev-shm-usage" ] } ) ;
15
+ browser = await puppeteer . launch ( {
16
+ headless : true ,
17
+ args : [ "--no-sandbox" , "--disable-setuid-sandbox" , "--disable-dev-shm-usage" ]
18
+ } ) ;
15
19
} ) ;
16
20
17
21
afterAll ( async ( ) => {
@@ -151,50 +155,60 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
151
155
expect ( codeName ) . toEqual ( "too-large" ) ;
152
156
} ) ;
153
157
154
- it ( "Saves a python file with the correct filename" , async function ( ) {
155
- if ( fs . existsSync ( "./spec/test-files/temp-test-files/program_test.py" ) ) {
156
- fs . unlinkSync ( "./spec/test-files/temp-test-files/program_test.py" ) ;
157
- }
158
+ it ( "Saves a python file with the correct filename" , async function ( ) {
159
+ const downloadFolder = path . join ( process . cwd ( ) , "spec" , "test-files" , "temp-test-files" ) ;
160
+ const filePath = path . join ( downloadFolder , "program_test.py" ) ;
161
+ if ( ! fs . existsSync ( downloadFolder ) ) fs . mkdirSync ( downloadFolder ) ;
162
+ if ( fs . existsSync ( filePath ) ) fs . unlinkSync ( filePath ) ;
158
163
const page = await browser . newPage ( ) ;
159
- await page . _client . send ( 'Page.setDownloadBehavior' , { behavior : 'allow' , downloadPath : './spec/test-files/temp-test-files' } ) ;
164
+ await page . _client . send ( 'Page.setDownloadBehavior' , {
165
+ behavior : 'allow' ,
166
+ downloadPath : downloadFolder
167
+ } ) ;
160
168
await page . goto ( "http://localhost:5000/editor.html" ) ;
161
169
162
- await page . evaluate ( ( ) => document . getElementById ( "script-name" ) . value = "program test" )
163
- const scriptName = await page . evaluate ( "document.getElementById('script-name').value" ) ;
164
- for ( let ms = 0 ; ms < 100 ; ms ++ ) {
170
+ await page . evaluate ( ( ) => document . getElementById ( "script-name" ) . value = "program test" )
171
+ for ( let ms = 0 ; ms < 100 ; ms ++ ) {
172
+ let scriptName = await page . evaluate ( "document.getElementById('script-name').value" ) ;
165
173
if ( scriptName === "program test" ) break ;
166
174
await page . waitFor ( 10 ) ;
167
175
}
168
176
await page . click ( "#command-files" ) ;
169
177
await page . click ( "#show-files" ) ;
178
+ await page . waitFor ( 100 ) ;
170
179
await page . click ( ".save-button.save" ) ;
171
- await page . waitFor ( 1000 ) ; //waiting to ensure file is saved
172
- const fileExists = fs . existsSync ( "./spec/test-files/temp-test-files/program_test.py" ) ;
173
- fs . unlinkSync ( "./spec/test-files/temp-test-files/program_test.py" ) ;
180
+ await page . waitFor ( 500 ) ; //waiting to ensure file is saved
181
+ const fileExists = fs . existsSync ( filePath ) ;
182
+ fs . unlinkSync ( filePath ) ;
183
+ fs . rmdirSync ( downloadFolder ) ;
174
184
175
185
expect ( fileExists ) . toBeTruthy ( ) ;
176
186
} ) ;
177
187
178
188
it ( "Correctly handles an mpy file" , async function ( ) {
179
189
const page = await browser . newPage ( ) ;
180
190
await page . goto ( "http://localhost:5000/editor.html" ) ;
191
+
181
192
await page . click ( "#command-files" ) ;
182
193
let fileInput = await page . $ ( "#file-upload-input" ) ;
183
194
await fileInput . uploadFile ( "./spec/test-files/samplempyfile.mpy" ) ;
184
195
const modalContent = await page . evaluate ( "$('#modal-msg-content').text()" ) ;
185
196
const modalDisplay = await page . evaluate ( "$('#modal-msg-overlay-container').css('display')" ) ;
197
+
186
198
expect ( modalContent ) . toContain ( "This version of the Python Editor doesn\'t currently support adding .mpy files." ) ;
187
199
expect ( modalDisplay ) . toContain ( "block" ) ;
188
200
} ) ;
189
201
190
202
it ( "Correctly handles a file with an invalid extension" , async function ( ) {
191
203
const page = await browser . newPage ( ) ;
192
204
await page . goto ( "http://localhost:5000/editor.html" ) ;
205
+
193
206
await page . click ( "#command-files" ) ;
194
207
let fileInput = await page . $ ( "#file-upload-input" ) ;
195
208
await fileInput . uploadFile ( "./spec/test-files/sampletxtfile.txt" ) ;
196
209
const modalContent = await page . evaluate ( "$('#modal-msg-content').text()" ) ;
197
210
const modalDisplay = await page . evaluate ( "$('#modal-msg-overlay-container').css('display')" ) ;
211
+
198
212
expect ( modalContent ) . toContain ( "The Python Editor can only load files with the .hex or .py extensions." ) ;
199
213
expect ( modalDisplay ) . toContain ( "block" ) ;
200
214
} ) ;
0 commit comments