|
23 | 23 | from selenium.webdriver.remote.remote_connection import RemoteConnection |
24 | 24 |
|
25 | 25 | from appium.common.logger import logger |
| 26 | +from appium.webdriver.command_method import CommandMethod |
26 | 27 | from appium.webdriver.common.mobileby import MobileBy |
27 | 28 |
|
28 | 29 | from .appium_connection import AppiumConnection |
@@ -356,6 +357,96 @@ def switch_to(self) -> MobileSwitchTo: |
356 | 357 |
|
357 | 358 | return MobileSwitchTo(self) |
358 | 359 |
|
| 360 | + def add_command(self, method: CommandMethod, url: str, name: str) -> T: |
| 361 | + """Add a custom command as 'name' |
| 362 | +
|
| 363 | + Args: |
| 364 | + method: The method of HTTP request. Available methods are CommandMethod. |
| 365 | + url: The url is URL template as https://www.w3.org/TR/webdriver/#endpoints. |
| 366 | + `$sessionId` is a placeholder of current session id. |
| 367 | + Other placeholders can be specified with `$` prefix like `$id`. |
| 368 | + Then, `{'id': 'some id'}` argument in `execute_custom_command` replaces |
| 369 | + the `$id` with the value, `some id`, in the request. |
| 370 | + name: The name of a command to call in `execute_custom_command`. |
| 371 | +
|
| 372 | + Returns: |
| 373 | + `appium.webdriver.webdriver.WebDriver`: Self instance |
| 374 | +
|
| 375 | + Raises: |
| 376 | + ValueError |
| 377 | +
|
| 378 | + Examples: |
| 379 | + Define 'test_command' as GET and 'session/$sessionId/path/to/custom/url' |
| 380 | +
|
| 381 | + >>> from appium.webdriver.command_method import CommandMethod |
| 382 | + >>> driver.add_command( |
| 383 | + method=CommandMethod.GET, |
| 384 | + url='session/$sessionId/path/to/custom/url', |
| 385 | + name='test_command' |
| 386 | + ) |
| 387 | +
|
| 388 | + """ |
| 389 | + if name in self.command_executor._commands: |
| 390 | + raise ValueError("{} is already defined".format(name)) |
| 391 | + |
| 392 | + if not isinstance(method, CommandMethod): |
| 393 | + raise ValueError( |
| 394 | + "'{}' is invalid. Valid method should be one of '{}'.".format(method, CommandMethod.__name__) |
| 395 | + ) |
| 396 | + |
| 397 | + self.command_executor._commands[name] = (method.value, url) |
| 398 | + return self |
| 399 | + |
| 400 | + def execute_custom_command(self, name: str, args: Dict = {}) -> Any: |
| 401 | + """Execute a custom command defined as 'name' with args command. |
| 402 | +
|
| 403 | + Args: |
| 404 | + name: The name of command defined by `add_command`. |
| 405 | + args: The argument as this command body |
| 406 | +
|
| 407 | + Returns: |
| 408 | + 'value' JSON object field in the response body. |
| 409 | +
|
| 410 | + Raises: |
| 411 | + ValueError, selenium.common.exceptions.WebDriverException |
| 412 | +
|
| 413 | + Examples: |
| 414 | + Calls 'test_command' command without arguments. |
| 415 | +
|
| 416 | + >>> from appium.webdriver.command_method import CommandMethod |
| 417 | + >>> driver.add_command( |
| 418 | + method=CommandMethod.GET, |
| 419 | + url='session/$sessionId/path/to/custom/url', |
| 420 | + name='test_command' |
| 421 | + ) |
| 422 | + >>> result = driver.execute_custom_command('test_command') |
| 423 | +
|
| 424 | + Calls 'test_command' command with arguments. |
| 425 | +
|
| 426 | + >>> from appium.webdriver.command_method import CommandMethod |
| 427 | + >>> driver.add_command( |
| 428 | + method=CommandMethod.POST, |
| 429 | + url='session/$sessionId/path/to/custom/url', |
| 430 | + name='test_command' |
| 431 | + ) |
| 432 | + >>> result = driver.execute_custom_command('test_command', {'dummy': 'test argument'}) |
| 433 | +
|
| 434 | + Calls 'test_command' command with arguments, and the path has 'element id'. |
| 435 | + The '$id' will be 'element id' by 'id' key in the given argument. |
| 436 | +
|
| 437 | + >>> from appium.webdriver.command_method import CommandMethod |
| 438 | + >>> driver.add_command( |
| 439 | + method=CommandMethod.POST, |
| 440 | + url='session/$sessionId/path/to/$id/url', |
| 441 | + name='test_command' |
| 442 | + ) |
| 443 | + >>> result = driver.execute_custom_command('test_command', {'id': 'element id', 'dummy': 'test argument'}) |
| 444 | +
|
| 445 | + """ |
| 446 | + if name not in self.command_executor._commands: |
| 447 | + raise ValueError("No {} custom command. Please add it by 'add_command'".format(name)) |
| 448 | + return self.execute(name, args)['value'] |
| 449 | + |
359 | 450 | # pylint: disable=protected-access |
360 | 451 |
|
361 | 452 | def _addCommands(self) -> None: |
|
0 commit comments