Skip to content

Commit 8196c08

Browse files
authored
Move WebDriverCache and WebDriverCreator under same module (#1046)
The WebDriverCache and WebDriverCreator are moved to under keywords.webdrivertools module. Fixes #1040
1 parent db27b34 commit 8196c08

6 files changed

Lines changed: 53 additions & 14 deletions

File tree

src/SeleniumLibrary/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
SelectElementKeywords,
3434
TableElementKeywords,
3535
WaitingKeywords,
36+
WebDriverCache,
3637
WindowKeywords)
3738
from SeleniumLibrary.locators import ElementFinder
38-
from SeleniumLibrary.utils import (Deprecated, LibraryListener, timestr_to_secs,
39-
WebDriverCache)
39+
from SeleniumLibrary.utils import Deprecated, LibraryListener, timestr_to_secs
4040

4141

4242
__version__ = '3.0.2.dev1'

src/SeleniumLibrary/keywords/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
from .selectelement import SelectElementKeywords
2727
from .tableelement import TableElementKeywords
2828
from .waiting import WaitingKeywords
29-
from .webdrivercreator import WebDriverCreator
29+
from .webdrivertools import WebDriverCache
30+
from .webdrivertools import WebDriverCreator
3031
from .window import WindowKeywords

src/SeleniumLibrary/keywords/browsermanagement.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from SeleniumLibrary.utils import (is_truthy, is_noney, secs_to_timestr,
2525
timestr_to_secs)
2626

27-
from .webdrivercreator import WebDriverCreator
27+
from .webdrivertools import WebDriverCreator
2828

2929

3030
class BrowserManagementKeywords(LibraryComponent):
@@ -93,12 +93,13 @@ def open_browser(self, url, browser='firefox', alias=None,
9393
opened, and reset back to 1 when `Close All Browsers` is called.
9494
See `Switch Browser` for more information and examples.
9595
96-
Optional ``remote_url`` is the URL for a remote Selenium server. If
97-
you specify a value for a remote, you can also specify
98-
``desired_capabilities`` to configure, for example, a proxy server
99-
for Internet Explorer or a browser and operating system when using
100-
[http://saucelabs.com|Sauce Labs]. Desired capabilities can be given
101-
either as a Python dictionary or as a string in format
96+
Optional ``remote_url`` is the URL for a
97+
[https://github.com/SeleniumHQ/selenium/wiki/Grid2|Selenium Grid].
98+
99+
Optional ``desired_capabilities`` can be used to configure, for example,
100+
logging preferences for a browser or a browser and operating system
101+
when using [http://saucelabs.com|Sauce Labs]. Desired capabilities can
102+
be given either as a Python dictionary or as a string in format
102103
``key1:value1,key2:value2``.
103104
[https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities|
104105
Selenium documentation] lists possible capabilities that can be
@@ -117,6 +118,9 @@ def open_browser(self, url, browser='firefox', alias=None,
117118
If the provided configuration options are not enough, it is possible
118119
to use `Create Webdriver` to customize browser initialization even
119120
more.
121+
122+
Applying ``desired_capabilities`` argument also for local browser is
123+
new in SeleniumLibrary 3.1.
120124
"""
121125
if is_truthy(remote_url):
122126
self.info("Opening browser '%s' to base url '%s' through "

src/SeleniumLibrary/keywords/webdrivercreator.py renamed to src/SeleniumLibrary/keywords/webdrivertools.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import copy
1817
import os
1918

19+
from robot.utils import ConnectionCache
2020
from selenium import webdriver
2121

2222
from SeleniumLibrary.utils import is_falsy, is_truthy, SELENIUM_VERSION
@@ -80,15 +80,16 @@ def create_chrome(self, desired_capabilities, remote_url, options=None):
8080
return self._remote(default, desired_capabilities, remote_url)
8181
capabilities = self._combine_capabilites(default, desired_capabilities)
8282
if SELENIUM_VERSION.major >= '3' and SELENIUM_VERSION.minor >= '8':
83-
return webdriver.Chrome(desired_capabilities=capabilities, options=options)
83+
return webdriver.Chrome(desired_capabilities=capabilities,
84+
options=options)
8485
return webdriver.Chrome(desired_capabilities=capabilities)
8586

8687
def create_headless_chrome(self, desired_capabilities, remote_url):
8788
if SELENIUM_VERSION.major >= '3' and SELENIUM_VERSION.minor >= '8':
8889
options = webdriver.ChromeOptions()
8990
options.set_headless()
9091
else:
91-
options=None
92+
options = None
9293
return self.create_chrome(desired_capabilities, remote_url, options)
9394

9495
def create_firefox(self, desired_capabilities, remote_url, ff_profile_dir,
@@ -191,3 +192,36 @@ def _combine_capabilites(self, default, user):
191192
default = default.copy()
192193
default.update(user)
193194
return default
195+
196+
197+
class WebDriverCache(ConnectionCache):
198+
199+
def __init__(self):
200+
ConnectionCache.__init__(self, no_current_msg='No current browser')
201+
self._closed = set()
202+
203+
@property
204+
def drivers(self):
205+
return self._connections
206+
207+
@property
208+
def active_drivers(self):
209+
open_drivers = []
210+
for driver in self._connections:
211+
if driver not in self._closed:
212+
open_drivers.append(driver)
213+
return open_drivers
214+
215+
def close(self):
216+
if self.current:
217+
driver = self.current
218+
driver.quit()
219+
self.current = self._no_current
220+
self._closed.add(driver)
221+
222+
def close_all(self):
223+
for driver in self._connections:
224+
if driver not in self._closed:
225+
driver.quit()
226+
self.empty_cache()
227+
return self.current
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from mockito import mock, verify
44

5-
from SeleniumLibrary.utils import WebDriverCache
5+
from SeleniumLibrary.keywords import WebDriverCache
66

77

88
class WebDriverCacheTests(unittest.TestCase):
File renamed without changes.

0 commit comments

Comments
 (0)