Skip to content

Commit 6003c0b

Browse files
authored
Merge pull request #46 from chavarera/main
[Feature] #40 added optional executable path
2 parents 8d05268 + 7918cf5 commit 6003c0b

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

s_tool/driver.py

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@
2424
)
2525

2626

27+
class Browser:
28+
"""Available Browser list"""
29+
30+
CHROME = "chrome"
31+
CHROMIUM = "chromium"
32+
FIREFOX = "firefox"
33+
IE = "ie"
34+
35+
2736
class SeleniumDriver:
2837
"""SeleniumDriver class to manage driver object and all utility functions at one place"""
2938

30-
def __init__(self, browser=None, headless=False):
39+
def __init__(self, browser=None, headless=False, executable_path=None):
40+
3141
self.browser_list = ["chrome", "chromium", "firefox", "ie"]
3242
self.driver = None
3343
self.browser = browser
3444
self.headless = headless
45+
self.executable_path = executable_path
3546

3647
self._load_driver()
3748
self._load_methods()
@@ -61,39 +72,69 @@ def __enter__(self):
6172
"""
6273
return self._load_driver()
6374

75+
def _exe_path(self, manager, **params):
76+
"""Return Webdriver path
77+
78+
Args:
79+
manager ([ChromeDriver,GeckoDriver,IEDriver]): WebDriver Manager
80+
81+
Returns:
82+
[str]: return an executable path
83+
"""
84+
if self.executable_path:
85+
return self.executable_path
86+
else:
87+
return manager(**params).install()
88+
89+
def _browser(self, browser):
90+
"""Return if an valid browser passed
91+
92+
Args:
93+
browser ([Browser]): Browser Type
94+
95+
Returns:
96+
[bool]: True if browser is valid
97+
"""
98+
available = False
99+
if self.browser.lower() == browser:
100+
available = True
101+
102+
return available
103+
64104
def _load_driver(self):
65105
"""Create Selenium webdriver object"""
66106
self.close()
67107
browser = self.browser.lower()
68-
if browser not in self.browser_list:
69-
raise SToolException(
70-
f"provided browser {browser} doesn't exists. available brower list:{self.browser_list}"
71-
)
72-
73-
# add chrome and firefox different options
74-
options = getattr(
75-
webdriver, browser if browser != "chromium" else "chrome"
76-
).options.Options()
108+
func = partial(self._exe_path)
109+
110+
# Add Browser Options
111+
browser_options = browser if browser != Browser.CHROMIUM else Browser.CHROME
112+
options = getattr(webdriver, browser_options).options.Options()
113+
77114
if self.headless:
78115
options.add_argument("--headless")
79116

80-
if browser in ["chrome", "chromium"]:
81-
browser_type = (
82-
ChromeType.CHROMIUM if browser == "chromium" else ChromeType.GOOGLE
83-
)
84-
self.driver = webdriver.Chrome(
85-
ChromeDriverManager(chrome_type=browser_type).install(), options=options
86-
)
87-
88-
if browser == "firefox":
89-
self.driver = webdriver.Firefox(
90-
executable_path=GeckoDriverManager().install(), options=options
91-
)
92-
93-
if browser == "ie":
94-
self.driver = webdriver.Ie(
95-
executable_path=IEDriverManager().install(), options=options
96-
)
117+
if self._browser(Browser.CHROME):
118+
exe_path = func(ChromeDriverManager, chrome_type=ChromeType.GOOGLE)
119+
_webdriver = webdriver.Chrome
120+
121+
elif self._browser(Browser.CHROMIUM):
122+
exe_path = func(ChromeDriverManager, chrome_type=ChromeType.CHROMIUM)
123+
_webdriver = webdriver.Chrome
124+
125+
elif self._browser(Browser.FIREFOX):
126+
exe_path = func(GeckoDriverManager)
127+
_webdriver = webdriver.Firefox
128+
129+
elif self._browser(Browser.IE):
130+
exe_path = func(IEDriverManager)()
131+
_webdriver = webdriver.Ie
132+
133+
else:
134+
err = f"browser not implemented. available browers :{self.browser_list}"
135+
raise SToolException(err)
136+
137+
self.driver = _webdriver(executable_path=exe_path, options=options)
97138

98139
# Maximize window to give normal browser feel
99140
self.driver.maximize_window()

0 commit comments

Comments
 (0)