Path: blob/trunk/java/test/org/openqa/selenium/ElementDomAttributeTest.java
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.1617package org.openqa.selenium;1819import static java.util.Arrays.asList;20import static org.assertj.core.api.Assertions.assertThat;21import static org.assertj.core.api.Assertions.assertThatExceptionOfType;22import static org.openqa.selenium.testing.drivers.Browser.CHROME;23import static org.openqa.selenium.testing.drivers.Browser.EDGE;24import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;25import static org.openqa.selenium.testing.drivers.Browser.IE;26import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2728import java.util.List;29import org.junit.jupiter.api.Test;30import org.openqa.selenium.environment.webserver.Page;31import org.openqa.selenium.testing.JupiterTestBase;32import org.openqa.selenium.testing.NotYetImplemented;3334class ElementDomAttributeTest extends JupiterTestBase {3536@Test37void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {38driver.get(pages.simpleTestPage);39WebElement head = driver.findElement(By.xpath("/html"));40String attribute = head.getDomAttribute("cheese");41assertThat(attribute).isNull();42}4344@Test45void testShouldReturnNullWhenGettingSrcAttributeOfInvalidImgTag() {46driver.get(pages.simpleTestPage);47WebElement img = driver.findElement(By.id("invalidImgTag"));48String attribute = img.getDomAttribute("src");49assertThat(attribute).isNull();50}5152@Test53void testShouldReturnTheActualValueWhenGettingSrcAttributeOfAValidImgTag() {54driver.get(pages.simpleTestPage);55WebElement img = driver.findElement(By.id("validImgTag"));56String attribute = img.getDomAttribute("src");57assertThat(attribute).isEqualTo("icon.gif");58}5960@Test61void testShouldReturnTheActualValueWhenGettingHrefAttributeOfAValidAnchorTag() {62driver.get(pages.simpleTestPage);63WebElement img = driver.findElement(By.id("validAnchorTag"));64String attribute = img.getDomAttribute("href");65assertThat(attribute).isEqualTo("icon.gif");66}6768@Test69void testShouldReturnEmptyAttributeValuesWhenPresentAndTheValueIsActuallyEmpty() {70driver.get(pages.simpleTestPage);71WebElement body = driver.findElement(By.xpath("//body"));72assertThat(body.getDomAttribute("style")).isEmpty();73}7475@Test76void testShouldReturnTheValueOfTheDisabledAttributeAsNullIfNotSet() {77driver.get(pages.formPage);78WebElement inputElement = driver.findElement(By.xpath("//input[@id='working']"));79assertThat(inputElement.getDomAttribute("disabled")).isNull();80assertThat(inputElement.isEnabled()).isTrue();8182WebElement pElement = driver.findElement(By.id("peas"));83assertThat(pElement.getDomAttribute("disabled")).isNull();84assertThat(pElement.isEnabled()).isTrue();85}8687@Test88void testShouldNotReturnTheValueOfTheIndexAttributeIfItIsMissing() {89driver.get(pages.formPage);90WebElement multiSelect = driver.findElement(By.id("multi"));91List<WebElement> options = multiSelect.findElements(By.tagName("option"));92assertThat(options.get(1).getDomAttribute("index")).isNull();93}9495@Test96void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {97driver.get(pages.formPage);98WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));99assertThat(inputElement.isEnabled()).isFalse();100101inputElement = driver.findElement(By.xpath("//input[@id='working']"));102assertThat(inputElement.isEnabled()).isTrue();103}104105@Test106void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {107driver.get(pages.formPage);108WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));109assertThat(disabledTextElement1.isEnabled()).isFalse();110111WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));112assertThat(disabledTextElement2.isEnabled()).isFalse();113114WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));115assertThat(disabledSubmitElement.isEnabled()).isFalse();116}117118@Test119@NotYetImplemented(SAFARI)120public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {121driver.get(pages.formPage);122WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));123assertThatExceptionOfType(InvalidElementStateException.class)124.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));125assertThat(disabledTextElement1.getText()).isEmpty();126127WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));128assertThatExceptionOfType(InvalidElementStateException.class)129.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));130assertThat(disabledTextElement2.getText()).isEmpty();131}132133@Test134void testShouldIndicateWhenATextAreaIsDisabled() {135driver.get(pages.formPage);136WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));137assertThat(textArea.isEnabled()).isFalse();138}139140@Test141void testShouldIndicateWhenASelectIsDisabled() {142driver.get(pages.formPage);143144WebElement enabled = driver.findElement(By.name("selectomatic"));145WebElement disabled = driver.findElement(By.name("no-select"));146147assertThat(enabled.isEnabled()).isTrue();148assertThat(disabled.isEnabled()).isFalse();149}150151@Test152void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {153driver.get(pages.formPage);154WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));155List<WebElement> options = selectBox.findElements(By.tagName("option"));156WebElement one = options.get(0);157WebElement two = options.get(1);158assertThat(one.isSelected()).isTrue();159assertThat(two.isSelected()).isFalse();160assertThat(one.getDomAttribute("selected")).isEqualTo("true");161assertThat(two.getDomAttribute("selected")).isNull();162}163164@Test165void testShouldReturnValueOfClassAttributeOfAnElement() {166driver.get(pages.xhtmlTestPage);167168WebElement heading = driver.findElement(By.xpath("//h1"));169String className = heading.getDomAttribute("class");170171assertThat(className).isEqualTo("header");172}173174@Test175void testShouldNotReturnTheContentsOfATextAreaAsItsValue() {176driver.get(pages.formPage);177String value = driver.findElement(By.id("withText")).getDomAttribute("value");178assertThat(value).isNull();179}180181@Test182void testShouldNotReturnInnerHtmlProperty() {183driver.get(pages.simpleTestPage);184String html = driver.findElement(By.id("wrappingtext")).getDomAttribute("innerHTML");185assertThat(html).isNull();186}187188@Test189void testShouldTreatReadonlyAsAValue() {190driver.get(pages.formPage);191192WebElement element = driver.findElement(By.name("readonly"));193String readonly = element.getDomAttribute("readonly");194195assertThat(readonly).isNotNull();196197WebElement textInput = driver.findElement(By.name("x"));198String notReadonly = textInput.getDomAttribute("readonly");199200assertThat(readonly).isNotEqualTo(notReadonly);201}202203@Test204void testShouldNotReturnTextContentProperty() {205driver.get(pages.simpleTestPage);206WebElement element = driver.findElement(By.id("hiddenline"));207assertThat(element.getDomAttribute("textContent")).isNull();208}209210@Test211void testShouldGetNumericAttribute() {212driver.get(pages.formPage);213WebElement element = driver.findElement(By.id("withText"));214assertThat(element.getDomAttribute("rows")).isEqualTo("5");215}216217@Test218void testCanReturnATextApproximationOfTheStyleAttribute() {219driver.get(pages.javascriptPage);220221String style = driver.findElement(By.id("red-item")).getDomAttribute("style");222223assertThat(style.toLowerCase().contains("background-color")).isTrue();224}225226@Test227void testShouldCorrectlyReportValueOfColspan() {228driver.get(pages.tables);229230WebElement th1 = driver.findElement(By.id("th1"));231WebElement td2 = driver.findElement(By.id("td2"));232233assertThat(th1.getDomAttribute("id")).isEqualTo("th1");234assertThat(th1.getDomAttribute("colspan")).isEqualTo("3");235236assertThat(td2.getDomAttribute("id")).isEqualTo("td2");237assertThat(td2.getDomAttribute("colspan")).isEqualTo("2");238}239240// This is a test-case re-creating issue 900.241@Test242void testShouldReturnValueOfOnClickAttribute() {243driver.get(pages.javascriptPage);244245WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));246247String onClickValue = mouseclickDiv.getDomAttribute("onclick");248String expectedOnClickValue = "displayMessage('mouse click');";249assertThat(onClickValue)250.as("Javascript code")251.isIn(252"javascript:" + expectedOnClickValue, // Non-IE253"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE254"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE255256WebElement mousedownDiv = driver.findElement(By.id("mousedown"));257assertThat(mousedownDiv.getDomAttribute("onclick")).isNull();258}259260@Test261void testgetDomAttributeDoesNotReturnAnObjectForSvgProperties() {262driver.get(pages.svgPage);263WebElement svgElement = driver.findElement(By.id("rotate"));264assertThat(svgElement.getDomAttribute("transform")).isEqualTo("rotate(30)");265}266267@Test268void testCanRetrieveTheCurrentValueOfATextFormFieldWithPresetText() {269driver.get(pages.formPage);270WebElement element = driver.findElement(By.id("inputWithText"));271assertThat(element.getDomAttribute("value")).isEqualTo("Example text");272element.sendKeys("[email protected]");273assertThat(element.getDomAttribute("value")).isEqualTo("Example text");274}275276@Test277void testShouldNotReturnTextOfATextArea() {278driver.get(pages.formPage);279WebElement element = driver.findElement(By.id("withText"));280assertThat(element.getDomAttribute("value")).isNull();281}282283@Test284void testShouldReturnNullForNonPresentBooleanAttributes() {285driver.get(pages.booleanAttributes);286WebElement element1 = driver.findElement(By.id("working"));287assertThat(element1.getDomAttribute("required")).isNull();288WebElement element2 = driver.findElement(By.id("wallace"));289assertThat(element2.getDomAttribute("nowrap")).isNull();290}291292@Test293@NotYetImplemented(value = CHROME, reason = "It returns a property")294@NotYetImplemented(EDGE)295@NotYetImplemented(FIREFOX)296public void testShouldReturnEmptyStringForPresentBooleanAttributes() {297driver.get(pages.booleanAttributes);298WebElement element1 = driver.findElement(By.id("emailRequired"));299assertThat(element1.getDomAttribute("required")).isEmpty();300WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));301assertThat(element2.getDomAttribute("required")).isEqualTo("required");302WebElement element3 = driver.findElement(By.id("inputRequired"));303assertThat(element3.getDomAttribute("required")).isEmpty();304WebElement element4 = driver.findElement(By.id("textAreaRequired"));305assertThat(element4.getDomAttribute("required")).isEqualTo("false");306WebElement element5 = driver.findElement(By.id("unwrappable"));307assertThat(element5.getDomAttribute("nowrap")).isEmpty();308}309310@Test311void testMultipleAttributeShouldBeNullWhenNotSet() {312driver.get(pages.selectPage);313WebElement element = driver.findElement(By.id("selectWithoutMultiple"));314assertThat(element.getDomAttribute("multiple")).isNull();315}316317@Test318void testMultipleAttributeShouldBeTrueWhenSet() {319driver.get(pages.selectPage);320WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));321assertThat(element.getDomAttribute("multiple")).isEqualTo("true");322}323324@Test325void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {326driver.get(pages.selectPage);327WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));328assertThat(element.getDomAttribute("multiple")).isEqualTo("true");329}330331@Test332void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {333driver.get(pages.selectPage);334WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));335assertThat(element.getDomAttribute("multiple")).isEqualTo("true");336}337338@Test339void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {340driver.get(pages.selectPage);341WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));342assertThat(element.getDomAttribute("multiple")).isEqualTo("true");343}344345@Test346void shouldTreatContenteditableAsEnumeratedButNotBoolean() {347checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");348}349350@Test351@NotYetImplemented(IE)352@NotYetImplemented(SAFARI)353public void shouldTreatDraggableAsEnumeratedButNotBoolean() {354checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");355}356357private void checkEnumeratedAttribute(String name, String... values) {358asList(values)359.forEach(360value -> {361driver.get(362appServer.create(363new Page()364.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));365assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEqualTo(value);366});367368driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));369assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEmpty();370371driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));372assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isNull();373}374}375376377