Path: blob/trunk/py/test/selenium/webdriver/common/element_attribute_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import pytest1819from selenium.common.exceptions import WebDriverException20from selenium.webdriver.common.by import By212223def test_should_return_null_when_getting_the_value_of_an_attribute_that_is_not_listed(driver, pages):24pages.load("simpleTest.html")25head = driver.find_element(By.XPATH, "/html")26attribute = head.get_attribute("cheese")27assert attribute is None282930def test_should_return_null_when_getting_src_attribute_of_invalid_img_tag(driver, pages):31pages.load("simpleTest.html")32img = driver.find_element(By.ID, "invalidImgTag")33img_attr = img.get_attribute("src")34assert img_attr is None353637def test_should_return_an_absolute_url_when_getting_src_attribute_of_avalid_img_tag(driver, pages):38pages.load("simpleTest.html")39img = driver.find_element(By.ID, "validImgTag")40img_attr = img.get_attribute("src")41assert "icon.gif" in img_attr424344def test_should_return_an_absolute_url_when_getting_href_attribute_of_avalid_anchor_tag(driver, pages):45pages.load("simpleTest.html")46img = driver.find_element(By.ID, "validAnchorTag")47img_attr = img.get_attribute("href")48assert "icon.gif" in img_attr495051def test_should_return_empty_attribute_values_when_present_and_the_value_is_actually_empty(driver, pages):52pages.load("simpleTest.html")53body = driver.find_element(By.XPATH, "//body")54assert "" == body.get_attribute("style")555657def test_should_return_the_value_of_the_disabled_attribute_as_false_if_not_set(driver, pages):58pages.load("formPage.html")59inputElement = driver.find_element(By.XPATH, "//input[@id='working']")60assert inputElement.get_attribute("disabled") is None61assert inputElement.is_enabled()6263pElement = driver.find_element(By.ID, "peas")64assert pElement.get_attribute("disabled") is None65assert pElement.is_enabled()666768def test_should_return_the_value_of_the_index_attribute_even_if_it_is_missing(driver, pages):69pages.load("formPage.html")70multiSelect = driver.find_element(By.ID, "multi")71options = multiSelect.find_elements(By.TAG_NAME, "option")72assert "1" == options[1].get_attribute("index")737475def test_should_indicate_the_elements_that_are_disabled_are_not_is_enabled(driver, pages):76pages.load("formPage.html")77inputElement = driver.find_element(By.XPATH, "//input[@id='notWorking']")78assert not inputElement.is_enabled()7980inputElement = driver.find_element(By.XPATH, "//input[@id='working']")81assert inputElement.is_enabled()828384def test_elements_should_be_disabled_if_they_are_disabled_using_random_disabled_strings(driver, pages):85pages.load("formPage.html")86disabledTextElement1 = driver.find_element(By.ID, "disabledTextElement1")87assert not disabledTextElement1.is_enabled()8889disabledTextElement2 = driver.find_element(By.ID, "disabledTextElement2")90assert not disabledTextElement2.is_enabled()9192disabledSubmitElement = driver.find_element(By.ID, "disabledSubmitElement")93assert not disabledSubmitElement.is_enabled()949596def test_should_indicate_when_atext_area_is_disabled(driver, pages):97pages.load("formPage.html")98textArea = driver.find_element(By.XPATH, "//textarea[@id='notWorkingArea']")99assert not textArea.is_enabled()100101102@pytest.mark.xfail_safari103def test_should_throw_exception_if_sending_keys_to_element_disabled_using_random_disabled_strings(driver, pages):104pages.load("formPage.html")105disabledTextElement1 = driver.find_element(By.ID, "disabledTextElement1")106with pytest.raises(WebDriverException):107disabledTextElement1.send_keys("foo")108assert "" == disabledTextElement1.text109110disabledTextElement2 = driver.find_element(By.ID, "disabledTextElement2")111with pytest.raises(WebDriverException):112disabledTextElement2.send_keys("bar")113assert "" == disabledTextElement2.text114115116def test_should_indicate_when_aselect_is_disabled(driver, pages):117pages.load("formPage.html")118enabled = driver.find_element(By.NAME, "selectomatic")119disabled = driver.find_element(By.NAME, "no-select")120121assert enabled.is_enabled()122assert not disabled.is_enabled()123124125def test_should_return_the_value_of_checked_for_acheckbox_even_if_it_lacks_that_attribute(driver, pages):126pages.load("formPage.html")127checkbox = driver.find_element(By.XPATH, "//input[@id='checky']")128assert checkbox.get_attribute("checked") is None129checkbox.click()130assert "true" == checkbox.get_attribute("checked")131132133def test_should_return_the_value_of_selected_for_radio_buttons_even_if_they_lack_that_attribute(driver, pages):134pages.load("formPage.html")135neverSelected = driver.find_element(By.ID, "cheese")136initiallyNotSelected = driver.find_element(By.ID, "peas")137initiallySelected = driver.find_element(By.ID, "cheese_and_peas")138139assert neverSelected.get_attribute("checked") is None140assert initiallyNotSelected.get_attribute("checked") is None141assert "true" == initiallySelected.get_attribute("checked")142143initiallyNotSelected.click()144assert neverSelected.get_attribute("selected") is None145assert "true" == initiallyNotSelected.get_attribute("checked")146assert initiallySelected.get_attribute("checked") is None147148149def test_should_return_the_value_of_selected_for_options_in_selects_even_if_they_lack_that_attribute(driver, pages):150pages.load("formPage.html")151selectBox = driver.find_element(By.XPATH, "//select[@name='selectomatic']")152options = selectBox.find_elements(By.TAG_NAME, "option")153one = options[0]154two = options[1]155assert one.is_selected()156assert not two.is_selected()157assert "true" == one.get_attribute("selected")158assert two.get_attribute("selected") is None159160161def test_should_return_value_of_class_attribute_of_an_element(driver, pages):162pages.load("xhtmlTest.html")163heading = driver.find_element(By.XPATH, "//h1")164classname = heading.get_attribute("class")165assert "header" == classname166167168# Disabled due to issues with Frames169# def test_should_return_value_of_class_attribute_of_an_element_after_switching_iframe(driver, pages):170# pages.load("iframes.html")171# driver.switch_to.frame("iframe1")172#173# wallace = driver.find_element(By.XPATH, "//div[@id='wallace']")174# classname = wallace.get_attribute("class")175# assert "gromit" == classname176177178def test_should_return_the_contents_of_atext_area_as_its_value(driver, pages):179pages.load("formPage.html")180value = driver.find_element(By.ID, "withText").get_attribute("value")181assert "Example text" == value182183184def test_should_return_the_contents_of_atext_area_as_its_value_when_set_to_non_norminal_true(driver, pages):185pages.load("formPage.html")186e = driver.find_element(By.ID, "withText")187driver.execute_script("arguments[0].value = 'tRuE'", e)188value = e.get_attribute("value")189assert "tRuE" == value190191192def test_should_treat_readonly_as_avalue(driver, pages):193pages.load("formPage.html")194element = driver.find_element(By.NAME, "readonly")195readOnlyAttribute = element.get_attribute("readonly")196197textInput = driver.find_element(By.NAME, "x")198notReadOnly = textInput.get_attribute("readonly")199200assert readOnlyAttribute != notReadOnly201202203def test_should_get_numeric_attribute(driver, pages):204pages.load("formPage.html")205element = driver.find_element(By.ID, "withText")206assert "5" == element.get_attribute("rows")207208209def test_can_return_atext_approximation_of_the_style_attribute(driver, pages):210pages.load("javascriptPage.html")211style = driver.find_element(By.ID, "red-item").get_attribute("style")212assert "background-color" in style.lower()213214215def test_should_correctly_report_value_of_colspan(driver, pages):216pages.load("tables.html")217218th1 = driver.find_element(By.ID, "th1")219td2 = driver.find_element(By.ID, "td2")220221assert "th1" == th1.get_attribute("id")222assert "3" == th1.get_attribute("colspan")223224assert "td2" == td2.get_attribute("id")225assert "2" == td2.get_attribute("colspan")226227228def test_can_retrieve_the_current_value_of_atext_form_field_text_input(driver, pages):229pages.load("formPage.html")230element = driver.find_element(By.ID, "working")231assert "" == element.get_attribute("value")232element.send_keys("hello world")233assert "hello world" == element.get_attribute("value")234235236def test_can_retrieve_the_current_value_of_atext_form_field_email_input(driver, pages):237pages.load("formPage.html")238element = driver.find_element(By.ID, "email")239assert "" == element.get_attribute("value")240element.send_keys("[email protected]")241assert "[email protected]" == element.get_attribute("value")242243244def test_can_retrieve_the_current_value_of_atext_form_field_text_area(driver, pages):245pages.load("formPage.html")246element = driver.find_element(By.ID, "emptyTextArea")247assert "" == element.get_attribute("value")248element.send_keys("hello world")249assert "hello world" == element.get_attribute("value")250251252def test_should_return_null_for_non_present_boolean_attributes(driver, pages):253pages.load("booleanAttributes.html")254element1 = driver.find_element(By.ID, "working")255assert element1.get_attribute("required") is None256257258@pytest.mark.xfail_ie259def test_should_return_true_for_present_boolean_attributes(driver, pages):260pages.load("booleanAttributes.html")261element1 = driver.find_element(By.ID, "emailRequired")262assert "true" == element1.get_attribute("required")263element2 = driver.find_element(By.ID, "emptyTextAreaRequired")264assert "true" == element2.get_attribute("required")265element3 = driver.find_element(By.ID, "inputRequired")266assert "true" == element3.get_attribute("required")267element4 = driver.find_element(By.ID, "textAreaRequired")268assert "true" == element4.get_attribute("required")269270271@pytest.mark.xfail_chrome272@pytest.mark.xfail_edge273@pytest.mark.xfail_firefox274@pytest.mark.xfail_safari275@pytest.mark.xfail_remote276def test_should_get_unicode_chars_from_attribute(driver, pages):277pages.load("formPage.html")278title = driver.find_element(By.ID, "vsearchGadget").get_attribute("title")279assert "Hvad s\xf8ger du?" == title280281282@pytest.mark.xfail_chrome283@pytest.mark.xfail_edge284@pytest.mark.xfail_firefox285@pytest.mark.xfail_safari286@pytest.mark.xfail_remote287def test_should_get_values_and_not_miss_items(driver, pages):288pages.load("attributes.html")289expected = "4b273a33fbbd29013nN93dy4F1A~"290result = driver.find_element(By.CSS_SELECTOR, "li").get_attribute("value")291assert expected == result292293294