Skip to content

Commit 11c4a2c

Browse files
committed
tests for custom element test
1 parent 4503b1e commit 11c4a2c

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
import pytest
18+
19+
from selenium import webdriver
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 driver():
32+
options = webdriver.ChromeOptions()
33+
driver = webdriver.Chrome(options=options)
34+
yield driver
35+
driver.quit()
36+
37+
38+
def test_find_element_with_custom_class(driver, pages):
39+
"""Test to ensure custom element class is used for a single element."""
40+
driver._web_element_cls = MyCustomElement
41+
pages.load("simpleTest.html")
42+
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(driver, pages):
48+
"""Test to ensure custom element class is used for multiple elements."""
49+
driver._web_element_cls = MyCustomElement
50+
pages.load("simpleTest.html")
51+
elements = driver.find_elements(By.TAG_NAME, "div")
52+
assert all(isinstance(el, MyCustomElement) for el in elements)
53+
assert all(el.custom_method() == "Custom element method" for el in elements)
54+
55+
56+
def test_default_element_class(driver, pages):
57+
"""Test to ensure default WebElement class is used."""
58+
pages.load("simpleTest.html")
59+
element = driver.find_element(By.TAG_NAME, "body")
60+
assert isinstance(element, WebElement)
61+
assert not hasattr(element, "custom_method")

py/test/selenium/webdriver/remote/remote_custom_locator_tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ def convert(self, by, value):
3131

3232
@pytest.fixture
3333
def driver() -> WebDriver:
34-
# Setup for driver
3534
options = webdriver.ChromeOptions()
3635
driver = webdriver.Chrome(options=options)
3736
driver.locator_converter = CustomLocatorConverter()
38-
3937
driver.get("data:text/html,<div custom-attr='example'>Test</div>")
38+
4039
yield driver
41-
# Teardown after test
4240
driver.quit()
4341

4442

@@ -49,6 +47,8 @@ def test_find_element_with_custom_locator(driver: WebDriver) -> None:
4947

5048

5149
def test_find_elements_with_custom_locator(driver: WebDriver) -> None:
50+
driver.get("data:text/html,<div custom-attr='example'>Test1</div><div custom-attr='example'>Test2</div>")
5251
elements = driver.find_elements("custom", "example")
53-
assert len(elements) == 1
54-
assert elements[0].text == "Test"
52+
assert len(elements) == 2
53+
assert elements[0].text == "Test1"
54+
assert elements[1].text == "Test2"

0 commit comments

Comments
 (0)