Skip to content

Commit

Permalink
[py] Automate RemoteConnection subclass selection (#8010)
Browse files Browse the repository at this point in the history
Select an appropriate RemoteConnection subclass based on specified
capabilities while instantiating WebDriver (aka Remote) class.

Add a new field "brower_name" to RemoteConnection class.
Field can be overridden in RemoteConnection subclasses
that wish to handle connections to specific browsers.

Related #7959
  • Loading branch information
Abdelrahman Talaat authored Feb 14, 2020
1 parent d0b6ab1 commit 3169619
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions py/selenium/webdriver/chromium/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
# under the License.

from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class ChromiumRemoteConnection(RemoteConnection):

browser_name = DesiredCapabilities.CHROME['browserName']

def __init__(self, remote_server_addr, keep_alive=True):
RemoteConnection.__init__(self, remote_server_addr, keep_alive)
self._commands["launchApp"] = ('POST', '/session/$sessionId/chromium/launch_app')
Expand Down
4 changes: 4 additions & 0 deletions py/selenium/webdriver/firefox/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
# under the License.

from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class FirefoxRemoteConnection(RemoteConnection):

browser_name = DesiredCapabilities.FIREFOX['browserName']

def __init__(self, remote_server_addr, keep_alive=True):
RemoteConnection.__init__(self, remote_server_addr, keep_alive)

Expand Down
1 change: 1 addition & 0 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class RemoteConnection(object):
Communicates with the server using the WebDriver wire protocol:
https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol"""

browser_name = None
_timeout = socket._GLOBAL_DEFAULT_TIMEOUT

@classmethod
Expand Down
16 changes: 15 additions & 1 deletion py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ def _make_w3c_caps(caps):
return {"firstMatch": [{}], "alwaysMatch": always_match}


def get_remote_connection(capabilities, command_executor, keep_alive):
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.safari.remote_connection import SafariRemoteConnection
from selenium.webdriver.firefox.remote_connection import FirefoxRemoteConnection

candidates = [RemoteConnection] + [ChromiumRemoteConnection, SafariRemoteConnection, FirefoxRemoteConnection]
handler = next(
(c for c in candidates if c.browser_name == capabilities.get('browserName')),
RemoteConnection
)

return handler(command_executor, keep_alive=keep_alive)


class WebDriver(object):
"""
Controls a browser by sending commands to a remote server.
Expand Down Expand Up @@ -143,7 +157,7 @@ def __init__(self, command_executor='http://127.0.0.1:4444',
capabilities.update(desired_capabilities)
self.command_executor = command_executor
if type(self.command_executor) is bytes or isinstance(self.command_executor, str):
self.command_executor = RemoteConnection(command_executor, keep_alive=keep_alive)
self.command_executor = get_remote_connection(capabilities, command_executor=command_executor, keep_alive=keep_alive)
self._is_remote = True
self.session_id = None
self.capabilities = {}
Expand Down
4 changes: 4 additions & 0 deletions py/selenium/webdriver/safari/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
# under the License.

from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class SafariRemoteConnection(RemoteConnection):

browser_name = DesiredCapabilities.SAFARI['browserName']

def __init__(self, remote_server_addr, keep_alive=True):
RemoteConnection.__init__(self, remote_server_addr, keep_alive)

Expand Down

0 comments on commit 3169619

Please sign in to comment.