Skip to content

Commit 4c7487c

Browse files
committed
ENH: Expose capabilities and define default capabilities for all browsers supported.
DOC: Add documentation for all browser modules.
1 parent a1245ce commit 4c7487c

File tree

7 files changed

+191
-5
lines changed

7 files changed

+191
-5
lines changed

botcity/web/bot.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, headless=False):
5454

5555
self._browser = Browser.CHROME
5656
self._options = None
57+
self._capabilities = None
5758
self._driver_path = None
5859

5960
self._driver = None
@@ -141,6 +142,26 @@ def options(self, options):
141142
"""
142143
self._options = options
143144

145+
@property
146+
def capabilities(self):
147+
"""
148+
The capabilities to be passed down to the WebDriver when starting the browser.
149+
150+
Returns:
151+
capabilities (Dict): The browser specific capabilities to be used.
152+
"""
153+
return self._capabilities
154+
155+
@capabilities.setter
156+
def capabilities(self, capabilities):
157+
"""
158+
The capabilities to be passed down to the WebDriver when starting the browser.
159+
160+
Args:
161+
capabilities (Dict): The browser specific capabilities to be used.
162+
"""
163+
self._capabilities = capabilities
164+
144165
@property
145166
def download_folder_path(self):
146167
return self._download_folder_path
@@ -196,13 +217,17 @@ def check_driver():
196217
driver_class = BROWSER_CONFIGS.get(self.browser).get("class")
197218
# Specific default options method for a given browser
198219
func_def_options = BROWSER_CONFIGS.get(self.browser).get("options")
220+
# Specific capabilities method for a given browser
221+
func_def_capabilities = BROWSER_CONFIGS.get(self.browser).get("capabilities")
199222

200223
opt = self.options or func_def_options(self.headless, self._download_folder_path, None)
224+
cap = self.capabilities or func_def_capabilities()
201225
self.options = opt
226+
self.capabilities = cap
202227
driver_path = self.driver_path or check_driver()
203228
self.driver_path = driver_path
204229

205-
self._driver = driver_class(options=opt, executable_path=driver_path)
230+
self._driver = driver_class(options=opt, desired_capabilities=cap, executable_path=driver_path)
206231
self.set_screen_resolution()
207232

208233
def stop_browser(self):

botcity/web/browsers/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ class Browser(str, enum.Enum):
2424
"driver": "chromedriver",
2525
"class": chrome.Chrome,
2626
"options": chrome.default_options,
27+
"capabilities": chrome.default_capabilities,
2728
"wait_for_downloads": chrome.wait_for_downloads
2829
},
2930
Browser.FIREFOX: {
3031
"driver": "geckodriver",
3132
"class": firefox.Firefox,
3233
"options": firefox.default_options,
34+
"capabilities": firefox.default_capabilities,
3335
"wait_for_downloads": firefox.wait_for_downloads
3436
},
3537
Browser.EDGE: {
3638
"driver": "msedgedriver",
3739
"class": edge.Edge,
3840
"options": edge.default_options,
41+
"capabilities": edge.default_capabilities,
3942
"wait_for_downloads": edge.wait_for_downloads
4043
},
4144
}

botcity/web/browsers/chrome.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import json
21
import atexit
2+
import json
33
import os
44
import tempfile
5+
from typing import Dict
56

67
from selenium.webdriver import Chrome # noqa: F401, F403
78
from selenium.webdriver.chrome.options import Options as ChromeOptions
9+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
810

911
from ..util import cleanup_temp_dir
1012

1113

12-
def default_options(headless=False, download_folder_path=None, user_data_dir=None):
14+
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> ChromeOptions:
15+
"""Retrieve the default options for this browser curated by BotCity.
16+
17+
Args:
18+
headless (bool, optional): Whether or not to use the headless mode. Defaults to False.
19+
download_folder_path (str, optional): The default path in which to save files.
20+
If None, the current directory is used. Defaults to None.
21+
user_data_dir ([type], optional): The directory to use as user profile.
22+
If None, a new temporary directory is used. Defaults to None.
23+
24+
Returns:
25+
ChromeOptions: The Chrome options.
26+
"""
1327
chrome_options = ChromeOptions()
1428
chrome_options.add_argument("--remote-debugging-port=0")
1529
chrome_options.add_argument("--no-first-run")
@@ -83,7 +97,19 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
8397
return chrome_options
8498

8599

100+
def default_capabilities() -> Dict:
101+
"""Fetch the default capabilities for this browser.
102+
103+
Returns:
104+
Dict: Dictionary with the default capabilities defined.
105+
"""
106+
return DesiredCapabilities.CHROME.copy()
107+
108+
86109
def wait_for_downloads(driver):
110+
"""Wait for all downloads to finish.
111+
*Important*: This method overwrites the current page with the downloads page.
112+
"""
87113
if not driver.current_url.startswith("chrome://downloads"):
88114
driver.get("chrome://downloads/")
89115
return driver.execute_script("""

botcity/web/browsers/edge.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
import json
33
import os
44
import tempfile
5+
from typing import Dict
56

67
from msedge.selenium_tools import Edge, EdgeOptions # noqa: F401, F403
8+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
79

810
from ..util import cleanup_temp_dir
911

1012

11-
def default_options(headless=False, download_folder_path=None, user_data_dir=None):
13+
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> EdgeOptions:
14+
"""Retrieve the default options for this browser curated by BotCity.
15+
16+
Args:
17+
headless (bool, optional): Whether or not to use the headless mode. Defaults to False.
18+
download_folder_path (str, optional): The default path in which to save files.
19+
If None, the current directory is used. Defaults to None.
20+
user_data_dir ([type], optional): The directory to use as user profile.
21+
If None, a new temporary directory is used. Defaults to None.
22+
23+
Returns:
24+
EdgeOptions: The Edge options.
25+
"""
1226
edge_options = EdgeOptions()
1327
edge_options.use_chromium = True
1428
edge_options.add_argument("--remote-debugging-port=0")
@@ -87,7 +101,19 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
87101
return edge_options
88102

89103

104+
def default_capabilities() -> Dict:
105+
"""Fetch the default capabilities for this browser.
106+
107+
Returns:
108+
Dict: Dictionary with the default capabilities defined.
109+
"""
110+
return DesiredCapabilities.EDGE.copy()
111+
112+
90113
def wait_for_downloads(driver):
114+
"""Wait for all downloads to finish.
115+
*Important*: This method overwrites the current page with the downloads page.
116+
"""
91117
if not driver.current_url.startswith("chrome://downloads"):
92118
driver.get("chrome://downloads/")
93119
return driver.execute_script("""

botcity/web/browsers/firefox.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import atexit
22
import os
33
import tempfile
4+
from typing import Dict
45

56
from selenium import webdriver
67
from selenium.webdriver import Firefox # noqa: F401, F403
8+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
79
from selenium.webdriver.firefox.options import Options as FirefoxOptions
810

911
from ..util import cleanup_temp_dir
@@ -334,7 +336,19 @@
334336
'application/vnd.zzazz.deck+xml']
335337

336338

337-
def default_options(headless=False, download_folder_path=None, user_data_dir=None):
339+
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> FirefoxOptions:
340+
"""Retrieve the default options for this browser curated by BotCity.
341+
342+
Args:
343+
headless (bool, optional): Whether or not to use the headless mode. Defaults to False.
344+
download_folder_path (str, optional): The default path in which to save files.
345+
If None, the current directory is used. Defaults to None.
346+
user_data_dir ([type], optional): The directory to use as user profile.
347+
If None, a new temporary directory is used. Defaults to None.
348+
349+
Returns:
350+
FirefoxOptions: The Firefox options.
351+
"""
338352
firefox_options = FirefoxOptions()
339353
firefox_options.headless = headless
340354
if not user_data_dir:
@@ -361,7 +375,19 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
361375
return firefox_options
362376

363377

378+
def default_capabilities() -> Dict:
379+
"""Fetch the default capabilities for this browser.
380+
381+
Returns:
382+
Dict: Dictionary with the default capabilities defined.
383+
"""
384+
return DesiredCapabilities.FIREFOX.copy()
385+
386+
364387
def wait_for_downloads(driver):
388+
"""Wait for all downloads to finish.
389+
*Important*: This method overwrites the current page with the downloads page.
390+
"""
365391
if not driver.current_url.startswith("about:downloads"):
366392
driver.get("about:downloads")
367393

docs/browsers.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Browsers
2+
3+
Every supported browser has a default set of options and capabilities curated for you that are used by default.
4+
5+
In case you need to customize the options or capabilities you can do so via the `default_options` and `default_capabilities` methods available within each browser module.
6+
7+
Here is an example on how to do that:
8+
9+
```python
10+
from botcity.web import WebBot, Browser
11+
12+
# For Chrome
13+
from botcity.web.browsers.chrome import default_options, default_capabilities
14+
# For Firefox
15+
#from botcity.web.browsers.firefox import default_options, default_capabilities
16+
# For Edge
17+
#from botcity.web.browsers.edge import default_options, default_capabilities
18+
19+
20+
class Bot(WebBot):
21+
def action(self, execution=None):
22+
# Configure whether or not to run on headless mode
23+
self.headless = False
24+
25+
# Fetch the default options for my preferred browser
26+
# Pass in the headless, download_folder_path and user_data_dir
27+
# to be used when building the default_options
28+
def_options = default_options(
29+
headless=self.headless,
30+
download_folder_path=self.download_folder_path,
31+
user_data_dir=None # Informing None here will generate a temporary directory
32+
)
33+
34+
# Add your customized argument
35+
def_options.add_argument("<My Special Argument>")
36+
37+
# Update the options to use the customized Options.
38+
self.options = def_options
39+
40+
# Fetch the default options for my preferred browser
41+
def_capabilities = default_capabilities()
42+
43+
# Set of modify the key and value for my desired capability
44+
def_capabilities["<My Special Parameter>"] = "special value"
45+
46+
# Update the capabilities to use the customized configurations.
47+
self.capabilities = def_capabilities
48+
49+
...
50+
```
51+
52+
## Specific Browser Modules
53+
54+
Here are the documentation for the methods mentioned above for each of the supported browsers.
55+
56+
### Chrome
57+
::: botcity.web.browsers.chrome.default_options
58+
rendering:
59+
heading_level: 4
60+
61+
::: botcity.web.browsers.chrome.default_capabilities
62+
rendering:
63+
heading_level: 4
64+
65+
### Firefox
66+
::: botcity.web.browsers.firefox.default_options
67+
rendering:
68+
heading_level: 4
69+
::: botcity.web.browsers.firefox.default_capabilities
70+
rendering:
71+
heading_level: 4
72+
73+
### Edge
74+
::: botcity.web.browsers.edge.default_options
75+
rendering:
76+
heading_level: 4
77+
::: botcity.web.browsers.edge.default_capabilities
78+
rendering:
79+
heading_level: 4

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ nav:
77
- Home: index.md
88
- Getting Started: intro.md
99
- Framework: bot.md
10+
- Browsers: browsers.md
1011

1112
theme:
1213
icon:

0 commit comments

Comments
 (0)