-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[🚀 Feature] [py]: Better compatibility with Appium-python (#14587)
* [py] override default locator converter for python * Support registering custom finders in py * Support registering extra HTTP commands and methods in python * Support overriding User-Agent in python * Support registering extra headers * [py] Support ignore certificates * Support using custom element classes * tests for custom element test * address review comments * close parenthesis Co-authored-by: Kazuaki Matsuo <[email protected]> * pass `init_args_for_pool_manager` in constructor * use existing driver fixture in tests * convert `add_command` to instance method --------- Co-authored-by: Sri Harsha <[email protected]> Co-authored-by: Kazuaki Matsuo <[email protected]>
- Loading branch information
1 parent
6ea5651
commit 1345193
Showing
9 changed files
with
280 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you 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. | ||
|
||
|
||
class LocatorConverter: | ||
def convert(self, by, value): | ||
# Default conversion logic | ||
if by == "id": | ||
return "css selector", f'[id="{value}"]' | ||
elif by == "class name": | ||
return "css selector", f".{value}" | ||
elif by == "name": | ||
return "css selector", f'[name="{value}"]' | ||
return by, value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you 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.common.by import By | ||
from selenium.webdriver.remote.webelement import WebElement | ||
|
||
|
||
# Custom element class | ||
class MyCustomElement(WebElement): | ||
def custom_method(self): | ||
return "Custom element method" | ||
|
||
|
||
def test_find_element_with_custom_class(driver, pages): | ||
"""Test to ensure custom element class is used for a single element.""" | ||
driver._web_element_cls = MyCustomElement | ||
pages.load("simpleTest.html") | ||
element = driver.find_element(By.TAG_NAME, "body") | ||
assert isinstance(element, MyCustomElement) | ||
assert element.custom_method() == "Custom element method" | ||
|
||
|
||
def test_find_elements_with_custom_class(driver, pages): | ||
"""Test to ensure custom element class is used for multiple elements.""" | ||
driver._web_element_cls = MyCustomElement | ||
pages.load("simpleTest.html") | ||
elements = driver.find_elements(By.TAG_NAME, "div") | ||
assert all(isinstance(el, MyCustomElement) for el in elements) | ||
assert all(el.custom_method() == "Custom element method" for el in elements) | ||
|
||
|
||
def test_default_element_class(driver, pages): | ||
"""Test to ensure default WebElement class is used.""" | ||
pages.load("simpleTest.html") | ||
element = driver.find_element(By.TAG_NAME, "body") | ||
assert isinstance(element, WebElement) | ||
assert not hasattr(element, "custom_method") |
40 changes: 40 additions & 0 deletions
40
py/test/selenium/webdriver/remote/remote_custom_locator_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you 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.locator_converter import LocatorConverter | ||
|
||
|
||
class CustomLocatorConverter(LocatorConverter): | ||
def convert(self, by, value): | ||
# Custom conversion logic | ||
if by == "custom": | ||
return "css selector", f'[custom-attr="{value}"]' | ||
return super().convert(by, value) | ||
|
||
|
||
def test_find_element_with_custom_locator(driver): | ||
driver.get("data:text/html,<div custom-attr='example'>Test</div>") | ||
element = driver.find_element("custom", "example") | ||
assert element is not None | ||
assert element.text == "Test" | ||
|
||
|
||
def test_find_elements_with_custom_locator(driver): | ||
driver.get("data:text/html,<div custom-attr='example'>Test1</div><div custom-attr='example'>Test2</div>") | ||
elements = driver.find_elements("custom", "example") | ||
assert len(elements) == 2 | ||
assert elements[0].text == "Test1" | ||
assert elements[1].text == "Test2" |
Oops, something went wrong.