Skip to content

Commit

Permalink
[py] Update get_property for attr selected, checked, index
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Nov 26, 2024
1 parent 8764b11 commit aca6f88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
5 changes: 2 additions & 3 deletions py/selenium/webdriver/support/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}")
Expand Down
87 changes: 44 additions & 43 deletions py/test/selenium/webdriver/common/element_attribute_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,53 @@
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()


def test_should_return_the_value_of_the_index_attribute_even_if_it_is_missing(driver, pages):
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):
Expand Down Expand Up @@ -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") == False
checkbox.click()
assert "true" == checkbox.get_attribute("checked")
assert True == checkbox.get_property("checked")


def test_should_return_the_value_of_selected_for_radio_buttons_even_if_they_lack_that_attribute(driver, pages):
Expand All @@ -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") == False
assert initiallyNotSelected.get_property("checked") == False
assert True == 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 == initiallyNotSelected.get_property("checked")
assert initiallySelected.get_property("checked") == False


def test_should_return_the_value_of_selected_for_options_in_selects_even_if_they_lack_that_attribute(driver, pages):
Expand All @@ -155,14 +155,15 @@ 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" == one.get_dom_attribute("selected")
assert True == one.get_property("selected")
assert two.get_dom_attribute("selected") is None


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


Expand All @@ -172,44 +173,44 @@ 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


def test_should_return_the_contents_of_atext_area_as_its_value_when_set_to_non_norminal_true(driver, pages):
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


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()


Expand All @@ -219,54 +220,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("[email protected]")
assert "[email protected]" == element.get_attribute("value")
assert "[email protected]" == 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
Expand All @@ -276,7 +277,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


Expand All @@ -288,5 +289,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

0 comments on commit aca6f88

Please sign in to comment.