Skip to content

web_driver_factory

This module provides the BaseDriverFactory class for creating WebDriver instances.

The BaseDriverFactory class provides methods for creating WebDriver instances based on the specified browser and other parameters.

WebDriverFactory

A class used to create WebDriver instances.

...

Attributes

platform : str the name of the platform

Methods

platform_name(): Returns the name of the platform. selenium_desired_capabilities(browser): Returns the desired capabilities for the specified browser. create_driver(browser, use_grid=None, selenium_grid_ip=None, proxies=None, capabilities=None, chrome_options=None, edge_options=None, firefox_options=None, safari_options=None, custom_experimental_options=None, ie_edge_clear_browser_history=False, firefox_preferences=None, grid_directory_path=None, chrome_version=None, project_path=None): Creates a WebDriver instance based on the specified parameters.

Source code in libs\cafex_ui\src\cafex_ui\web_client\web_driver_factory.py
 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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
class WebDriverFactory:
    """A class used to create WebDriver instances.

    ...

    Attributes
    ----------
    platform : str
        the name of the platform

    Methods
    -------
    platform_name():
        Returns the name of the platform.
    selenium_desired_capabilities(browser):
        Returns the desired capabilities for the specified browser.
    create_driver(browser, use_grid=None, selenium_grid_ip=None, proxies=None, capabilities=None,
                  chrome_options=None, edge_options=None, firefox_options=None, safari_options=None,
                  custom_experimental_options=None, ie_edge_clear_browser_history=False, firefox_preferences=None,
                  grid_directory_path=None, chrome_version=None, project_path=None):
        Creates a WebDriver instance based on the specified parameters.
    """

    def __init__(self) -> None:
        """Constructs all the necessary attributes for the BaseDriverFactory
        object."""
        self.platform = self.platform_name

    @classmethod
    def platform_name(cls) -> str | None:
        """Returns the name of the platform.

        The method returns the name of the platform based on the platform attribute of the BaseDriverFactory
        object.

        Returns
        -------
            platform_name : str
                the name of the platform
        """
        platform_name = platform.system().lower()
        if "win" in platform_name:
            return "WINDOWS"
        if "darwin" in platform_name or "os" in platform_name:
            return "MAC"
        if "linux" in platform_name:
            return "LINUX"
        return None

    @staticmethod
    def selenium_desired_capabilities(browser: str) -> dict:
        """Returns the desired capabilities for the specified browser.

        The method returns the desired capabilities for the specified browser.

        Parameters
        ----------
            browser : str
                the name of the browser

        Returns
        -------
            browser_capabilities : dict
                the desired capabilities for the specified browser
        """
        capabilities = {
            Browser.CHROME.value: DesiredCapabilities.CHROME,
            Browser.FIREFOX.value: DesiredCapabilities.FIREFOX,
            Browser.INTERNETEXPLORER.value: DesiredCapabilities.INTERNETEXPLORER,
            Browser.HEADLESS_CHROME.value: DesiredCapabilities.CHROME,
            Browser.EDGE.value: DesiredCapabilities.EDGE,
        }
        return capabilities.get(browser, {})

    def create_driver(
            self,
            browser: str,
            use_grid: bool = None,
            selenium_grid_ip: str = None,
            proxies: str = None,
            capabilities: dict = None,
            browser_version: str = None,
            chrome_options: list = None,
            chrome_preferences: dict = None,
            firefox_options: list = None,
            firefox_preferences: dict = None,
            safari_options: list = None,
            ie_and_edge_clear_browser_history: bool = False,
            edge_options: list = None,
            ie_options: list = None,
            run_on_browserstack: bool = False,
            browserstack_username: str = None,
            browserstack_access_key: str = None,
            browserstack_capabilities: dict = None,
    ) -> typing.Union[WebDriver, None]:
        """Creates a WebDriver instance.

        The method creates a WebDriver instance based on the specified parameters.

        Parameters
        ----------
            browser : str
                the name of the browser
            use_grid : bool, optional
                a flag indicating whether to use grid execution (default is None)
            selenium_grid_ip : str, optional
                the IP of the Selenium grid (default is None)
            proxies : str, optional
                the proxies to be used (default is None)
            capabilities : dict, optional
                the capabilities to be used (default is None)
            chrome_options : list, optional
                a list of Chrome options to be set (default is None)
            chrome_preferences : dict, optional
                a dictionary of Chrome preferences to be set (default is None)
            firefox_options : list, optional
                a list of Firefox options to be set (default is None)
            firefox_preferences : dict, optional
                a dictionary of Firefox preferences to be set (default is None)
            edge_options : list, optional
                a list of Edge options to be set (default is None)
            safari_options : list, optional
                a list of Safari options to be set (default is None)
            ie_and_edge_clear_browser_history : bool, optional
                a flag indicating whether to clear browser history for IE Edge (default is False)
            browser_version : str, optional
                the version of browser to be used (default is None)
            ie_options : list, optional
                a list of IE options to be set (default is None)
            run_on_browserstack : bool, optional
                a flag indicating whether to run on BrowserStack (default is False)
            browserstack_username : str, optional
                the username for BrowserStack (default is None)
            browserstack_access_key : str, optional
                the access key for BrowserStack (default is None)
            browserstack_capabilities : dict, optional
                a dict of BrowserStack capabilities to be set (default is None)

        Returns
        -------
            driver_ : WebDriver
                the created WebDriver instance
        """
        default_capabilities = self.selenium_desired_capabilities(browser)
        if capabilities:
            default_capabilities.update(capabilities or {})
        proxies = Proxy(proxies).to_capabilities() if proxies else {}

        driver_map = {
            Browser.CHROME.value: ChromeDriver(
                chrome_options=chrome_options,
                chrome_preferences=chrome_preferences,
                chrome_version=browser_version,
                proxies=proxies,
                grid_execution=use_grid,
                run_on_browserstack=run_on_browserstack,
                browserstack_username=browserstack_username,
                browserstack_access_key=browserstack_access_key,
                browserstack_capabilities=browserstack_capabilities,
            ),
            Browser.FIREFOX.value: FirefoxDriverFactory(
                firefox_options=firefox_options,
                firefox_preferences=firefox_preferences,
                proxies=proxies,
                grid_execution=use_grid,
                run_on_browserstack=run_on_browserstack,
                browserstack_username=browserstack_username,
                browserstack_access_key=browserstack_access_key,
                browserstack_capabilities=browserstack_capabilities,
            ),
            Browser.EDGE.value: EdgeDriver(
                edge_options=edge_options,
                proxies=proxies,
                grid_execution=use_grid,
                ie_and_edge_clear_browser_history=ie_and_edge_clear_browser_history,
                run_on_browserstack=run_on_browserstack,
                browserstack_username=browserstack_username,
                browserstack_access_key=browserstack_access_key,
                browserstack_capabilities=browserstack_capabilities,
            ),
            Browser.SAFARI.value: SafariDriver(
                safari_options=safari_options,
                proxies=proxies,
                grid_execution=use_grid,
                run_on_browserstack=run_on_browserstack,
                browserstack_username=browserstack_username,
                browserstack_access_key=browserstack_access_key,
                browserstack_capabilities=browserstack_capabilities,
            ),
            Browser.INTERNETEXPLORER.value: IEDriver(
                ie_options=ie_options,
                proxies=proxies,
                grid_execution=use_grid,
                ie_and_edge_clear_browser_history=ie_and_edge_clear_browser_history,
                run_on_browserstack=run_on_browserstack,
                browserstack_username=browserstack_username,
                browserstack_access_key=browserstack_access_key,
                browserstack_capabilities=browserstack_capabilities,
            ),
        }

        driver_obj = driver_map.get(browser)
        if driver_obj:
            return driver_obj.create_driver(selenium_grid_ip=selenium_grid_ip)
        raise Exception(
            "browser value should be either chrome, firefox, edge, safari or internet explorer"
        )

__init__()

Constructs all the necessary attributes for the BaseDriverFactory object.

Source code in libs\cafex_ui\src\cafex_ui\web_client\web_driver_factory.py
43
44
45
46
def __init__(self) -> None:
    """Constructs all the necessary attributes for the BaseDriverFactory
    object."""
    self.platform = self.platform_name

create_driver(browser, use_grid=None, selenium_grid_ip=None, proxies=None, capabilities=None, browser_version=None, chrome_options=None, chrome_preferences=None, firefox_options=None, firefox_preferences=None, safari_options=None, ie_and_edge_clear_browser_history=False, edge_options=None, ie_options=None, run_on_browserstack=False, browserstack_username=None, browserstack_access_key=None, browserstack_capabilities=None)

Creates a WebDriver instance.

The method creates a WebDriver instance based on the specified parameters.

Parameters
browser : str
    the name of the browser
use_grid : bool, optional
    a flag indicating whether to use grid execution (default is None)
selenium_grid_ip : str, optional
    the IP of the Selenium grid (default is None)
proxies : str, optional
    the proxies to be used (default is None)
capabilities : dict, optional
    the capabilities to be used (default is None)
chrome_options : list, optional
    a list of Chrome options to be set (default is None)
chrome_preferences : dict, optional
    a dictionary of Chrome preferences to be set (default is None)
firefox_options : list, optional
    a list of Firefox options to be set (default is None)
firefox_preferences : dict, optional
    a dictionary of Firefox preferences to be set (default is None)
edge_options : list, optional
    a list of Edge options to be set (default is None)
safari_options : list, optional
    a list of Safari options to be set (default is None)
ie_and_edge_clear_browser_history : bool, optional
    a flag indicating whether to clear browser history for IE Edge (default is False)
browser_version : str, optional
    the version of browser to be used (default is None)
ie_options : list, optional
    a list of IE options to be set (default is None)
run_on_browserstack : bool, optional
    a flag indicating whether to run on BrowserStack (default is False)
browserstack_username : str, optional
    the username for BrowserStack (default is None)
browserstack_access_key : str, optional
    the access key for BrowserStack (default is None)
browserstack_capabilities : dict, optional
    a dict of BrowserStack capabilities to be set (default is None)
Returns
driver_ : WebDriver
    the created WebDriver instance
Source code in libs\cafex_ui\src\cafex_ui\web_client\web_driver_factory.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def create_driver(
        self,
        browser: str,
        use_grid: bool = None,
        selenium_grid_ip: str = None,
        proxies: str = None,
        capabilities: dict = None,
        browser_version: str = None,
        chrome_options: list = None,
        chrome_preferences: dict = None,
        firefox_options: list = None,
        firefox_preferences: dict = None,
        safari_options: list = None,
        ie_and_edge_clear_browser_history: bool = False,
        edge_options: list = None,
        ie_options: list = None,
        run_on_browserstack: bool = False,
        browserstack_username: str = None,
        browserstack_access_key: str = None,
        browserstack_capabilities: dict = None,
) -> typing.Union[WebDriver, None]:
    """Creates a WebDriver instance.

    The method creates a WebDriver instance based on the specified parameters.

    Parameters
    ----------
        browser : str
            the name of the browser
        use_grid : bool, optional
            a flag indicating whether to use grid execution (default is None)
        selenium_grid_ip : str, optional
            the IP of the Selenium grid (default is None)
        proxies : str, optional
            the proxies to be used (default is None)
        capabilities : dict, optional
            the capabilities to be used (default is None)
        chrome_options : list, optional
            a list of Chrome options to be set (default is None)
        chrome_preferences : dict, optional
            a dictionary of Chrome preferences to be set (default is None)
        firefox_options : list, optional
            a list of Firefox options to be set (default is None)
        firefox_preferences : dict, optional
            a dictionary of Firefox preferences to be set (default is None)
        edge_options : list, optional
            a list of Edge options to be set (default is None)
        safari_options : list, optional
            a list of Safari options to be set (default is None)
        ie_and_edge_clear_browser_history : bool, optional
            a flag indicating whether to clear browser history for IE Edge (default is False)
        browser_version : str, optional
            the version of browser to be used (default is None)
        ie_options : list, optional
            a list of IE options to be set (default is None)
        run_on_browserstack : bool, optional
            a flag indicating whether to run on BrowserStack (default is False)
        browserstack_username : str, optional
            the username for BrowserStack (default is None)
        browserstack_access_key : str, optional
            the access key for BrowserStack (default is None)
        browserstack_capabilities : dict, optional
            a dict of BrowserStack capabilities to be set (default is None)

    Returns
    -------
        driver_ : WebDriver
            the created WebDriver instance
    """
    default_capabilities = self.selenium_desired_capabilities(browser)
    if capabilities:
        default_capabilities.update(capabilities or {})
    proxies = Proxy(proxies).to_capabilities() if proxies else {}

    driver_map = {
        Browser.CHROME.value: ChromeDriver(
            chrome_options=chrome_options,
            chrome_preferences=chrome_preferences,
            chrome_version=browser_version,
            proxies=proxies,
            grid_execution=use_grid,
            run_on_browserstack=run_on_browserstack,
            browserstack_username=browserstack_username,
            browserstack_access_key=browserstack_access_key,
            browserstack_capabilities=browserstack_capabilities,
        ),
        Browser.FIREFOX.value: FirefoxDriverFactory(
            firefox_options=firefox_options,
            firefox_preferences=firefox_preferences,
            proxies=proxies,
            grid_execution=use_grid,
            run_on_browserstack=run_on_browserstack,
            browserstack_username=browserstack_username,
            browserstack_access_key=browserstack_access_key,
            browserstack_capabilities=browserstack_capabilities,
        ),
        Browser.EDGE.value: EdgeDriver(
            edge_options=edge_options,
            proxies=proxies,
            grid_execution=use_grid,
            ie_and_edge_clear_browser_history=ie_and_edge_clear_browser_history,
            run_on_browserstack=run_on_browserstack,
            browserstack_username=browserstack_username,
            browserstack_access_key=browserstack_access_key,
            browserstack_capabilities=browserstack_capabilities,
        ),
        Browser.SAFARI.value: SafariDriver(
            safari_options=safari_options,
            proxies=proxies,
            grid_execution=use_grid,
            run_on_browserstack=run_on_browserstack,
            browserstack_username=browserstack_username,
            browserstack_access_key=browserstack_access_key,
            browserstack_capabilities=browserstack_capabilities,
        ),
        Browser.INTERNETEXPLORER.value: IEDriver(
            ie_options=ie_options,
            proxies=proxies,
            grid_execution=use_grid,
            ie_and_edge_clear_browser_history=ie_and_edge_clear_browser_history,
            run_on_browserstack=run_on_browserstack,
            browserstack_username=browserstack_username,
            browserstack_access_key=browserstack_access_key,
            browserstack_capabilities=browserstack_capabilities,
        ),
    }

    driver_obj = driver_map.get(browser)
    if driver_obj:
        return driver_obj.create_driver(selenium_grid_ip=selenium_grid_ip)
    raise Exception(
        "browser value should be either chrome, firefox, edge, safari or internet explorer"
    )

platform_name() classmethod

Returns the name of the platform.

The method returns the name of the platform based on the platform attribute of the BaseDriverFactory object.

Returns
platform_name : str
    the name of the platform
Source code in libs\cafex_ui\src\cafex_ui\web_client\web_driver_factory.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@classmethod
def platform_name(cls) -> str | None:
    """Returns the name of the platform.

    The method returns the name of the platform based on the platform attribute of the BaseDriverFactory
    object.

    Returns
    -------
        platform_name : str
            the name of the platform
    """
    platform_name = platform.system().lower()
    if "win" in platform_name:
        return "WINDOWS"
    if "darwin" in platform_name or "os" in platform_name:
        return "MAC"
    if "linux" in platform_name:
        return "LINUX"
    return None

selenium_desired_capabilities(browser) staticmethod

Returns the desired capabilities for the specified browser.

The method returns the desired capabilities for the specified browser.

Parameters
browser : str
    the name of the browser
Returns
browser_capabilities : dict
    the desired capabilities for the specified browser
Source code in libs\cafex_ui\src\cafex_ui\web_client\web_driver_factory.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@staticmethod
def selenium_desired_capabilities(browser: str) -> dict:
    """Returns the desired capabilities for the specified browser.

    The method returns the desired capabilities for the specified browser.

    Parameters
    ----------
        browser : str
            the name of the browser

    Returns
    -------
        browser_capabilities : dict
            the desired capabilities for the specified browser
    """
    capabilities = {
        Browser.CHROME.value: DesiredCapabilities.CHROME,
        Browser.FIREFOX.value: DesiredCapabilities.FIREFOX,
        Browser.INTERNETEXPLORER.value: DesiredCapabilities.INTERNETEXPLORER,
        Browser.HEADLESS_CHROME.value: DesiredCapabilities.CHROME,
        Browser.EDGE.value: DesiredCapabilities.EDGE,
    }
    return capabilities.get(browser, {})