@@ -478,9 +478,23 @@ def set_extension_variables():
478478 file .write (text .replace ("__ZeuZ__SibLing_maPP_true" , "__ZeuZ__SibLing_maPP" , 1 ))
479479
480480
481+ def browser_process_status (browser :str ):
482+ list_process_command = ['tasklist' ]
483+ seacrh_command = ['findstr' , f'{ browser } .exe' ]
484+
485+ list_process = subprocess .Popen (list_process_command , stdout = subprocess .PIPE )
486+ search_process = subprocess .Popen (seacrh_command , stdin = list_process .stdout , stdout = subprocess .PIPE , text = True )
487+
488+ output ,_ = search_process .communicate ()
489+
490+ if output :
491+ return True
492+ else :
493+ return False
494+
481495initial_download_folder = None
482496@logger
483- def Open_Browser (dependency , window_size_X = None , window_size_Y = None , capability = None , browser_options = None ):
497+ def Open_Browser (dependency , window_size_X = None , window_size_Y = None , capability = None , browser_options = None , profile_options = None ):
484498 """ Launch browser and create instance """
485499
486500 global selenium_driver
@@ -541,6 +555,55 @@ def Open_Browser(dependency, window_size_X=None, window_size_Y=None, capability=
541555 browser = browser .lower ().strip ()
542556 import selenium
543557 selenium_version = selenium .__version__
558+
559+ kill_process = False
560+
561+ browser_map = {
562+ "microsoft edge chromium" : 'msedge' ,
563+ "chrome" : "chrome" ,
564+ "firefox" : "firefox" ,
565+ "chromeheadless" : "chrome" ,
566+ "firefoxheadless" : "firefox" ,
567+ "edgechromiumheadless" : "msedge" ,
568+ }
569+
570+ if profile_options :
571+ if browser .strip ().lower () in browser_map .keys ():
572+ browser_short_form = browser_map [browser ]
573+ process_status = browser_process_status (browser_short_form )
574+ for options in profile_options :
575+ if options [0 ] == "autokillforprofile" and options [1 ] == "True" :
576+ kill_process = True
577+
578+ if process_status == True and kill_process == False :
579+ err_msg = f'''
580+ Please close all the { browser } browser instance.
581+ Or, use the following optional parameter to do it automatically:
582+ ( auto kill for profile | browser option | True )
583+ '''
584+ CommonUtil .ExecLog (
585+ sModuleInfo , err_msg , 3
586+ )
587+ raise Exception
588+ elif process_status == True and kill_process == True :
589+ task_kill_command = ['taskkill' , '/IM' , f'{ browser_short_form } .exe' , '/F' ]
590+ task_kill_process = subprocess .Popen (task_kill_command , stdout = subprocess .PIPE , text = True )
591+ kill_output ,err = task_kill_process .communicate ()
592+ kill_output_status = kill_output .split ()[0 ]
593+ if kill_output_status == "SUCCESS:" :
594+ CommonUtil .ExecLog (
595+ sModuleInfo , f"Successfully terminated all the { browser_short_form } .exe processes" , 1
596+ )
597+ else :
598+ CommonUtil .ExecLog (
599+ sModuleInfo , f"Could not terminate the { browser_short_form } .exe processes" , 3
600+ )
601+ else :
602+ CommonUtil .ExecLog (
603+ sModuleInfo , f"ZeuZ does not support browser profile for { browser } browser" , 3
604+ )
605+ raise Exception
606+
544607 if browser in ("ios" ,):
545608 # Finds the appium binary and starts the server.
546609 appium_port = start_appium_server ()
@@ -618,6 +681,12 @@ def Open_Browser(dependency, window_size_X=None, window_size_Y=None, capability=
618681 else :
619682 options .add_experimental_option (list (right .items ())[0 ][0 ], list (right .items ())[0 ][1 ])
620683
684+ if profile_options :
685+ for left , right in profile_options :
686+ if left in ("addargument" , "addarguments" ):
687+ options .add_argument (right .strip ())
688+ print (left , right )
689+
621690 if browser == "android" :
622691 mobile_emulation = {"deviceName" : "Pixel 2 XL" }
623692 options .add_experimental_option ("mobileEmulation" , mobile_emulation )
@@ -701,6 +770,12 @@ def chromeheadless():
701770 from selenium .webdriver .firefox .options import Options
702771 options = Options ()
703772
773+ if profile_options :
774+ for left , right in profile_options :
775+ if left in ("addargument" , "addarguments" ):
776+ options .add_argument (right .strip ())
777+ print (left , right )
778+
704779 if remote_browser_version :
705780 options .set_capability ("browserVersion" ,remote_browser_version )
706781
@@ -805,6 +880,12 @@ def firefoxheadless():
805880
806881 options .use_chromium = True
807882
883+ if profile_options :
884+ for left , right in profile_options :
885+ if left in ("addargument" , "addarguments" ):
886+ options .add_argument (right .strip ())
887+ print (left , right )
888+
808889 if "headless" in browser :
809890 def edgeheadless ():
810891 options .headless = True
@@ -1135,6 +1216,7 @@ def Go_To_Link(step_data, page_title=False):
11351216
11361217 # options for add_argument or add_extension etc
11371218 browser_options = []
1219+ profile_options = []
11381220
11391221 # Open browser and create driver if user has not already done so
11401222 global dependency
@@ -1178,6 +1260,8 @@ def Go_To_Link(step_data, page_title=False):
11781260 browser_options .append ([left , right .strip ()])
11791261 elif mid .strip ().lower () in ("chrome experimental option" , "chrome experimental options" ) and dependency ["Browser" ].lower () == "chrome" :
11801262 browser_options .append (["addexperimentaloption" , {left .strip ():CommonUtil .parse_value_into_object (right .strip ())}])
1263+ elif mid .strip ().lower () in ("profile option" , "profile options" ):
1264+ profile_options .append ([left , right .strip ()])
11811265
11821266 if browser_options :
11831267 CommonUtil .ExecLog (sModuleInfo , f"Got these browser_options\n { browser_options } " , 1 )
@@ -1206,13 +1290,13 @@ def Go_To_Link(step_data, page_title=False):
12061290 Tear_Down_Selenium () # If dependency is changed then teardown and relaunch selenium driver
12071291 CommonUtil .ExecLog (sModuleInfo , "Browser not previously opened, doing so now" , 1 )
12081292 if window_size_X == "None" and window_size_Y == "None" :
1209- result = Open_Browser (dependency , capability = capabilities , browser_options = browser_options )
1293+ result = Open_Browser (dependency , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12101294 elif window_size_X == "None" :
1211- result = Open_Browser (dependency , window_size_Y , capability = capabilities , browser_options = browser_options )
1295+ result = Open_Browser (dependency , window_size_Y , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12121296 elif window_size_Y == "None" :
1213- result = Open_Browser (dependency , window_size_X , capability = capabilities , browser_options = browser_options )
1297+ result = Open_Browser (dependency , window_size_X , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12141298 else :
1215- result = Open_Browser (dependency , window_size_X , window_size_Y , capability = capabilities , browser_options = browser_options )
1299+ result = Open_Browser (dependency , window_size_X , window_size_Y , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12161300
12171301 if result == "zeuz_failed" :
12181302 return "zeuz_failed"
@@ -1256,13 +1340,13 @@ def Go_To_Link(step_data, page_title=False):
12561340 else :
12571341 return CommonUtil .Exception_Handler (sys .exc_info ())
12581342 if window_size_X == "None" and window_size_Y == "None" :
1259- result = Open_Browser (dependency , capability = capabilities , browser_options = browser_options )
1343+ result = Open_Browser (dependency , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12601344 elif window_size_X == "None" :
1261- result = Open_Browser (dependency , window_size_Y , capability = capabilities , browser_options = browser_options )
1345+ result = Open_Browser (dependency , window_size_Y , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12621346 elif window_size_Y == "None" :
1263- result = Open_Browser (dependency , window_size_X , capability = capabilities , browser_options = browser_options )
1347+ result = Open_Browser (dependency , window_size_X , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12641348 else :
1265- result = Open_Browser (dependency , window_size_X , window_size_Y , capability = capabilities , browser_options = browser_options )
1349+ result = Open_Browser (dependency , window_size_X , window_size_Y , capability = capabilities , browser_options = browser_options , profile_options = profile_options )
12661350 else :
12671351 result = "zeuz_failed"
12681352
0 commit comments