11const  fs  =  require ( "fs" ) ; 
2+ const  path  =  require ( "path" ) ; 
23const  puppeteer  =  require ( "puppeteer" ) ; 
34
45jest . setTimeout ( 20000 ) ; 
@@ -11,7 +12,10 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
1112    beforeAll ( async ( )  =>  { 
1213        // Setup a headless Chromium browser. 
1314        // 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+         } ) ; 
1519    } ) ; 
1620
1721    afterAll ( async ( )  =>  { 
@@ -151,50 +155,60 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
151155        expect ( codeName ) . toEqual ( "too-large" ) ; 
152156    } ) ; 
153157
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 ) ; 
158163        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+         } ) ; 
160168        await  page . goto ( "http://localhost:5000/editor.html" ) ; 
161169
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" ) ; 
165173            if  ( scriptName  ===  "program test" )  break ; 
166174            await  page . waitFor ( 10 ) ; 
167175        } 
168176        await  page . click ( "#command-files" ) ; 
169177        await  page . click ( "#show-files" ) ; 
178+         await  page . waitFor ( 100 ) ; 
170179        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 ) ; 
174184
175185        expect ( fileExists ) . toBeTruthy ( ) ; 
176186    } ) ; 
177187
178188    it ( "Correctly handles an mpy file" ,  async  function ( ) { 
179189        const  page  =  await  browser . newPage ( ) ; 
180190        await  page . goto ( "http://localhost:5000/editor.html" ) ; 
191+ 
181192        await  page . click ( "#command-files" ) ; 
182193        let  fileInput  =  await  page . $ ( "#file-upload-input" ) ; 
183194        await  fileInput . uploadFile ( "./spec/test-files/samplempyfile.mpy" ) ; 
184195        const  modalContent  =  await  page . evaluate ( "$('#modal-msg-content').text()" ) ; 
185196        const  modalDisplay  =  await  page . evaluate ( "$('#modal-msg-overlay-container').css('display')" ) ; 
197+ 
186198        expect ( modalContent ) . toContain ( "This version of the Python Editor doesn\'t currently support adding .mpy files." ) ; 
187199        expect ( modalDisplay ) . toContain ( "block" ) ; 
188200    } ) ; 
189201
190202    it ( "Correctly handles a file with an invalid extension" ,  async  function ( ) { 
191203        const  page  =  await  browser . newPage ( ) ; 
192204        await  page . goto ( "http://localhost:5000/editor.html" ) ; 
205+ 
193206        await  page . click ( "#command-files" ) ; 
194207        let  fileInput  =  await  page . $ ( "#file-upload-input" ) ; 
195208        await  fileInput . uploadFile ( "./spec/test-files/sampletxtfile.txt" ) ; 
196209        const  modalContent  =  await  page . evaluate ( "$('#modal-msg-content').text()" ) ; 
197210        const  modalDisplay  =  await  page . evaluate ( "$('#modal-msg-overlay-container').css('display')" ) ; 
211+ 
198212        expect ( modalContent ) . toContain ( "The Python Editor can only load files with the .hex or .py extensions." ) ; 
199213        expect ( modalDisplay ) . toContain ( "block" ) ; 
200214    } ) ; 
0 commit comments