From cfcbf27e6fe64d75766e932332af349814b88754 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 02:06:32 -0700 Subject: [PATCH 01/11] add add_commmand in python --- appium/webdriver/webdriver.py | 11 ++++++++++- test/unit/webdriver/webdriver_test.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 9b17d43cb..9ef36977d 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -15,7 +15,7 @@ # pylint: disable=too-many-lines,too-many-public-methods,too-many-statements,no-self-use import copy -from typing import Any, Dict, List, Optional, TypeVar, Union +from typing import Any, Callable, Dict, List, Optional, TypeVar, Union from selenium.common.exceptions import InvalidArgumentException from selenium.webdriver.common.by import By @@ -356,6 +356,15 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) + def add_command(self, method: str, url: str, name: str) -> Callable: + """""" + self.command_executor._commands[name] = (method, url) + + def function() -> Any: + return self.execute(name, {}) + + return function + # pylint: disable=protected-access def _addCommands(self) -> None: diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index bcd71d974..2f3fd0ba1 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -250,6 +250,22 @@ def exceptionCallback(request, uri, headers): mock_warning.assert_called_once() assert events == {} + @httpretty.activate + def test_add_command(self): + driver = ios_w3c_driver() + httpretty.register_uri( + httpretty.GET, + appium_command('session/1234567890/path/to/custom/url'), + body=json.dumps({'value': {}}), + ) + driver.test_command = driver.add_command( + method='GET', url='session/$sessionId/path/to/custom/url', name='test_command' + ) + + result = driver.test_command() + + assert result == {'value': {}} + class SubWebDriver(WebDriver): def __init__(self, command_executor, desired_capabilities, direct_connection=False): From 1ce459301077149bdcbecf9e4b49322ab8fffc87 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 02:21:42 -0700 Subject: [PATCH 02/11] add test --- appium/webdriver/webdriver.py | 8 ++------ test/unit/webdriver/webdriver_test.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 9ef36977d..c99a3410b 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -356,14 +356,10 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) - def add_command(self, method: str, url: str, name: str) -> Callable: + def registar_command(self, method: str, url: str, name: str) -> Callable: """""" self.command_executor._commands[name] = (method, url) - - def function() -> Any: - return self.execute(name, {}) - - return function + return self # pylint: disable=protected-access diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 2f3fd0ba1..29410d65f 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -20,7 +20,7 @@ from appium import version as appium_version from appium import webdriver from appium.webdriver.webdriver import WebDriver -from test.unit.helper.test_helper import android_w3c_driver, appium_command, ios_w3c_driver +from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body, ios_w3c_driver class TestWebDriverWebDriver(object): @@ -258,14 +258,26 @@ def test_add_command(self): appium_command('session/1234567890/path/to/custom/url'), body=json.dumps({'value': {}}), ) - driver.test_command = driver.add_command( - method='GET', url='session/$sessionId/path/to/custom/url', name='test_command' - ) + driver.registar_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + result = driver.execute('test_command', {}) - result = driver.test_command() + assert result == {'value': {}} + @httpretty.activate + def test_add_command_body(self): + driver = ios_w3c_driver() + httpretty.register_uri( + httpretty.POST, + appium_command('session/1234567890/path/to/custom/url'), + body=json.dumps({'value': {}}), + ) + driver.registar_command(method='POST', url='session/$sessionId/path/to/custom/url', name='test_command') + result = driver.execute('test_command', {'dummy': 'test argument'}) assert result == {'value': {}} + d = get_httpretty_request_body(httpretty.last_request()) + assert d['dummy'] == 'test argument' + class SubWebDriver(WebDriver): def __init__(self, command_executor, desired_capabilities, direct_connection=False): From 810a4ac68fc0568c885cbd595c2dffe19fb9082b Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 11:29:05 -0700 Subject: [PATCH 03/11] add exceptions, tweak method --- appium/webdriver/webdriver.py | 10 ++++++++-- test/unit/webdriver/webdriver_test.py | 28 +++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index c99a3410b..d32206f34 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -356,11 +356,17 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) - def registar_command(self, method: str, url: str, name: str) -> Callable: - """""" + def add_command(self, method: str, url: str, name: str) -> Callable: + if name in self.command_executor._commands: + raise ValueError("{} is already defined".format(name)) self.command_executor._commands[name] = (method, url) return self + def execute_custom_command(self, name: str, args: Dict) -> Any: + if name not in self.command_executor._commands: + raise ValueError("No {} custom command. Please add it with 'add_command'".format(name)) + return self.execute(name, args)['value'] + # pylint: disable=protected-access def _addCommands(self) -> None: diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 29410d65f..ab4599fc0 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -258,7 +258,7 @@ def test_add_command(self): appium_command('session/1234567890/path/to/custom/url'), body=json.dumps({'value': {}}), ) - driver.registar_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') result = driver.execute('test_command', {}) assert result == {'value': {}} @@ -271,13 +271,33 @@ def test_add_command_body(self): appium_command('session/1234567890/path/to/custom/url'), body=json.dumps({'value': {}}), ) - driver.registar_command(method='POST', url='session/$sessionId/path/to/custom/url', name='test_command') - result = driver.execute('test_command', {'dummy': 'test argument'}) - assert result == {'value': {}} + driver.add_command(method='POST', url='session/$sessionId/path/to/custom/url', name='test_command') + result = driver.execute_custom_command('test_command', {'dummy': 'test argument'}) + assert result == {} d = get_httpretty_request_body(httpretty.last_request()) assert d['dummy'] == 'test argument' + @httpretty.activate + def test_add_command_already_defined(self): + driver = ios_w3c_driver() + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + try: + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + assert False, 'Should raise InvalidArgumentException' + except ValueError: + assert True + + @httpretty.activate + def test_execute_custom_command(self): + driver = ios_w3c_driver() + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + try: + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + assert False, 'Should raise InvalidArgumentException' + except ValueError: + assert True + class SubWebDriver(WebDriver): def __init__(self, command_executor, desired_capabilities, direct_connection=False): From 02e4c84f79fb13961f88eaaa2e3dbed39bf05897 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 11:50:25 -0700 Subject: [PATCH 04/11] append docstring --- appium/webdriver/webdriver.py | 34 +++++++++++++++++++++++++-- test/unit/webdriver/webdriver_test.py | 17 ++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index d32206f34..3b38f9f08 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -58,6 +58,8 @@ from .switch_to import MobileSwitchTo from .webelement import WebElement as MobileWebElement +AVAILABLE_METHOD = frozenset(['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']) + # From remote/webdriver.py _W3C_CAPABILITY_NAMES = frozenset( [ @@ -357,14 +359,42 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) def add_command(self, method: str, url: str, name: str) -> Callable: + """Add a custom command as 'name' + + Args: + method: The method of HTTP request. Available methods are AVAILABLE_METHOD. + url: The url to URL template as https://www.w3.org/TR/webdriver/#endpoints. + '$sessionId' is a placeholder of current session id. + '$id' is a placeholder of an element. + name: The name of command to call in `execute_custom_command`. + + Returns: + `appium.webdriver.webdriver.WebDriver`: Self instance + + """ if name in self.command_executor._commands: raise ValueError("{} is already defined".format(name)) + + method = method.upper() + if method not in AVAILABLE_METHOD: + raise ValueError("'{}' is invalid. Valid method is {}.".format(method, AVAILABLE_METHOD)) + self.command_executor._commands[name] = (method, url) return self - def execute_custom_command(self, name: str, args: Dict) -> Any: + def execute_custom_command(self, name: str, args: Dict = {}) -> Any: + """Execute a custom command defined as 'name' with args command. + + Args: + name: The name of command defined by `add_command`. + args: The argument as this command body + + Returns: + 'value' JSON object field in the response body. + + """ if name not in self.command_executor._commands: - raise ValueError("No {} custom command. Please add it with 'add_command'".format(name)) + raise ValueError("No {} custom command. Please add it by 'add_command'".format(name)) return self.execute(name, args)['value'] # pylint: disable=protected-access diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index ab4599fc0..f37fce267 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -259,9 +259,9 @@ def test_add_command(self): body=json.dumps({'value': {}}), ) driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - result = driver.execute('test_command', {}) + result = driver.execute_custom_command('test_command') - assert result == {'value': {}} + assert result == {} @httpretty.activate def test_add_command_body(self): @@ -284,7 +284,7 @@ def test_add_command_already_defined(self): driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') try: driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - assert False, 'Should raise InvalidArgumentException' + assert False, 'Should raise ValueError' except ValueError: assert True @@ -294,7 +294,16 @@ def test_execute_custom_command(self): driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') try: driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - assert False, 'Should raise InvalidArgumentException' + assert False, 'Should raise ValueError' + except ValueError: + assert True + + @httpretty.activate + def test_invalid_method(self): + driver = ios_w3c_driver() + try: + driver.add_command(method='error', url='session/$sessionId/path/to/custom/url', name='test_command') + assert False, 'Should raise ValueError' except ValueError: assert True From 72f553deae1c9360b35970fa405fbe7710ee3443 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 12:33:57 -0700 Subject: [PATCH 05/11] add $id example --- appium/webdriver/webdriver.py | 10 ++++++---- test/unit/webdriver/webdriver_test.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 3b38f9f08..451d5218a 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -363,10 +363,12 @@ def add_command(self, method: str, url: str, name: str) -> Callable: Args: method: The method of HTTP request. Available methods are AVAILABLE_METHOD. - url: The url to URL template as https://www.w3.org/TR/webdriver/#endpoints. - '$sessionId' is a placeholder of current session id. - '$id' is a placeholder of an element. - name: The name of command to call in `execute_custom_command`. + url: The url is URL template as https://www.w3.org/TR/webdriver/#endpoints. + `$sessionId` is a placeholder of current session id. + Other placeholders can be specified with `$` prefix like `$id`. + Then, `{'id': 'some id'}` argument in `execute_custom_command` replaces + the `$id` with the value, `some id`, in the request. + name: The name of a command to call in `execute_custom_command`. Returns: `appium.webdriver.webdriver.WebDriver`: Self instance diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index f37fce267..daef95f8f 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -278,6 +278,18 @@ def test_add_command_body(self): d = get_httpretty_request_body(httpretty.last_request()) assert d['dummy'] == 'test argument' + @httpretty.activate + def test_add_command_with_element_id(self): + driver = ios_w3c_driver() + httpretty.register_uri( + httpretty.GET, + appium_command('session/1234567890/path/to/custom/element_id/url'), + body=json.dumps({'value': {}}), + ) + driver.add_command(method='GET', url='session/$sessionId/path/to/custom/$id/url', name='test_command') + result = driver.execute_custom_command('test_command', {'id': 'element_id'}) + assert result == {} + @httpretty.activate def test_add_command_already_defined(self): driver = ios_w3c_driver() From c61ad83b002e8e8e2c2342eeeecf3716dc47446f Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 17:41:40 -0700 Subject: [PATCH 06/11] use pytest.raises --- appium/webdriver/webdriver.py | 4 ++-- test/unit/webdriver/webdriver_test.py | 16 ++++------------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 451d5218a..fec69ff0e 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -15,7 +15,7 @@ # pylint: disable=too-many-lines,too-many-public-methods,too-many-statements,no-self-use import copy -from typing import Any, Callable, Dict, List, Optional, TypeVar, Union +from typing import Any, Dict, List, Optional, TypeVar, Union from selenium.common.exceptions import InvalidArgumentException from selenium.webdriver.common.by import By @@ -358,7 +358,7 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) - def add_command(self, method: str, url: str, name: str) -> Callable: + def add_command(self, method: str, url: str, name: str) -> T: """Add a custom command as 'name' Args: diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index daef95f8f..7489cb81f 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -15,6 +15,7 @@ import json import httpretty +import pytest from mock import patch from appium import version as appium_version @@ -294,30 +295,21 @@ def test_add_command_with_element_id(self): def test_add_command_already_defined(self): driver = ios_w3c_driver() driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - try: + with pytest.raises(ValueError): driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - assert False, 'Should raise ValueError' - except ValueError: - assert True @httpretty.activate def test_execute_custom_command(self): driver = ios_w3c_driver() driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - try: + with pytest.raises(ValueError): driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') - assert False, 'Should raise ValueError' - except ValueError: - assert True @httpretty.activate def test_invalid_method(self): driver = ios_w3c_driver() - try: + with pytest.raises(ValueError): driver.add_command(method='error', url='session/$sessionId/path/to/custom/url', name='test_command') - assert False, 'Should raise ValueError' - except ValueError: - assert True class SubWebDriver(WebDriver): From 08f2bf1cd17124cea4c48a28d5c21ffb45066354 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 18:32:44 -0700 Subject: [PATCH 07/11] add examples as docstring --- appium/webdriver/webdriver.py | 39 +++++++++++++++++++++------ test/unit/webdriver/webdriver_test.py | 21 ++++++++++----- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index fec69ff0e..6b3d89efa 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -23,6 +23,7 @@ from selenium.webdriver.remote.remote_connection import RemoteConnection from appium.common.logger import logger +from appium.webdriver.command_method import CommandMethod from appium.webdriver.common.mobileby import MobileBy from .appium_connection import AppiumConnection @@ -58,8 +59,6 @@ from .switch_to import MobileSwitchTo from .webelement import WebElement as MobileWebElement -AVAILABLE_METHOD = frozenset(['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']) - # From remote/webdriver.py _W3C_CAPABILITY_NAMES = frozenset( [ @@ -358,11 +357,11 @@ def switch_to(self) -> MobileSwitchTo: return MobileSwitchTo(self) - def add_command(self, method: str, url: str, name: str) -> T: + def add_command(self, method: CommandMethod, url: str, name: str) -> T: """Add a custom command as 'name' Args: - method: The method of HTTP request. Available methods are AVAILABLE_METHOD. + method: The method of HTTP request. Available methods are CommandMethod. url: The url is URL template as https://www.w3.org/TR/webdriver/#endpoints. `$sessionId` is a placeholder of current session id. Other placeholders can be specified with `$` prefix like `$id`. @@ -373,15 +372,27 @@ def add_command(self, method: str, url: str, name: str) -> T: Returns: `appium.webdriver.webdriver.WebDriver`: Self instance + Raises: + ValueError + + Examples: + Define 'test_command' as GET and 'session/$sessionId/path/to/custom/url' + + >>> from appium.webdriver.command_method import CommandMethod + >>> driver.add_command( + method=CommandMethod.GET, + url='session/$sessionId/path/to/custom/url', + name='test_command' + ) + """ if name in self.command_executor._commands: raise ValueError("{} is already defined".format(name)) - method = method.upper() - if method not in AVAILABLE_METHOD: - raise ValueError("'{}' is invalid. Valid method is {}.".format(method, AVAILABLE_METHOD)) + if not isinstance(method, CommandMethod): + raise ValueError("'{}' is invalid. Valid method is 'CommandMethod'.".format(method)) - self.command_executor._commands[name] = (method, url) + self.command_executor._commands[name] = (method.value, url) return self def execute_custom_command(self, name: str, args: Dict = {}) -> Any: @@ -394,6 +405,18 @@ def execute_custom_command(self, name: str, args: Dict = {}) -> Any: Returns: 'value' JSON object field in the response body. + Raises: + ValueError, selenium.common.exceptions.WebDriverException + + Examples: + Calls 'test_command' command without arguments. + + >>> result = driver.execute_custom_command('test_command') + + Calls 'test_command' command with arguments. + + >>> result = driver.execute_custom_command('test_command', {'dummy': 'test argument'}) + """ if name not in self.command_executor._commands: raise ValueError("No {} custom command. Please add it by 'add_command'".format(name)) diff --git a/test/unit/webdriver/webdriver_test.py b/test/unit/webdriver/webdriver_test.py index 7489cb81f..df7d5e88f 100644 --- a/test/unit/webdriver/webdriver_test.py +++ b/test/unit/webdriver/webdriver_test.py @@ -20,6 +20,7 @@ from appium import version as appium_version from appium import webdriver +from appium.webdriver.command_method import CommandMethod from appium.webdriver.webdriver import WebDriver from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body, ios_w3c_driver @@ -259,7 +260,7 @@ def test_add_command(self): appium_command('session/1234567890/path/to/custom/url'), body=json.dumps({'value': {}}), ) - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command(method=CommandMethod.GET, url='session/$sessionId/path/to/custom/url', name='test_command') result = driver.execute_custom_command('test_command') assert result == {} @@ -272,7 +273,7 @@ def test_add_command_body(self): appium_command('session/1234567890/path/to/custom/url'), body=json.dumps({'value': {}}), ) - driver.add_command(method='POST', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command(method=CommandMethod.POST, url='session/$sessionId/path/to/custom/url', name='test_command') result = driver.execute_custom_command('test_command', {'dummy': 'test argument'}) assert result == {} @@ -287,23 +288,29 @@ def test_add_command_with_element_id(self): appium_command('session/1234567890/path/to/custom/element_id/url'), body=json.dumps({'value': {}}), ) - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/$id/url', name='test_command') + driver.add_command( + method=CommandMethod.GET, url='session/$sessionId/path/to/custom/$id/url', name='test_command' + ) result = driver.execute_custom_command('test_command', {'id': 'element_id'}) assert result == {} @httpretty.activate def test_add_command_already_defined(self): driver = ios_w3c_driver() - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command(method=CommandMethod.GET, url='session/$sessionId/path/to/custom/url', name='test_command') with pytest.raises(ValueError): - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command( + method=CommandMethod.GET, url='session/$sessionId/path/to/custom/url', name='test_command' + ) @httpretty.activate def test_execute_custom_command(self): driver = ios_w3c_driver() - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command(method=CommandMethod.GET, url='session/$sessionId/path/to/custom/url', name='test_command') with pytest.raises(ValueError): - driver.add_command(method='GET', url='session/$sessionId/path/to/custom/url', name='test_command') + driver.add_command( + method=CommandMethod.GET, url='session/$sessionId/path/to/custom/url', name='test_command' + ) @httpretty.activate def test_invalid_method(self): From 7d26e8592f8db662b4a9f4f11e85c03b1f64264a Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 18:33:14 -0700 Subject: [PATCH 08/11] add command method --- appium/webdriver/command_method.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 appium/webdriver/command_method.py diff --git a/appium/webdriver/command_method.py b/appium/webdriver/command_method.py new file mode 100644 index 000000000..dde45cb44 --- /dev/null +++ b/appium/webdriver/command_method.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +# 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. + +import enum + + +class CommandMethod(enum.Enum): + GET = 'GET' + HEAD = 'HEAD' + POST = 'POST' + PUT = 'PUT' + DELETE = 'DELETE' + CONNECT = 'CONNECT' + OPTIONS = 'OPTIONS' + TRACE = 'TRACE' + PATCH = 'PATCH' From e5533eaaf4f6a2a41bd2bebc52dd97036a09a82b Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 18:35:42 -0700 Subject: [PATCH 09/11] tweak message --- appium/webdriver/webdriver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 6b3d89efa..f218ca289 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -390,7 +390,7 @@ def add_command(self, method: CommandMethod, url: str, name: str) -> T: raise ValueError("{} is already defined".format(name)) if not isinstance(method, CommandMethod): - raise ValueError("'{}' is invalid. Valid method is 'CommandMethod'.".format(method)) + raise ValueError("'{}' is invalid. Valid method is in '{}'.".format(method, CommandMethod.__name__)) self.command_executor._commands[name] = (method.value, url) return self From 9a7fe77329a2c9232c5a635f62c4a4ffe1d9296c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 5 Jun 2021 21:05:24 -0700 Subject: [PATCH 10/11] add example more --- appium/webdriver/webdriver.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index f218ca289..b98f42f49 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -411,12 +411,35 @@ def execute_custom_command(self, name: str, args: Dict = {}) -> Any: Examples: Calls 'test_command' command without arguments. + >>> from appium.webdriver.command_method import CommandMethod + >>> driver.add_command( + method=CommandMethod.GET, + url='session/$sessionId/path/to/custom/url', + name='test_command' + ) >>> result = driver.execute_custom_command('test_command') Calls 'test_command' command with arguments. + >>> from appium.webdriver.command_method import CommandMethod + >>> driver.add_command( + method=CommandMethod.POST, + url='session/$sessionId/path/to/custom/url', + name='test_command' + ) >>> result = driver.execute_custom_command('test_command', {'dummy': 'test argument'}) + Calls 'test_command' command with arguments, and the path has 'element id'. + The '$id' will be 'element id' by 'id' key in the given argument. + + >>> from appium.webdriver.command_method import CommandMethod + >>> driver.add_command( + method=CommandMethod.POST, + url='session/$sessionId/path/to/$id/url', + name='test_command' + ) + >>> result = driver.execute_custom_command('test_command', {'id': 'element id', 'dummy': 'test argument'}) + """ if name not in self.command_executor._commands: raise ValueError("No {} custom command. Please add it by 'add_command'".format(name)) From 309451384fc51414dbea9345097f788eff08dc13 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 6 Jun 2021 11:25:32 -0700 Subject: [PATCH 11/11] tweak message --- appium/webdriver/webdriver.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index b98f42f49..393684841 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -390,7 +390,9 @@ def add_command(self, method: CommandMethod, url: str, name: str) -> T: raise ValueError("{} is already defined".format(name)) if not isinstance(method, CommandMethod): - raise ValueError("'{}' is invalid. Valid method is in '{}'.".format(method, CommandMethod.__name__)) + raise ValueError( + "'{}' is invalid. Valid method should be one of '{}'.".format(method, CommandMethod.__name__) + ) self.command_executor._commands[name] = (method.value, url) return self