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 13 commits into
base: master
Choose a base branch
from
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo argument

- `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(
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 @@ -274,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 @@ -283,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
38 changes: 38 additions & 0 deletions appium/webdriver/client_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.
This class inherits selenium.webdriver.remote.client_config.ClientConfig.
"""

def __init__(self, remote_server_addr: str, *args, **kwargs):
"""
Please refer to selenium.webdriver.remote.client_config.ClientConfig documentation
about available arguments. Only 'direct_connection' below is AppiumClientConfig
specific argument.

Args:
direct_connection: If enables [directConnect](https://github.com/appium/python-client?tab=readme-ov-file#direct-connect-urls)
feature.
"""
self._direct_connection = kwargs.pop('direct_connection', False)
super().__init__(remote_server_addr, *args, **kwargs)

@property
def direct_connection(self) -> bool:
"""Return if [directConnect](https://github.com/appium/python-client?tab=readme-ov-file#direct-connect-urls)
is enabled."""
return self._direct_connection
28 changes: 11 additions & 17 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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.file_detector import FileDetector
from selenium.webdriver.remote.remote_connection import RemoteConnection
from typing_extensions import Self

Expand All @@ -32,6 +32,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,28 +205,21 @@ class WebDriver(
):
def __init__( # noqa: PLR0913
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it still complain about too many arguments after this change?

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:4723',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also a breaking change

extensions: Optional[List['WebDriver']] = None,
strict_ssl: bool = True,
file_detector: Optional[FileDetector] = None,
options: Union[AppiumOptions, List[AppiumOptions], None] = None,
client_config: Optional[ClientConfig] = None,
client_config: Optional[AppiumClientConfig] = None,
):
if isinstance(command_executor, str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to extract this logic into a separate method, so super class init would be the very first call there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super class init would be the very first call there?

Actually we could by modifying here like below:

    def __init__(
        self,
        command_executor: AppiumConnection,
        client_config: AppiumClientConfig,
        extensions: Optional[List['WebDriver']] = None,
        file_detector: Optional[FileDetector] = None,
        options: Union[AppiumOptions, List[AppiumOptions], None] = None,
    ):

Then, a new method will help to build client_config and command_executor.

To avoid handling string of command_executor in selenium, which uses ClientConfig, I have added these lines to keep using AppiumClientConfig as appium python client for such a string case (to keep providing similar syntax to webdriver.Remote for selenium and appium)

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
if client_config is None:
client_config = AppiumClientConfig(remote_server_addr=command_executor)
# To prevent generating RemoteConnection in selenium
command_executor = AppiumConnection(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."
)

super().__init__(
command_executor=command_executor,
file_detector=file_detector,
options=options,
locator_converter=AppiumLocatorConverter(),
web_element_cls=MobileWebElement,
Expand All @@ -237,8 +231,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
54 changes: 46 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,16 +166,54 @@ 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
assert ['NATIVE_APP', 'CHROMIUM'] == driver.contexts
assert isinstance(driver.command_executor, AppiumConnection)

@httpretty.activate
def test_create_session_remote_server_addr_treatment_with_appiumclientconfig(self):
# remote server add in AppiumRemoteCong will be prior than the string of 'command_executor'
# as same as Selenium behavior.
httpretty.register_uri(
httpretty.POST,
f'{SERVER_URL_BASE}/session',
body=json.dumps(
{
'sessionId': 'session-id',
'capabilities': {
'deviceName': 'Android Emulator',
},
}
),
)

httpretty.register_uri(
httpretty.GET,
f'{SERVER_URL_BASE}/session/session-id/contexts',
body=json.dumps({'value': ['NATIVE_APP', 'CHROMIUM']}),
)

desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',
'app': 'path/to/app',
'automationName': 'UIAutomator2',
}
client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE, direct_connection=True)
driver = webdriver.Remote(
'http://localhost:8080/something/path',
options=UiAutomator2Options().load_capabilities(desired_caps),
client_config=client_config,
)

assert SERVER_URL_BASE == driver.command_executor._client_config.remote_server_addr
assert isinstance(driver.command_executor, AppiumConnection)

@httpretty.activate
def test_get_events(self):
driver = ios_w3c_driver()
Expand Down Expand Up @@ -382,19 +422,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