Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip]: define AppiumClientConfig #1070

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ For example, some changes in the Selenium binding could break the Appium client.
> to keep compatible version combinations.


### Quick migration guide from v4 to v5
- Please use `AppiumClientConfig` as `client_config` arguemnt in favor of client arguments below
- `keep_alive`, `direct_connection` and `strict_ssl` arguments.
```python
SERVER_URL_BASE = 'http://127.0.0.1:4723'
# before
driver = webdriver.Remote(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(desired_caps),
direct_connection=True,
keep_alive=False,
strict_ssl=False
)

# after
from appium.webdriver.client_config import AppiumClientConfig
client_config = AppiumClientConfig(
remote_server_addr=SERVER_URL_BASE,
direct_connection=True,
keep_alive=False,
ignore_certificates=True,
)
driver = webdriver.Remote(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(desired_caps),
client_config=client_config
)
```

### Quick migration guide from v3 to v4
- Removal
- `MultiAction` and `TouchAction` are removed. Please use W3C WebDriver actions or `mobile:` extensions
Expand Down Expand Up @@ -273,6 +302,7 @@ from appium import webdriver
# If you use an older client then switch to desired_capabilities
# instead: https://github.com/appium/python-client/pull/720
from appium.options.ios import XCUITestOptions
from appium.webdriver.client_config import AppiumClientConfig

# load_capabilities API could be used to
# load options mapping stored in a dictionary
Expand All @@ -282,11 +312,16 @@ options = XCUITestOptions().load_capabilities({
'app': '/full/path/to/app/UICatalog.app.zip',
})

client_config = AppiumClientConfig(
remote_server_addr='http://127.0.0.1:4723',
direct_connection=True
)

driver = webdriver.Remote(
# Appium1 points to http://127.0.0.1:4723/wd/hub by default
'http://127.0.0.1:4723',
options=options,
direct_connection=True
client_config=client_config
)
```

Expand Down
28 changes: 28 additions & 0 deletions appium/webdriver/client_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium.webdriver.remote.client_config import ClientConfig


class AppiumClientConfig(ClientConfig):
"""ClientConfig class for Appium Python client."""

def __init__(self, remote_server_addr: str, *args, **kwargs):
"""
TODO: add description
"""
self._direct_connection = kwargs.pop('direct_connection', False)
super().__init__(remote_server_addr, *args, **kwargs)

@property
def direct_connection(self) -> bool:
return self._direct_connection
26 changes: 8 additions & 18 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
WebDriverException,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.command import Command as RemoteCommand
from selenium.webdriver.remote.remote_connection import RemoteConnection
from typing_extensions import Self
Expand All @@ -32,6 +31,7 @@
from appium.webdriver.common.appiumby import AppiumBy

from .appium_connection import AppiumConnection
from .client_config import AppiumClientConfig
from .errorhandler import MobileErrorHandler
from .extensions.action_helpers import ActionHelpers
from .extensions.android.activities import Activities
Expand Down Expand Up @@ -204,25 +204,15 @@ class WebDriver(
):
def __init__( # noqa: PLR0913
self,
command_executor: Union[str, AppiumConnection] = 'http://127.0.0.1:4444/wd/hub',
keep_alive: bool = True,
direct_connection: bool = True,
command_executor: Union[str, AppiumConnection] = 'http://127.0.0.1:4444',
extensions: Optional[List['WebDriver']] = None,
strict_ssl: bool = True,
options: Union[AppiumOptions, List[AppiumOptions], None] = None,
client_config: Optional[ClientConfig] = None,
client_config: Optional[AppiumClientConfig] = None,
):
if isinstance(command_executor, str):
client_config = client_config or ClientConfig(
remote_server_addr=command_executor, keep_alive=keep_alive, ignore_certificates=not strict_ssl
)
client_config.remote_server_addr = command_executor
command_executor = AppiumConnection(remote_server_addr=command_executor, client_config=client_config)
elif isinstance(command_executor, AppiumConnection) and strict_ssl is False:
logger.warning(
"Please set 'ignore_certificates' in the given 'appium.webdriver.appium_connection.AppiumConnection' or "
"'selenium.webdriver.remote.client_config.ClientConfig' instead. Ignoring."
)
if client_config is None:
client_config = AppiumClientConfig(remote_server_addr=command_executor)
command_executor = AppiumConnection(client_config=client_config)

super().__init__(
command_executor=command_executor,
Expand All @@ -237,8 +227,8 @@ def __init__( # noqa: PLR0913

self.error_handler = MobileErrorHandler()

if direct_connection:
self._update_command_executor(keep_alive=keep_alive)
if client_config and client_config.direct_connection:
self._update_command_executor(keep_alive=client_config.keep_alive)

# add new method to the `find_by_*` pantheon
By.IOS_PREDICATE = AppiumBy.IOS_PREDICATE
Expand Down
15 changes: 7 additions & 8 deletions test/unit/webdriver/webdriver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_connection import AppiumConnection
from appium.webdriver.client_config import AppiumClientConfig
from appium.webdriver.webdriver import ExtensionBase, WebDriver
from test.helpers.constants import SERVER_URL_BASE
from test.unit.helper.test_helper import (
Expand Down Expand Up @@ -124,10 +125,11 @@ def test_create_session_register_uridirect(self):
'app': 'path/to/app',
'automationName': 'UIAutomator2',
}
client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE, direct_connection=True)
driver = webdriver.Remote(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(desired_caps),
direct_connection=True,
client_config=client_config,
)

assert 'http://localhost2:4800/special/path/wd/hub' == driver.command_executor._client_config.remote_server_addr
Expand Down Expand Up @@ -164,10 +166,9 @@ def test_create_session_register_uridirect_no_direct_connect_path(self):
'app': 'path/to/app',
'automationName': 'UIAutomator2',
}
client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE, direct_connection=True)
driver = webdriver.Remote(
SERVER_URL_BASE,
options=UiAutomator2Options().load_capabilities(desired_caps),
direct_connection=True,
SERVER_URL_BASE, options=UiAutomator2Options().load_capabilities(desired_caps), client_config=client_config
)

assert SERVER_URL_BASE == driver.command_executor._client_config.remote_server_addr
Expand Down Expand Up @@ -382,19 +383,17 @@ def test_extention_command_check(self):


class SubWebDriver(WebDriver):
def __init__(self, command_executor, direct_connection=False, options=None):
def __init__(self, command_executor, options=None):
super().__init__(
command_executor=command_executor,
direct_connection=direct_connection,
options=options,
)


class SubSubWebDriver(SubWebDriver):
def __init__(self, command_executor, direct_connection=False, options=None):
def __init__(self, command_executor, options=None):
super().__init__(
command_executor=command_executor,
direct_connection=direct_connection,
options=options,
)

Expand Down
Loading