1+ import  os 
2+ import  glob 
3+ import  shutil 
4+ import  time 
15from  PuppeteerLibrary .ikeywords .iformelement_async  import  iFormElementAsync 
26
37
@@ -14,9 +18,33 @@ async def input_text(self, locator: str, text: str, clear=True):
1418    async  def  clear_element_text (self , locator : str ):
1519        await  self ._clear_input_text (locator )
1620
17-     async  def  download_file (self , locator : str ):
18-         raise  Exception ("Sorry, keyword: download_file not support." )
19-     
21+     async  def  download_file (self , locator : str , timeout = None ):
22+         path  =  os .getcwd ()+ '' + os .sep + 'tmp-download' 
23+         try :
24+             shutil .rmtree (path )
25+         except :
26+             self .info ('Cannot cleanup the tmp download folder.' )
27+         page  =  self .library_ctx .get_current_page ().get_page ()
28+         await  page ._client .send ('Page.setDownloadBehavior' , {
29+             'behavior' : 'allow' , 
30+             'downloadPath' : path 
31+         })
32+         await  self .library_ctx .get_current_page ().click_with_selenium_locator (locator )
33+         timeout  =  self .timestr_to_secs_for_default_timeout (timeout )
34+         max_time  =  time .time () +  timeout 
35+         file  =  None 
36+         while  time .time () <  max_time :
37+             time .sleep (1 )
38+             files  =  glob .glob (path + '' + os .sep + '*' )
39+             if  len (files ) ==  1 : 
40+                 file  =  files [0 ]
41+                 break 
42+         return  file 
43+ 
44+     async  def  upload_file (self , locator : str , file_path : str ):
45+         element  =  await  self .library_ctx .get_current_page ().querySelector_with_selenium_locator (locator )
46+         return  await  element .uploadFile (file_path )
47+         
2048    async  def  _clear_input_text (self , selenium_locator ):
2149        await  self .library_ctx .get_current_page ().click_with_selenium_locator (selenium_locator , {'clickCount' : 3 })
2250        await  self .library_ctx .get_current_page ().get_page ().keyboard .press ('Backspace' )
0 commit comments