Skip to content

Commit 104c53d

Browse files
author
Tareq
committed
browser profile addition for Chrome, Edge and Firefox
1 parent 9139720 commit 104c53d

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

Framework/Built_In_Automation/Sequential_Actions/action_declarations/info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"graphql",
8686
"shared capability",
8787
"chrome option", "chrome options", "chrome experimental option", "chrome experimental options",
88+
"profile option", "profile options",
8889
"pre sleep", "post sleep", "pre post sleep", "post pre sleep"
8990
)
9091
patterns = [

Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
481495
initial_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
@@ -540,6 +554,52 @@ def Open_Browser(dependency, window_size_X=None, window_size_Y=None, capability=
540554
CommonUtil.teardown = True
541555
browser = browser.lower().strip()
542556
selenium_version = selenium.__version__
557+
558+
kill_process = False
559+
560+
browser_map = {
561+
"microsoft edge chromium": 'msedge',
562+
"chrome": "chrome",
563+
"fireFox": "firefox",
564+
"firefox": "firefox",
565+
"chromeHeadless": "chrome",
566+
"firefoxHeadless": "firefox",
567+
"edgeChromiumHeadless": "msedge",
568+
}
569+
570+
if profile_options:
571+
if browser 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"ZeuZ does not support browser profile for {browser} browser", 3
600+
)
601+
raise Exception
602+
543603
if browser in ("ios",):
544604
# Finds the appium binary and starts the server.
545605
appium_port = start_appium_server()
@@ -614,6 +674,12 @@ def Open_Browser(dependency, window_size_X=None, window_size_Y=None, capability=
614674
else:
615675
options.add_experimental_option(list(right.items())[0][0], list(right.items())[0][1])
616676

677+
if profile_options:
678+
for left, right in profile_options:
679+
if left in ("addargument", "addarguments"):
680+
options.add_argument(right.strip())
681+
print(left, right)
682+
617683
if browser == "android":
618684
mobile_emulation = {"deviceName": "Pixel 2 XL"}
619685
options.add_experimental_option("mobileEmulation", mobile_emulation)
@@ -692,6 +758,12 @@ def chromeheadless():
692758
from selenium.webdriver.firefox.options import Options
693759
options = Options()
694760

761+
if profile_options:
762+
for left, right in profile_options:
763+
if left in ("addargument", "addarguments"):
764+
options.add_argument(right.strip())
765+
print(left, right)
766+
695767
if remote_browser_version:
696768
options.set_capability("browserVersion",remote_browser_version)
697769

@@ -796,6 +868,12 @@ def firefoxheadless():
796868

797869
options.use_chromium = True
798870

871+
if profile_options:
872+
for left, right in profile_options:
873+
if left in ("addargument", "addarguments"):
874+
options.add_argument(right.strip())
875+
print(left, right)
876+
799877
if "headless" in browser:
800878
def edgeheadless():
801879
options.headless = True
@@ -1121,6 +1199,7 @@ def Go_To_Link(step_data, page_title=False):
11211199

11221200
# options for add_argument or add_extension etc
11231201
browser_options = []
1202+
profile_options = []
11241203

11251204
# Open browser and create driver if user has not already done so
11261205
global dependency
@@ -1161,6 +1240,8 @@ def Go_To_Link(step_data, page_title=False):
11611240
browser_options.append([left, right.strip()])
11621241
elif mid.strip().lower() in ("chrome experimental option", "chrome experimental options") and dependency["Browser"].lower() == "chrome":
11631242
browser_options.append(["addexperimentaloption", {left.strip():CommonUtil.parse_value_into_object(right.strip())}])
1243+
elif mid.strip().lower() in ("profile option", "profile options"):
1244+
profile_options.append([left, right.strip()])
11641245

11651246
if browser_options:
11661247
CommonUtil.ExecLog(sModuleInfo, f"Got these browser_options\n{browser_options}", 1)
@@ -1189,13 +1270,13 @@ def Go_To_Link(step_data, page_title=False):
11891270
Tear_Down_Selenium() # If dependency is changed then teardown and relaunch selenium driver
11901271
CommonUtil.ExecLog(sModuleInfo, "Browser not previously opened, doing so now", 1)
11911272
if window_size_X == "None" and window_size_Y == "None":
1192-
result = Open_Browser(dependency, capability=capabilities, browser_options=browser_options)
1273+
result = Open_Browser(dependency, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
11931274
elif window_size_X == "None":
1194-
result = Open_Browser(dependency, window_size_Y, capability=capabilities, browser_options=browser_options)
1275+
result = Open_Browser(dependency, window_size_Y, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
11951276
elif window_size_Y == "None":
1196-
result = Open_Browser(dependency, window_size_X, capability=capabilities, browser_options=browser_options)
1277+
result = Open_Browser(dependency, window_size_X, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
11971278
else:
1198-
result = Open_Browser(dependency, window_size_X, window_size_Y, capability=capabilities, browser_options=browser_options)
1279+
result = Open_Browser(dependency, window_size_X, window_size_Y, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
11991280

12001281
if result == "zeuz_failed":
12011282
return "zeuz_failed"
@@ -1232,13 +1313,13 @@ def Go_To_Link(step_data, page_title=False):
12321313
else:
12331314
return CommonUtil.Exception_Handler(sys.exc_info())
12341315
if window_size_X == "None" and window_size_Y == "None":
1235-
result = Open_Browser(dependency, capability=capabilities, browser_options=browser_options)
1316+
result = Open_Browser(dependency, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
12361317
elif window_size_X == "None":
1237-
result = Open_Browser(dependency, window_size_Y, capability=capabilities, browser_options=browser_options)
1318+
result = Open_Browser(dependency, window_size_Y, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
12381319
elif window_size_Y == "None":
1239-
result = Open_Browser(dependency, window_size_X, capability=capabilities, browser_options=browser_options)
1320+
result = Open_Browser(dependency, window_size_X, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
12401321
else:
1241-
result = Open_Browser(dependency, window_size_X, window_size_Y, capability=capabilities, browser_options=browser_options)
1322+
result = Open_Browser(dependency, window_size_X, window_size_Y, capability=capabilities, browser_options=browser_options, profile_options=profile_options)
12421323
else:
12431324
result = "zeuz_failed"
12441325

0 commit comments

Comments
 (0)