From 956318ee6904bbe224a806389503617456979e05 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Tue, 26 Nov 2024 19:18:39 +0700 Subject: [PATCH] [py] Update get_property for attr selected, checked, index Signed-off-by: Viet Nguyen Duc --- .../webdriver/support/expected_conditions.py | 13 ++- py/selenium/webdriver/support/select.py | 5 +- .../common/element_attribute_tests.py | 86 +++++++++---------- 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/py/selenium/webdriver/support/expected_conditions.py b/py/selenium/webdriver/support/expected_conditions.py index a95c35fea5b7b..d085a2a68ff5d 100644 --- a/py/selenium/webdriver/support/expected_conditions.py +++ b/py/selenium/webdriver/support/expected_conditions.py @@ -43,6 +43,7 @@ T = TypeVar("T") WebDriverOrWebElement = Union[WebDriver, WebElement] +attr_get_property = ["value", "checked", "index", "selected"] def title_is(title: str) -> Callable[[WebDriver], bool]: @@ -278,7 +279,11 @@ def text_to_be_present_in_element_attribute( def _predicate(driver: WebDriverOrWebElement): try: - element_text = driver.find_element(*locator).get_dom_attribute(attribute_) + element_text = ( + driver.find_element(*locator).get_dom_attribute(attribute_) + if attribute_ not in attr_get_property + else driver.find_element(*locator).get_property(attribute_) + ) if element_text is None: return False return text_ in element_text @@ -483,7 +488,11 @@ def element_attribute_to_include(locator: Tuple[str, str], attribute_: str) -> C def _predicate(driver: WebDriverOrWebElement): try: - element_attribute = driver.find_element(*locator).get_dom_attribute(attribute_) + element_attribute = ( + driver.find_element(*locator).get_dom_attribute(attribute_) + if attribute_ not in attr_get_property + else driver.find_element(*locator).get_property(attribute_) + ) return element_attribute is not None except StaleElementReferenceException: return False diff --git a/py/selenium/webdriver/support/select.py b/py/selenium/webdriver/support/select.py index 1726ef4174948..0a736bcef054e 100644 --- a/py/selenium/webdriver/support/select.py +++ b/py/selenium/webdriver/support/select.py @@ -92,9 +92,8 @@ def select_by_index(self, index: int) -> None: throws NoSuchElementException If there is no option with specified index in SELECT """ - match = str(index) for opt in self.options: - if opt.get_dom_attribute("index") == match: + if opt.get_property("index") == index: self._set_selected(opt) return raise NoSuchElementException(f"Could not locate element with index {index}") @@ -182,7 +181,7 @@ def deselect_by_index(self, index: int) -> None: if not self.is_multiple: raise NotImplementedError("You may only deselect options of a multi-select") for opt in self.options: - if opt.get_dom_attribute("index") == str(index): + if opt.get_property("index") == index: self._unset_selected(opt) return raise NoSuchElementException(f"Could not locate element with index {index}") diff --git a/py/test/selenium/webdriver/common/element_attribute_tests.py b/py/test/selenium/webdriver/common/element_attribute_tests.py index a1717111c7b98..409e9ed869580 100644 --- a/py/test/selenium/webdriver/common/element_attribute_tests.py +++ b/py/test/selenium/webdriver/common/element_attribute_tests.py @@ -24,45 +24,45 @@ def test_should_return_null_when_getting_the_value_of_an_attribute_that_is_not_listed(driver, pages): pages.load("simpleTest.html") head = driver.find_element(By.XPATH, "/html") - attribute = head.get_attribute("cheese") + attribute = head.get_dom_attribute("cheese") assert attribute is None def test_should_return_null_when_getting_src_attribute_of_invalid_img_tag(driver, pages): pages.load("simpleTest.html") img = driver.find_element(By.ID, "invalidImgTag") - img_attr = img.get_attribute("src") + img_attr = img.get_dom_attribute("src") assert img_attr is None def test_should_return_an_absolute_url_when_getting_src_attribute_of_avalid_img_tag(driver, pages): pages.load("simpleTest.html") img = driver.find_element(By.ID, "validImgTag") - img_attr = img.get_attribute("src") + img_attr = img.get_dom_attribute("src") assert "icon.gif" in img_attr def test_should_return_an_absolute_url_when_getting_href_attribute_of_avalid_anchor_tag(driver, pages): pages.load("simpleTest.html") img = driver.find_element(By.ID, "validAnchorTag") - img_attr = img.get_attribute("href") + img_attr = img.get_dom_attribute("href") assert "icon.gif" in img_attr def test_should_return_empty_attribute_values_when_present_and_the_value_is_actually_empty(driver, pages): pages.load("simpleTest.html") body = driver.find_element(By.XPATH, "//body") - assert "" == body.get_attribute("style") + assert "" == body.get_dom_attribute("style") def test_should_return_the_value_of_the_disabled_attribute_as_false_if_not_set(driver, pages): pages.load("formPage.html") inputElement = driver.find_element(By.XPATH, "//input[@id='working']") - assert inputElement.get_attribute("disabled") is None + assert inputElement.get_dom_attribute("disabled") is None assert inputElement.is_enabled() pElement = driver.find_element(By.ID, "peas") - assert pElement.get_attribute("disabled") is None + assert pElement.get_dom_attribute("disabled") is None assert pElement.is_enabled() @@ -70,7 +70,7 @@ def test_should_return_the_value_of_the_index_attribute_even_if_it_is_missing(dr pages.load("formPage.html") multiSelect = driver.find_element(By.ID, "multi") options = multiSelect.find_elements(By.TAG_NAME, "option") - assert "1" == options[1].get_attribute("index") + assert 1 == options[1].get_property("index") def test_should_indicate_the_elements_that_are_disabled_are_not_is_enabled(driver, pages): @@ -126,9 +126,9 @@ def test_should_indicate_when_aselect_is_disabled(driver, pages): def test_should_return_the_value_of_checked_for_acheckbox_even_if_it_lacks_that_attribute(driver, pages): pages.load("formPage.html") checkbox = driver.find_element(By.XPATH, "//input[@id='checky']") - assert checkbox.get_attribute("checked") is None + assert checkbox.get_property("checked") is False checkbox.click() - assert "true" == checkbox.get_attribute("checked") + assert True is checkbox.get_property("checked") def test_should_return_the_value_of_selected_for_radio_buttons_even_if_they_lack_that_attribute(driver, pages): @@ -137,14 +137,14 @@ def test_should_return_the_value_of_selected_for_radio_buttons_even_if_they_lack initiallyNotSelected = driver.find_element(By.ID, "peas") initiallySelected = driver.find_element(By.ID, "cheese_and_peas") - assert neverSelected.get_attribute("checked") is None - assert initiallyNotSelected.get_attribute("checked") is None - assert "true" == initiallySelected.get_attribute("checked") + assert neverSelected.get_property("checked") is False + assert initiallyNotSelected.get_property("checked") is False + assert True is initiallySelected.get_property("checked") initiallyNotSelected.click() - assert neverSelected.get_attribute("selected") is None - assert "true" == initiallyNotSelected.get_attribute("checked") - assert initiallySelected.get_attribute("checked") is None + assert neverSelected.get_property("selected") is None + assert True is initiallyNotSelected.get_property("checked") + assert initiallySelected.get_property("checked") is False def test_should_return_the_value_of_selected_for_options_in_selects_even_if_they_lack_that_attribute(driver, pages): @@ -155,14 +155,14 @@ def test_should_return_the_value_of_selected_for_options_in_selects_even_if_they two = options[1] assert one.is_selected() assert not two.is_selected() - assert "true" == one.get_attribute("selected") - assert two.get_attribute("selected") is None + assert True is one.get_property("selected") + assert two.get_property("selected") is False def test_should_return_value_of_class_attribute_of_an_element(driver, pages): pages.load("xhtmlTest.html") heading = driver.find_element(By.XPATH, "//h1") - classname = heading.get_attribute("class") + classname = heading.get_dom_attribute("class") assert "header" == classname @@ -172,13 +172,13 @@ def test_should_return_value_of_class_attribute_of_an_element(driver, pages): # driver.switch_to.frame("iframe1") # # wallace = driver.find_element(By.XPATH, "//div[@id='wallace']") -# classname = wallace.get_attribute("class") +# classname = wallace.get_dom_attribute("class") # assert "gromit" == classname def test_should_return_the_contents_of_atext_area_as_its_value(driver, pages): pages.load("formPage.html") - value = driver.find_element(By.ID, "withText").get_attribute("value") + value = driver.find_element(By.ID, "withText").get_property("value") assert "Example text" == value @@ -186,17 +186,17 @@ def test_should_return_the_contents_of_atext_area_as_its_value_when_set_to_non_n pages.load("formPage.html") e = driver.find_element(By.ID, "withText") driver.execute_script("arguments[0].value = 'tRuE'", e) - value = e.get_attribute("value") + value = e.get_property("value") assert "tRuE" == value def test_should_treat_readonly_as_avalue(driver, pages): pages.load("formPage.html") element = driver.find_element(By.NAME, "readonly") - readOnlyAttribute = element.get_attribute("readonly") + readOnlyAttribute = element.get_dom_attribute("readonly") textInput = driver.find_element(By.NAME, "x") - notReadOnly = textInput.get_attribute("readonly") + notReadOnly = textInput.get_dom_attribute("readonly") assert readOnlyAttribute != notReadOnly @@ -204,12 +204,12 @@ def test_should_treat_readonly_as_avalue(driver, pages): def test_should_get_numeric_attribute(driver, pages): pages.load("formPage.html") element = driver.find_element(By.ID, "withText") - assert "5" == element.get_attribute("rows") + assert "5" == element.get_dom_attribute("rows") def test_can_return_atext_approximation_of_the_style_attribute(driver, pages): pages.load("javascriptPage.html") - style = driver.find_element(By.ID, "red-item").get_attribute("style") + style = driver.find_element(By.ID, "red-item").get_dom_attribute("style") assert "background-color" in style.lower() @@ -219,54 +219,54 @@ def test_should_correctly_report_value_of_colspan(driver, pages): th1 = driver.find_element(By.ID, "th1") td2 = driver.find_element(By.ID, "td2") - assert "th1" == th1.get_attribute("id") - assert "3" == th1.get_attribute("colspan") + assert "th1" == th1.get_dom_attribute("id") + assert "3" == th1.get_dom_attribute("colspan") - assert "td2" == td2.get_attribute("id") - assert "2" == td2.get_attribute("colspan") + assert "td2" == td2.get_dom_attribute("id") + assert "2" == td2.get_dom_attribute("colspan") def test_can_retrieve_the_current_value_of_atext_form_field_text_input(driver, pages): pages.load("formPage.html") element = driver.find_element(By.ID, "working") - assert "" == element.get_attribute("value") + assert "" == element.get_property("value") element.send_keys("hello world") - assert "hello world" == element.get_attribute("value") + assert "hello world" == element.get_property("value") def test_can_retrieve_the_current_value_of_atext_form_field_email_input(driver, pages): pages.load("formPage.html") element = driver.find_element(By.ID, "email") - assert "" == element.get_attribute("value") + assert "" == element.get_property("value") element.send_keys("hello@example.com") - assert "hello@example.com" == element.get_attribute("value") + assert "hello@example.com" == element.get_property("value") def test_can_retrieve_the_current_value_of_atext_form_field_text_area(driver, pages): pages.load("formPage.html") element = driver.find_element(By.ID, "emptyTextArea") - assert "" == element.get_attribute("value") + assert "" == element.get_property("value") element.send_keys("hello world") - assert "hello world" == element.get_attribute("value") + assert "hello world" == element.get_property("value") def test_should_return_null_for_non_present_boolean_attributes(driver, pages): pages.load("booleanAttributes.html") element1 = driver.find_element(By.ID, "working") - assert element1.get_attribute("required") is None + assert element1.get_dom_attribute("required") is None @pytest.mark.xfail_ie def test_should_return_true_for_present_boolean_attributes(driver, pages): pages.load("booleanAttributes.html") element1 = driver.find_element(By.ID, "emailRequired") - assert "true" == element1.get_attribute("required") + assert "true" == element1.get_dom_attribute("required") element2 = driver.find_element(By.ID, "emptyTextAreaRequired") - assert "true" == element2.get_attribute("required") + assert "true" == element2.get_dom_attribute("required") element3 = driver.find_element(By.ID, "inputRequired") - assert "true" == element3.get_attribute("required") + assert "true" == element3.get_dom_attribute("required") element4 = driver.find_element(By.ID, "textAreaRequired") - assert "true" == element4.get_attribute("required") + assert "true" == element4.get_dom_attribute("required") @pytest.mark.xfail_chrome @@ -276,7 +276,7 @@ def test_should_return_true_for_present_boolean_attributes(driver, pages): @pytest.mark.xfail_remote def test_should_get_unicode_chars_from_attribute(driver, pages): pages.load("formPage.html") - title = driver.find_element(By.ID, "vsearchGadget").get_attribute("title") + title = driver.find_element(By.ID, "vsearchGadget").get_dom_attribute("title") assert "Hvad s\xf8ger du?" == title @@ -288,5 +288,5 @@ def test_should_get_unicode_chars_from_attribute(driver, pages): def test_should_get_values_and_not_miss_items(driver, pages): pages.load("attributes.html") expected = "4b273a33fbbd29013nN93dy4F1A~" - result = driver.find_element(By.CSS_SELECTOR, "li").get_attribute("value") + result = driver.find_element(By.CSS_SELECTOR, "li").get_property("value") assert expected == result