Skip to content

Commit cc3d655

Browse files
Implement abc for webdriver (#8366)
* Add Abstract Base Class to WebElement and WebDriver Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
1 parent a0d9489 commit cc3d655

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

py/selenium/webdriver/common/options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
from abc import ABCMeta, abstractmethod
1919

20+
from six import add_metaclass
2021

22+
23+
@add_metaclass(ABCMeta)
2124
class BaseOptions(object):
2225
"""
2326
Base class for individual browser options
2427
"""
25-
__metaclass__ = ABCMeta
2628

2729
def __init__(self):
2830
self._caps = self.default_capabilities

py/selenium/webdriver/remote/file_detector.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
import abc
18+
from abc import ABCMeta, abstractmethod
1919
import os
2020
from selenium.webdriver.common.utils import keys_to_typing
21+
from six import add_metaclass
2122

2223

24+
@add_metaclass(ABCMeta)
2325
class FileDetector(object):
2426
"""
2527
Used for identifying whether a sequence of chars represents the path to a
2628
file.
2729
"""
28-
__metaclass__ = abc.ABCMeta
2930

30-
@abc.abstractmethod
31+
@abstractmethod
3132
def is_local_file(self, *keys):
3233
return
3334

py/selenium/webdriver/remote/webdriver.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
from selenium.webdriver.common.html5.application_cache import ApplicationCache
4242
from selenium.webdriver.support.relative_locator import RelativeBy
4343

44+
from six import add_metaclass
45+
4446
try:
4547
str = basestring
4648
except NameError:
@@ -114,14 +116,14 @@ def get_remote_connection(capabilities, command_executor, keep_alive):
114116
return handler(command_executor, keep_alive=keep_alive)
115117

116118

119+
@add_metaclass(ABCMeta)
117120
class BaseWebDriver(object):
118121
"""
119122
Abstract Base Class for all Webdriver subtypes.
120123
ABC's allow custom implementations of Webdriver to be registered so that isinstance type checks
121124
will succeed.
122125
"""
123-
__metaclass__ = ABCMeta
124-
# TODO: After dropping Python 2, use ABC instead of ABCMeta and remove all Python 2 metaclass declarations.
126+
# TODO: After dropping Python 2, use ABC instead of ABCMeta and remove metaclass decorator.
125127

126128

127129
class WebDriver(BaseWebDriver):

py/selenium/webdriver/remote/webelement.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from selenium.webdriver.common.utils import keys_to_typing
3030
from .command import Command
3131

32+
from six import add_metaclass
33+
3234
# Python 3 imports
3335
try:
3436
str = basestring
@@ -48,12 +50,13 @@
4850
isDisplayed_js = pkgutil.get_data(_pkg, 'isDisplayed.js').decode('utf8')
4951

5052

53+
@add_metaclass(ABCMeta)
5154
class BaseWebElement(object):
5255
"""
5356
Abstract Base Class for WebElement.
5457
ABC's will allow custom types to be registered as a WebElement to pass type checks.
5558
"""
56-
__metaclass__ = ABCMeta
59+
pass
5760

5861

5962
class WebElement(BaseWebElement):

py/selenium/webdriver/support/event_firing_webdriver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626

2727

2828
def _wrap_elements(result, ef_driver):
29+
# handle the case if another wrapper wraps EventFiringWebElement
2930
if isinstance(result, EventFiringWebElement):
3031
return result
3132
elif isinstance(result, WebElement):
3233
return EventFiringWebElement(result, ef_driver)
3334
elif isinstance(result, list):
3435
return [_wrap_elements(item, ef_driver) for item in result]
36+
# result is a built in type.
37+
else:
38+
return result
3539

3640

3741
class EventFiringWebDriver(object):

0 commit comments

Comments
 (0)