Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] Replace deprecated get_attribute() in tests #14807

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions py/selenium/webdriver/support/expected_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
T = TypeVar("T")

WebDriverOrWebElement = Union[WebDriver, WebElement]
attr_get_property = ["value", "checked", "index", "selected"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "id" should be a property too, otherwise you only read ids defined in html.
All the .get_dom_attribute("id") calls below should be replaced by get_property("id")



def title_is(title: str) -> Callable[[WebDriver], bool]:
Expand Down Expand Up @@ -259,7 +260,7 @@ def text_to_be_present_in_element_value(

def _predicate(driver: WebDriverOrWebElement):
try:
element_text = driver.find_element(*locator).get_attribute("value")
element_text = driver.find_element(*locator).get_property("value")
return text_ in element_text
except StaleElementReferenceException:
return False
Expand All @@ -278,7 +279,11 @@ def text_to_be_present_in_element_attribute(

def _predicate(driver: WebDriverOrWebElement):
try:
element_text = driver.find_element(*locator).get_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
Expand Down Expand Up @@ -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_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_)
)
Comment on lines +491 to +495
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to translate the names on the caller site if one of PROPERTY_ALIASES (defined in the /attribute.js) is used.
https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/webdriver/atoms/attribute.js

return element_attribute is not None
except StaleElementReferenceException:
return False
Expand Down
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_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_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
16 changes: 8 additions & 8 deletions py/test/selenium/webdriver/common/api_example_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_find_elements_by_xpath(driver, pages):
pages.load("nestedElements.html")
elems = driver.find_elements(By.XPATH, "//option")
assert 48 == len(elems)
assert "One" == elems[0].get_attribute("value")
assert "One" == elems[0].get_property("value")


def test_find_elements_by_name(driver, pages):
Expand All @@ -66,28 +66,28 @@ def test_find_elements_by_name_in_element_context(driver, pages):
pages.load("nestedElements.html")
elem = driver.find_element(By.NAME, "form2")
sub_elem = elem.find_element(By.NAME, "selectomatic")
assert "2" == sub_elem.get_attribute("id")
assert "2" == sub_elem.get_dom_attribute("id")


def test_find_elements_by_link_text_in_element_context(driver, pages):
pages.load("nestedElements.html")
elem = driver.find_element(By.NAME, "div1")
sub_elem = elem.find_element(By.LINK_TEXT, "hello world")
assert "link1" == sub_elem.get_attribute("name")
assert "link1" == sub_elem.get_dom_attribute("name")


def test_find_element_by_id_in_element_context(driver, pages):
pages.load("nestedElements.html")
elem = driver.find_element(By.NAME, "form2")
sub_elem = elem.find_element(By.ID, "2")
assert "selectomatic" == sub_elem.get_attribute("name")
assert "selectomatic" == sub_elem.get_dom_attribute("name")


def test_find_element_by_xpath_in_element_context(driver, pages):
pages.load("nestedElements.html")
elem = driver.find_element(By.NAME, "form2")
sub_elem = elem.find_element(By.XPATH, "select")
assert "2" == sub_elem.get_attribute("id")
assert "2" == sub_elem.get_dom_attribute("id")


def test_find_element_by_xpath_in_element_context_not_found(driver, pages):
Expand All @@ -103,7 +103,7 @@ def test_should_be_able_to_enter_data_into_form_fields(driver, pages):
elem.clear()
elem.send_keys("some text")
elem = driver.find_element(By.XPATH, "//form[@name='someForm']/input[@id='username']")
assert "some text" == elem.get_attribute("value")
assert "some text" == elem.get_property("value")


def test_find_element_by_tag_name(driver, pages):
Expand Down Expand Up @@ -164,7 +164,7 @@ def test_get_attribute(driver, pages):
url = pages.url("xhtmlTest.html")
driver.get(url)
elem = driver.find_element(By.ID, "id1")
attr = elem.get_attribute("href")
attr = elem.get_property("href")
assert f"{url}#" == attr


Expand All @@ -173,7 +173,7 @@ def test_get_implicit_attribute(driver, pages):
elems = driver.find_elements(By.XPATH, "//option")
assert len(elems) >= 3
for i, elem in enumerate(elems[:3]):
assert i == int(elem.get_attribute("index"))
assert i == int(elem.get_property("index"))


def test_get_dom_attribute(driver, pages):
Expand Down
16 changes: 8 additions & 8 deletions py/test/selenium/webdriver/common/children_finding_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_should_find_element_by_xpath(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "form2")
child = element.find_element(By.XPATH, "select")
assert child.get_attribute("id") == "2"
assert child.get_dom_attribute("id") == "2"


def test_should_not_find_element_by_xpath(driver, pages):
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_should_find_element_by_name(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "form2")
child = element.find_element(By.NAME, "selectomatic")
assert child.get_attribute("id") == "2"
assert child.get_dom_attribute("id") == "2"


def test_should_find_elements_by_name(driver, pages):
Expand All @@ -86,7 +86,7 @@ def test_should_find_element_by_id(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "form2")
child = element.find_element(By.ID, "2")
assert child.get_attribute("name") == "selectomatic"
assert child.get_dom_attribute("name") == "selectomatic"


def test_should_find_elements_by_id(driver, pages):
Expand Down Expand Up @@ -114,16 +114,16 @@ def test_should_find_element_by_link_text(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "div1")
child = element.find_element(By.LINK_TEXT, "hello world")
assert child.get_attribute("name") == "link1"
assert child.get_dom_attribute("name") == "link1"


def test_should_find_elements_by_link_text(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "div1")
children = element.find_elements(By.LINK_TEXT, "hello world")
assert len(children) == 2
assert "link1" == children[0].get_attribute("name")
assert "link2" == children[1].get_attribute("name")
assert "link1" == children[0].get_dom_attribute("name")
assert "link2" == children[1].get_dom_attribute("name")


def test_should_find_element_by_class_name(driver, pages):
Expand All @@ -144,7 +144,7 @@ def test_should_find_element_by_tag_name(driver, pages):
pages.load("nestedElements.html")
parent = driver.find_element(By.NAME, "div1")
element = parent.find_element(By.TAG_NAME, "a")
assert "link1" == element.get_attribute("name")
assert "link1" == element.get_dom_attribute("name")


def test_should_find_elements_by_tag_name(driver, pages):
Expand All @@ -158,7 +158,7 @@ def test_should_be_able_to_find_an_element_by_css_selector(driver, pages):
pages.load("nestedElements.html")
parent = driver.find_element(By.NAME, "form2")
element = parent.find_element(By.CSS_SELECTOR, '*[name="selectomatic"]')
assert "2" == element.get_attribute("id")
assert "2" == element.get_dom_attribute("id")


def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):
Expand Down
4 changes: 2 additions & 2 deletions py/test/selenium/webdriver/common/clear_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_writable_text_input_should_clear(driver, pages):
pages.load("readOnlyPage.html")
element = driver.find_element(By.ID, "writableTextInput")
element.clear()
assert "" == element.get_attribute("value")
assert "" == element.get_property("value")


@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
Expand All @@ -49,7 +49,7 @@ def test_writable_text_area_should_clear(driver, pages):
pages.load("readOnlyPage.html")
element = driver.find_element(By.ID, "writableTextArea")
element.clear()
assert "" == element.get_attribute("value")
assert "" == element.get_property("value")


@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_should_emit_click_event_when_clicking_on_atext_input_element(driver, pa
clicker = driver.find_element(By.ID, "clickField")
clicker.click()

assert clicker.get_attribute("value") == "Clicked"
assert clicker.get_property("value") == "Clicked"


@pytest.mark.xfail_safari
Expand Down
46 changes: 23 additions & 23 deletions py/test/selenium/webdriver/common/driver_element_finding_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
def test_should_be_able_to_find_asingle_element_by_id(driver, pages):
pages.load("xhtmlTest.html")
element = driver.find_element(By.ID, "linkId")
assert element.get_attribute("id") == "linkId"
assert element.get_dom_attribute("id") == "linkId"


def test_should_be_able_to_find_asingle_element_by_numeric_id(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.ID, "2")
assert element.get_attribute("id") == "2"
assert element.get_dom_attribute("id") == "2"


def test_should_be_able_to_find_an_element_with_css_escape(driver, pages):
pages.load("idElements.html")
element = driver.find_element(By.ID, "with.dots")
assert element.get_attribute("id") == "with.dots"
assert element.get_dom_attribute("id") == "with.dots"


def test_should_be_able_to_find_multiple_elements_by_id(driver, pages):
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_no_such_element_error(driver, pages):
def test_should_be_able_to_find_asingle_element_by_name(driver, pages):
pages.load("formPage.html")
element = driver.find_element(By.NAME, "checky")
assert element.get_attribute("value") == "furrfu"
assert element.get_property("value") == "furrfu"


def test_should_be_able_to_find_multiple_elements_by_name(driver, pages):
Expand All @@ -118,7 +118,7 @@ def test_should_be_able_to_find_multiple_elements_by_name(driver, pages):
def test_should_be_able_to_find_an_element_that_does_not_support_the_name_property(driver, pages):
pages.load("nestedElements.html")
element = driver.find_element(By.NAME, "div1")
assert element.get_attribute("name") == "div1"
assert element.get_dom_attribute("name") == "div1"


# By.name negative
Expand Down Expand Up @@ -356,7 +356,7 @@ def test_should_be_able_to_find_an_element_by_xpath_with_multiple_attributes(dri
pages.load("formPage.html")
element = driver.find_element(By.XPATH, "//form[@name='optional']/input[@type='submit' and @value='Click!']")
assert element.tag_name.lower() == "input"
assert element.get_attribute("value") == "Click!"
assert element.get_property("value") == "Click!"


def test_finding_alink_by_xpath_should_locate_an_element_with_the_given_text(driver, pages):
Expand Down Expand Up @@ -489,7 +489,7 @@ def test_should_be_able_to_find_asingle_element_by_css_selector(driver, pages):
pages.load("xhtmlTest.html")
element = driver.find_element(By.CSS_SELECTOR, "div.content")
assert element.tag_name.lower() == "div"
assert element.get_attribute("class") == "content"
assert element.get_dom_attribute("class") == "content"


def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):
Expand All @@ -502,33 +502,33 @@ def test_should_be_able_to_find_asingle_element_by_compound_css_selector(driver,
pages.load("xhtmlTest.html")
element = driver.find_element(By.CSS_SELECTOR, "div.extraDiv, div.content")
assert element.tag_name.lower() == "div"
assert element.get_attribute("class") == "content"
assert element.get_dom_attribute("class") == "content"


def test_should_be_able_to_find_multiple_elements_by_compound_css_selector(driver, pages):
pages.load("xhtmlTest.html")
elements = driver.find_elements(By.CSS_SELECTOR, "div.extraDiv, div.content")
assert len(elements) > 1
assert elements[0].get_attribute("class") == "content"
assert elements[1].get_attribute("class") == "extraDiv"
assert elements[0].get_dom_attribute("class") == "content"
assert elements[1].get_dom_attribute("class") == "extraDiv"


def test_should_be_able_to_find_an_element_by_boolean_attribute_using_css_selector(driver, pages):
pages.load("locators_tests/boolean_attribute_selected.html")
element = driver.find_element(By.CSS_SELECTOR, "option[selected='selected']")
assert element.get_attribute("value") == "two"
assert element.get_property("value") == "two"


def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector(driver, pages):
pages.load("locators_tests/boolean_attribute_selected.html")
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
assert element.get_attribute("value") == "two"
assert element.get_property("value") == "two"


def test_should_be_able_to_find_an_element_by_boolean_attribute_using_short_css_selector_on_html_4_page(driver, pages):
pages.load("locators_tests/boolean_attribute_selected_html4.html")
element = driver.find_element(By.CSS_SELECTOR, "option[selected]")
assert element.get_attribute("value") == "two"
assert element.get_property("value") == "two"


# By.css_Selector negative
Expand Down Expand Up @@ -600,14 +600,14 @@ def test_should_be_able_to_find_multiple_links_by_text(driver, pages):
def test_should_find_element_by_link_text_containing_equals_sign(driver, pages):
pages.load("xhtmlTest.html")
element = driver.find_element(By.LINK_TEXT, "Link=equalssign")
assert element.get_attribute("id") == "linkWithEqualsSign"
assert element.get_dom_attribute("id") == "linkWithEqualsSign"


def test_should_find_multiple_elements_by_link_text_containing_equals_sign(driver, pages):
pages.load("xhtmlTest.html")
elements = driver.find_elements(By.LINK_TEXT, "Link=equalssign")
assert 1 == len(elements)
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
assert elements[0].get_dom_attribute("id") == "linkWithEqualsSign"


def test_finds_by_link_text_on_xhtml_page(driver, pages):
Expand All @@ -629,7 +629,7 @@ def test_link_with_formatting_tags(driver, pages):
def test_driver_can_get_link_by_link_test_ignoring_trailing_whitespace(driver, pages):
pages.load("simpleTest.html")
link = driver.find_element(By.LINK_TEXT, "link with trailing space")
assert link.get_attribute("id") == "linkWithTrailingSpace"
assert link.get_dom_attribute("id") == "linkWithTrailingSpace"
assert link.text == "link with trailing space"


Expand Down Expand Up @@ -666,14 +666,14 @@ def test_should_be_able_to_find_asingle_element_by_partial_link_text(driver, pag
def test_should_find_element_by_partial_link_text_containing_equals_sign(driver, pages):
pages.load("xhtmlTest.html")
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Link=")
assert element.get_attribute("id") == "linkWithEqualsSign"
assert element.get_dom_attribute("id") == "linkWithEqualsSign"


def test_should_find_multiple_elements_by_partial_link_text_containing_equals_sign(driver, pages):
pages.load("xhtmlTest.html")
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "Link=")
assert len(elements) == 1
assert elements[0].get_attribute("id") == "linkWithEqualsSign"
assert elements[0].get_dom_attribute("id") == "linkWithEqualsSign"


# Misc tests
Expand All @@ -693,22 +693,22 @@ def test_when_finding_by_name_should_not_return_by_id(driver, pages):
pages.load("formPage.html")

element = driver.find_element(By.NAME, "id-name1")
assert element.get_attribute("value") == "name"
assert element.get_property("value") == "name"

element = driver.find_element(By.ID, "id-name1")
assert element.get_attribute("value") == "id"
assert element.get_property("value") == "id"

element = driver.find_element(By.NAME, "id-name2")
assert element.get_attribute("value") == "name"
assert element.get_property("value") == "name"

element = driver.find_element(By.ID, "id-name2")
assert element.get_attribute("value") == "id"
assert element.get_property("value") == "id"


def test_should_be_able_to_find_ahidden_elements_by_name(driver, pages):
pages.load("formPage.html")
element = driver.find_element(By.NAME, "hidden")
assert element.get_attribute("name") == "hidden"
assert element.get_dom_attribute("name") == "hidden"


def test_should_not_be_able_to_find_an_element_on_a_blank_page(driver, pages):
Expand Down
Loading
Loading