Skip to content
Merged
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
13 changes: 11 additions & 2 deletions appium/protocols/webdriver/can_find_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from typing import TYPE_CHECKING, Protocol, runtime_checkable

if TYPE_CHECKING:
from selenium.webdriver.common.by import By

from appium.webdriver.webelement import WebElement


Expand All @@ -27,6 +29,13 @@ class CanFindElements(Protocol):
- find_elements(by, value): Find multiple elements
"""

def find_element(self, by: str, value: str | dict | None = None) -> 'WebElement': ...
if TYPE_CHECKING:

def find_element(self, by: str = By.ID, value: str | dict | None = None) -> 'WebElement': ...

def find_elements(self, by: str = By.ID, value: str | dict | None = None) -> list['WebElement']: ...
else:

def find_element(self, by: str = 'id', value: str | dict | None = None) -> 'WebElement': ...

def find_elements(self, by: str, value: str | dict | None = None) -> list['WebElement']: ...
def find_elements(self, by: str = 'id', value: str | dict | None = None) -> list['WebElement']: ...
7 changes: 5 additions & 2 deletions appium/webdriver/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@

from .mobilecommand import MobileCommand as Command

if TYPE_CHECKING:
from selenium.webdriver.common.by import By


class WebElement(SeleniumWebElement):
_execute: Callable
_id: str

if TYPE_CHECKING:

def find_element(self, by: str, value: str | dict | None = None) -> Self: # type: ignore[override]
def find_element(self, by: str = By.ID, value: str | dict | None = None) -> Self: # type: ignore[override]
...

def find_elements(self, by: str, value: str | dict | None = None) -> list[Self]: # type: ignore[override]
def find_elements(self, by: str = By.ID, value: str | dict | None = None) -> list[Self]: # type: ignore[override]
...

def get_attribute(self, name: str) -> str | dict | None: # type: ignore[override]
Expand Down
48 changes: 48 additions & 0 deletions test/unit/webdriver/webelement_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import httpretty

from appium.protocols.webdriver.can_find_elements import CanFindElements
from appium.webdriver.webelement import WebElement as MobileWebElement
from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body

Expand Down Expand Up @@ -101,3 +102,50 @@ def test_element_location_in_view(self):
httpretty.last_request()

assert loc == location_in_view

@httpretty.activate
def test_find_element_default_by(self):
driver = android_w3c_driver()
element = MobileWebElement(driver, 'element_id')
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/element/element_id/element'),
body='{"value": {"element-6066-11e4-a52e-4f735466cecf": "child-element-id"}}',
)

el = element.find_element(value='child_id')

d = get_httpretty_request_body(httpretty.last_request())
assert d['using'] == 'id'
assert d['value'] == 'child_id'
assert isinstance(el, MobileWebElement)
assert el.id == 'child-element-id'

@httpretty.activate
def test_find_elements_default_by(self):
driver = android_w3c_driver()
element = MobileWebElement(driver, 'element_id')
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/element/element_id/elements'),
body='{"value": [{"element-6066-11e4-a52e-4f735466cecf": "child-1"}, {"element-6066-11e4-a52e-4f735466cecf": "child-2"}]}',
)

els = element.find_elements(value='child_ids')

d = get_httpretty_request_body(httpretty.last_request())
assert d['using'] == 'id'
assert d['value'] == 'child_ids'
assert len(els) == 2
assert isinstance(els[0], MobileWebElement)
assert els[0].id == 'child-1'
assert isinstance(els[1], MobileWebElement)
assert els[1].id == 'child-2'

@httpretty.activate
def test_can_find_elements_protocol_conformance(self):
driver = android_w3c_driver()
element = MobileWebElement(driver, 'element_id')

assert isinstance(element, CanFindElements)
assert isinstance(driver, CanFindElements)
Loading