Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/remote/remote_custom_element_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.webdriver.common.by import By
21
from selenium.webdriver.remote.webelement import WebElement
22
23
24
# Custom element class
25
class MyCustomElement(WebElement):
26
def custom_method(self):
27
return "Custom element method"
28
29
30
@pytest.fixture()
31
def custom_element_driver(driver):
32
try:
33
driver._web_element_cls = MyCustomElement
34
yield driver
35
finally:
36
driver._web_element_cls = WebElement
37
38
39
def test_find_element_with_custom_class(custom_element_driver, pages):
40
"""Test to ensure custom element class is used for a single element."""
41
pages.load("simpleTest.html")
42
element = custom_element_driver.find_element(By.TAG_NAME, "body")
43
assert isinstance(element, MyCustomElement)
44
assert element.custom_method() == "Custom element method"
45
46
47
def test_find_elements_with_custom_class(custom_element_driver, pages):
48
"""Test to ensure custom element class is used for multiple elements."""
49
pages.load("simpleTest.html")
50
elements = custom_element_driver.find_elements(By.TAG_NAME, "div")
51
assert all(isinstance(el, MyCustomElement) for el in elements)
52
assert all(el.custom_method() == "Custom element method" for el in elements)
53
54
55
def test_default_element_class(driver, pages):
56
"""Test to ensure default WebElement class is used."""
57
pages.load("simpleTest.html")
58
element = driver.find_element(By.TAG_NAME, "body")
59
assert isinstance(element, WebElement)
60
assert not hasattr(element, "custom_method")
61
62