Path: blob/trunk/java/test/org/openqa/selenium/ElementDomAttributeTest.java
3989 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 org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.testing.drivers.Browser.CHROME;22import static org.openqa.selenium.testing.drivers.Browser.EDGE;23import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;24import static org.openqa.selenium.testing.drivers.Browser.IE;25import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2627import java.util.List;28import org.junit.jupiter.api.Test;29import org.openqa.selenium.environment.webserver.Page;30import org.openqa.selenium.testing.JupiterTestBase;31import org.openqa.selenium.testing.NotYetImplemented;3233class ElementDomAttributeTest extends JupiterTestBase {3435@Test36void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {37driver.get(pages.simpleTestPage);38WebElement head = driver.findElement(By.xpath("/html"));39String attribute = head.getDomAttribute("cheese");40assertThat(attribute).isNull();41}4243@Test44void testShouldReturnNullWhenGettingSrcAttributeOfInvalidImgTag() {45driver.get(pages.simpleTestPage);46WebElement img = driver.findElement(By.id("invalidImgTag"));47String attribute = img.getDomAttribute("src");48assertThat(attribute).isNull();49}5051@Test52void testShouldReturnTheActualValueWhenGettingSrcAttributeOfAValidImgTag() {53driver.get(pages.simpleTestPage);54WebElement img = driver.findElement(By.id("validImgTag"));55String attribute = img.getDomAttribute("src");56assertThat(attribute).isEqualTo("icon.gif");57}5859@Test60void testShouldReturnTheActualValueWhenGettingHrefAttributeOfAValidAnchorTag() {61driver.get(pages.simpleTestPage);62WebElement img = driver.findElement(By.id("validAnchorTag"));63String attribute = img.getDomAttribute("href");64assertThat(attribute).isEqualTo("icon.gif");65}6667@Test68void testShouldReturnEmptyAttributeValuesWhenPresentAndTheValueIsActuallyEmpty() {69driver.get(pages.simpleTestPage);70WebElement body = driver.findElement(By.xpath("//body"));71assertThat(body.getDomAttribute("style")).isEmpty();72}7374@Test75void testShouldReturnTheValueOfTheDisabledAttributeAsNullIfNotSet() {76driver.get(pages.formPage);77WebElement inputElement = driver.findElement(By.xpath("//input[@id='working']"));78assertThat(inputElement.getDomAttribute("disabled")).isNull();79assertThat(inputElement.isEnabled()).isTrue();8081WebElement pElement = driver.findElement(By.id("peas"));82assertThat(pElement.getDomAttribute("disabled")).isNull();83assertThat(pElement.isEnabled()).isTrue();84}8586@Test87void testShouldNotReturnTheValueOfTheIndexAttributeIfItIsMissing() {88driver.get(pages.formPage);89WebElement multiSelect = driver.findElement(By.id("multi"));90List<WebElement> options = multiSelect.findElements(By.tagName("option"));91assertThat(options.get(1).getDomAttribute("index")).isNull();92}9394@Test95void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {96driver.get(pages.formPage);97WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));98assertThat(inputElement.isEnabled()).isFalse();99100inputElement = driver.findElement(By.xpath("//input[@id='working']"));101assertThat(inputElement.isEnabled()).isTrue();102}103104@Test105void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {106driver.get(pages.formPage);107WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));108assertThat(disabledTextElement1.isEnabled()).isFalse();109110WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));111assertThat(disabledTextElement2.isEnabled()).isFalse();112113WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));114assertThat(disabledSubmitElement.isEnabled()).isFalse();115}116117@Test118@NotYetImplemented(SAFARI)119public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {120driver.get(pages.formPage);121WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));122assertThatExceptionOfType(InvalidElementStateException.class)123.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));124assertThat(disabledTextElement1.getText()).isEmpty();125126WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));127assertThatExceptionOfType(InvalidElementStateException.class)128.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));129assertThat(disabledTextElement2.getText()).isEmpty();130}131132@Test133void testShouldIndicateWhenATextAreaIsDisabled() {134driver.get(pages.formPage);135WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));136assertThat(textArea.isEnabled()).isFalse();137}138139@Test140void testShouldIndicateWhenASelectIsDisabled() {141driver.get(pages.formPage);142143WebElement enabled = driver.findElement(By.name("selectomatic"));144WebElement disabled = driver.findElement(By.name("no-select"));145146assertThat(enabled.isEnabled()).isTrue();147assertThat(disabled.isEnabled()).isFalse();148}149150@Test151void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {152driver.get(pages.formPage);153WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));154List<WebElement> options = selectBox.findElements(By.tagName("option"));155WebElement one = options.get(0);156WebElement two = options.get(1);157assertThat(one.isSelected()).isTrue();158assertThat(two.isSelected()).isFalse();159assertThat(one.getDomAttribute("selected")).isEqualTo("true");160assertThat(two.getDomAttribute("selected")).isNull();161}162163@Test164void testShouldReturnValueOfClassAttributeOfAnElement() {165driver.get(pages.xhtmlTestPage);166167WebElement heading = driver.findElement(By.xpath("//h1"));168String className = heading.getDomAttribute("class");169170assertThat(className).isEqualTo("header");171}172173@Test174void testShouldNotReturnTheContentsOfATextAreaAsItsValue() {175driver.get(pages.formPage);176String value = driver.findElement(By.id("withText")).getDomAttribute("value");177assertThat(value).isNull();178}179180@Test181void testShouldNotReturnInnerHtmlProperty() {182driver.get(pages.simpleTestPage);183String html = driver.findElement(By.id("wrappingtext")).getDomAttribute("innerHTML");184assertThat(html).isNull();185}186187@Test188void testShouldTreatReadonlyAsAValue() {189driver.get(pages.formPage);190191WebElement element = driver.findElement(By.name("readonly"));192String readonly = element.getDomAttribute("readonly");193194assertThat(readonly).isNotNull();195196WebElement textInput = driver.findElement(By.name("x"));197String notReadonly = textInput.getDomAttribute("readonly");198199assertThat(readonly).isNotEqualTo(notReadonly);200}201202@Test203void testShouldNotReturnTextContentProperty() {204driver.get(pages.simpleTestPage);205WebElement element = driver.findElement(By.id("hiddenline"));206assertThat(element.getDomAttribute("textContent")).isNull();207}208209@Test210void testShouldGetNumericAttribute() {211driver.get(pages.formPage);212WebElement element = driver.findElement(By.id("withText"));213assertThat(element.getDomAttribute("rows")).isEqualTo("5");214}215216@Test217void testCanReturnATextApproximationOfTheStyleAttribute() {218driver.get(pages.javascriptPage);219220String style = driver.findElement(By.id("red-item")).getDomAttribute("style");221222assertThat(style).containsIgnoringCase("background-color");223}224225@Test226void testShouldCorrectlyReportValueOfColspan() {227driver.get(pages.tables);228229WebElement th1 = driver.findElement(By.id("th1"));230WebElement td2 = driver.findElement(By.id("td2"));231232assertThat(th1.getDomAttribute("id")).isEqualTo("th1");233assertThat(th1.getDomAttribute("colspan")).isEqualTo("3");234235assertThat(td2.getDomAttribute("id")).isEqualTo("td2");236assertThat(td2.getDomAttribute("colspan")).isEqualTo("2");237}238239// This is a test-case re-creating issue 900.240@Test241void testShouldReturnValueOfOnClickAttribute() {242driver.get(pages.javascriptPage);243244WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));245246String onClickValue = mouseclickDiv.getDomAttribute("onclick");247String expectedOnClickValue = "displayMessage('mouse click');";248assertThat(onClickValue)249.as("Javascript code")250.isIn(251"javascript:" + expectedOnClickValue, // Non-IE252"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE253"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE254255WebElement mousedownDiv = driver.findElement(By.id("mousedown"));256assertThat(mousedownDiv.getDomAttribute("onclick")).isNull();257}258259@Test260void testgetDomAttributeDoesNotReturnAnObjectForSvgProperties() {261driver.get(pages.svgPage);262WebElement svgElement = driver.findElement(By.id("rotate"));263assertThat(svgElement.getDomAttribute("transform")).isEqualTo("rotate(30)");264}265266@Test267void testCanRetrieveTheCurrentValueOfATextFormFieldWithPresetText() {268driver.get(pages.formPage);269WebElement element = driver.findElement(By.id("inputWithText"));270assertThat(element.getDomAttribute("value")).isEqualTo("Example text");271element.sendKeys("[email protected]");272assertThat(element.getDomAttribute("value")).isEqualTo("Example text");273}274275@Test276void testShouldNotReturnTextOfATextArea() {277driver.get(pages.formPage);278WebElement element = driver.findElement(By.id("withText"));279assertThat(element.getDomAttribute("value")).isNull();280}281282@Test283void testShouldReturnNullForNonPresentBooleanAttributes() {284driver.get(pages.booleanAttributes);285WebElement element1 = driver.findElement(By.id("working"));286assertThat(element1.getDomAttribute("required")).isNull();287WebElement element2 = driver.findElement(By.id("wallace"));288assertThat(element2.getDomAttribute("nowrap")).isNull();289}290291@Test292@NotYetImplemented(value = CHROME, reason = "It returns a property")293@NotYetImplemented(EDGE)294@NotYetImplemented(FIREFOX)295public void testShouldReturnEmptyStringForPresentBooleanAttributes() {296driver.get(pages.booleanAttributes);297WebElement element1 = driver.findElement(By.id("emailRequired"));298assertThat(element1.getDomAttribute("required")).isEmpty();299WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));300assertThat(element2.getDomAttribute("required")).isEqualTo("required");301WebElement element3 = driver.findElement(By.id("inputRequired"));302assertThat(element3.getDomAttribute("required")).isEmpty();303WebElement element4 = driver.findElement(By.id("textAreaRequired"));304assertThat(element4.getDomAttribute("required")).isEqualTo("false");305WebElement element5 = driver.findElement(By.id("unwrappable"));306assertThat(element5.getDomAttribute("nowrap")).isEmpty();307}308309@Test310void testMultipleAttributeShouldBeNullWhenNotSet() {311driver.get(pages.selectPage);312WebElement element = driver.findElement(By.id("selectWithoutMultiple"));313assertThat(element.getDomAttribute("multiple")).isNull();314}315316@Test317void testMultipleAttributeShouldBeTrueWhenSet() {318driver.get(pages.selectPage);319WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));320assertThat(element.getDomAttribute("multiple")).isEqualTo("true");321}322323@Test324void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {325driver.get(pages.selectPage);326WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));327assertThat(element.getDomAttribute("multiple")).isEqualTo("true");328}329330@Test331void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {332driver.get(pages.selectPage);333WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));334assertThat(element.getDomAttribute("multiple")).isEqualTo("true");335}336337@Test338void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {339driver.get(pages.selectPage);340WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));341assertThat(element.getDomAttribute("multiple")).isEqualTo("true");342}343344@Test345void shouldTreatContenteditableAsEnumeratedButNotBoolean() {346checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");347}348349@Test350@NotYetImplemented(IE)351@NotYetImplemented(SAFARI)352public void shouldTreatDraggableAsEnumeratedButNotBoolean() {353checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");354}355356private void checkEnumeratedAttribute(String name, String... values) {357List.of(values)358.forEach(359value -> {360driver.get(361appServer.create(362new Page()363.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));364assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEqualTo(value);365});366367driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));368assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isEmpty();369370driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));371assertThat(driver.findElement(By.id("attr")).getDomAttribute(name)).isNull();372}373}374375376