|
| 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") |
0 commit comments