-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_selenv.py
93 lines (77 loc) · 3.18 KB
/
prepare_selenv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from selenium import webdriver
import logging
from pathlib import Path
def PrepareTravisDriver(path_binary):
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.binary_location = str(path_binary)
chrome_options.headless = True
chrome_options.add_argument("--no-sandbox") # This make Chromium reachable
driver = webdriver.Chrome(options=chrome_options)
return driver
def PrepareHeorkuDriver(GOOGLE_CHROME_PATH, CHROMEDRIVER_PATH):
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.binary_location = GOOGLE_CHROME_PATH
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(
executable_path=CHROMEDRIVER_PATH,
options=chrome_options
)
return driver
def PrepareLocalChromeDriver(path_binary):
"""
Create a Chrome Session
:param path_ChromeBinary:
:return:
"""
logging.debug(f"ChromeBinary Path:{path_binary}")
driver = webdriver.Chrome(path_binary) # path to chromedriver
return driver
def PrepareLocalFirefoxDriver(path_binary):
"""
Create a Firefox Session
:param path_FirefoxBinary:
:return:
"""
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(str(path_binary))
driver = webdriver.Firefox(firefox_binary=binary)
return driver
def handle_environment() -> (Path, webdriver):
"""
Properly handle the environment by setting the two key variable: self.path_binary and self.driver.
:return:
"""
# If on travis, .travis.yml already took care of the dependency.
if "TRAVIS" in os.environ:
path_binary = Path(r"/usr/bin/google-chrome")
assert path_binary.exists()
logging.debug("TravisCI environment encountered.")
return path_binary, PrepareTravisDriver(path_binary)
if 'on_heroku' in os.environ:
logging.debug("Heroku App environment encountered.")
# Per Source: https://medium.com/@mikelcbrowne/running-chromedriver-with-python-selenium-on-heroku-acc1566d161c
GOOGLE_CHROME_PATH = os.getenv("GOOGLE_CHROME_PATH")
CHROMEDRIVER_PATH = os.getenv("CHROMEDRIVER_PATH")
return GOOGLE_CHROME_PATH, PrepareHeorkuDriver(GOOGLE_CHROME_PATH, CHROMEDRIVER_PATH) # Heroku uses GOOGLE_CHROME_PATH
# Only local scenario going forward.
if "browser_path" not in os.environ:
message = "Browser path not specified!"
logging.critical(message)
raise ValueError(message)
# Set the variable to check.
path_binary = str(Path(os.getenv("browser_path"))).lower()
assert Path(path_binary).exists()
if "firefox" in path_binary:
logging.debug("Local firefox environment encountered.")
return path_binary, PrepareLocalFirefoxDriver(path_binary)
elif "chrome" in path_binary:
logging.debug("Local chrome Driver environment encountered.")
return path_binary, PrepareLocalChromeDriver(path_binary)
else:
message = "Binary path does not contain firefox OR Chrome!"
logging.critical(message)
raise ValueError(message)