Path: blob/trunk/java/test/org/openqa/selenium/ElementAttributeTest.java
4011 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.support.ui.ExpectedConditions;31import org.openqa.selenium.testing.JupiterTestBase;32import org.openqa.selenium.testing.NotYetImplemented;3334class ElementAttributeTest extends JupiterTestBase {3536@Test37void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {38driver.get(pages.simpleTestPage);39WebElement head = driver.findElement(By.xpath("/html"));40String attribute = head.getAttribute("cheese");41assertThat(attribute).isNull();42}4344@Test45void testShouldReturnNullWhenGettingSrcAttributeOfInvalidImgTag() {46driver.get(pages.simpleTestPage);47WebElement img = driver.findElement(By.id("invalidImgTag"));48String attribute = img.getAttribute("src");49assertThat(attribute).isNull();50}5152@Test53void testShouldReturnAnAbsoluteUrlWhenGettingSrcAttributeOfAValidImgTag() {54driver.get(pages.simpleTestPage);55WebElement img = driver.findElement(By.id("validImgTag"));56String attribute = img.getAttribute("src");57assertThat(attribute).isEqualTo(appServer.whereIs("icon.gif"));58}5960@Test61void testShouldReturnAnAbsoluteUrlWhenGettingHrefAttributeOfAValidAnchorTag() {62driver.get(pages.simpleTestPage);63WebElement img = driver.findElement(By.id("validAnchorTag"));64String attribute = img.getAttribute("href");65assertThat(attribute).isEqualTo(appServer.whereIs("icon.gif"));66}6768@Test69void testShouldReturnEmptyAttributeValuesWhenPresentAndTheValueIsActuallyEmpty() {70driver.get(pages.simpleTestPage);71WebElement body = driver.findElement(By.xpath("//body"));72assertThat(body.getAttribute("style")).isEmpty();73}7475@Test76void testShouldReturnTheValueOfTheDisabledAttributeAsNullIfNotSet() {77driver.get(pages.formPage);78WebElement inputElement = driver.findElement(By.xpath("//input[@id='working']"));79assertThat(inputElement.getAttribute("disabled")).isNull();80assertThat(inputElement.isEnabled()).isTrue();8182WebElement pElement = driver.findElement(By.id("peas"));83assertThat(pElement.getAttribute("disabled")).isNull();84assertThat(pElement.isEnabled()).isTrue();85}8687@Test88void testShouldReturnTheValueOfTheIndexAttributeEvenIfItIsMissing() {89driver.get(pages.formPage);9091WebElement multiSelect = driver.findElement(By.id("multi"));92List<WebElement> options = multiSelect.findElements(By.tagName("option"));93assertThat(options.get(1).getAttribute("index")).isEqualTo("1");94}9596@Test97void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {98driver.get(pages.formPage);99WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));100assertThat(inputElement.isEnabled()).isFalse();101102inputElement = driver.findElement(By.xpath("//input[@id='working']"));103assertThat(inputElement.isEnabled()).isTrue();104}105106@Test107void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {108driver.get(pages.formPage);109WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));110assertThat(disabledTextElement1.isEnabled()).isFalse();111112WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));113assertThat(disabledTextElement2.isEnabled()).isFalse();114115WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));116assertThat(disabledSubmitElement.isEnabled()).isFalse();117}118119@Test120@NotYetImplemented(SAFARI)121public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {122driver.get(pages.formPage);123WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));124assertThatExceptionOfType(InvalidElementStateException.class)125.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));126assertThat(disabledTextElement1.getText()).isEmpty();127128WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));129assertThatExceptionOfType(InvalidElementStateException.class)130.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));131assertThat(disabledTextElement2.getText()).isEmpty();132}133134@Test135void testShouldIndicateWhenATextAreaIsDisabled() {136driver.get(pages.formPage);137WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));138assertThat(textArea.isEnabled()).isFalse();139}140141@Test142void testShouldIndicateWhenASelectIsDisabled() {143driver.get(pages.formPage);144145WebElement enabled = driver.findElement(By.name("selectomatic"));146WebElement disabled = driver.findElement(By.name("no-select"));147148assertThat(enabled.isEnabled()).isTrue();149assertThat(disabled.isEnabled()).isFalse();150}151152@Test153void testShouldReturnTheValueOfCheckedForACheckboxOnlyIfItIsChecked() {154driver.get(pages.formPage);155WebElement checkbox = driver.findElement(By.xpath("//input[@id='checky']"));156assertThat(checkbox.getAttribute("checked")).isNull();157checkbox.click();158assertThat(checkbox.getAttribute("checked")).isEqualTo("true");159}160161@Test162void testShouldOnlyReturnTheValueOfSelectedForRadioButtonsIfItIsSet() {163driver.get(pages.formPage);164WebElement neverSelected = driver.findElement(By.id("cheese"));165WebElement initiallyNotSelected = driver.findElement(By.id("peas"));166WebElement initiallySelected = driver.findElement(By.id("cheese_and_peas"));167168assertThat(neverSelected.getAttribute("selected")).isNull();169assertThat(initiallyNotSelected.getAttribute("selected")).isNull();170assertThat(initiallySelected.getAttribute("selected")).isEqualTo("true");171172initiallyNotSelected.click();173assertThat(neverSelected.getAttribute("selected")).isNull();174assertThat(initiallyNotSelected.getAttribute("selected")).isEqualTo("true");175assertThat(initiallySelected.getAttribute("selected")).isNull();176}177178@Test179void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {180driver.get(pages.formPage);181WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));182List<WebElement> options = selectBox.findElements(By.tagName("option"));183WebElement one = options.get(0);184WebElement two = options.get(1);185assertThat(one.isSelected()).isTrue();186assertThat(two.isSelected()).isFalse();187assertThat(one.getAttribute("selected")).isEqualTo("true");188assertThat(two.getAttribute("selected")).isNull();189}190191@Test192void testShouldReturnValueOfClassAttributeOfAnElement() {193driver.get(pages.xhtmlTestPage);194195WebElement heading = driver.findElement(By.xpath("//h1"));196String className = heading.getAttribute("class");197198assertThat(className).isEqualTo("header");199}200201@Test202void testShouldReturnTheContentsOfATextAreaAsItsValue() {203driver.get(pages.formPage);204205String value = driver.findElement(By.id("withText")).getAttribute("value");206207assertThat(value).isEqualTo("Example text");208}209210@Test211void testShouldReturnInnerHtml() {212driver.get(pages.simpleTestPage);213214String html = driver.findElement(By.id("wrappingtext")).getAttribute("innerHTML");215assertThat(html).contains("<tbody>");216}217218@Test219void testShouldTreatReadonlyAsAValue() {220driver.get(pages.formPage);221222WebElement element = driver.findElement(By.name("readonly"));223String readonly = element.getAttribute("readonly");224225assertThat(readonly).isNotNull();226227WebElement textInput = driver.findElement(By.name("x"));228String notReadonly = textInput.getAttribute("readonly");229230assertThat(readonly).isNotEqualTo(notReadonly);231}232233@Test234void testShouldReturnHiddenTextForTextContentAttribute() {235driver.get(pages.simpleTestPage);236237WebElement element = driver.findElement(By.id("hiddenline"));238String textContent = element.getAttribute("textContent");239240assertThat(textContent).isEqualTo("A hidden line of text");241}242243@Test244void testShouldGetNumericAttribute() {245driver.get(pages.formPage);246WebElement element = driver.findElement(By.id("withText"));247assertThat(element.getAttribute("rows")).isEqualTo("5");248}249250@Test251void testCanReturnATextApproximationOfTheStyleAttribute() {252driver.get(pages.javascriptPage);253254String style = driver.findElement(By.id("red-item")).getAttribute("style");255256assertThat(style).containsIgnoringCase("background-color");257}258259@Test260void testShouldCorrectlyReportValueOfColspan() {261driver.get(pages.tables);262263WebElement th1 = driver.findElement(By.id("th1"));264WebElement td2 = driver.findElement(By.id("td2"));265266assertThat(th1.getAttribute("id")).isEqualTo("th1");267assertThat(th1.getAttribute("colspan")).isEqualTo("3");268269assertThat(td2.getAttribute("id")).isEqualTo("td2");270assertThat(td2.getAttribute("colspan")).isEqualTo("2");271}272273// This is a test-case re-creating issue 900.274@Test275void testShouldReturnValueOfOnClickAttribute() {276driver.get(pages.javascriptPage);277278WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));279280String onClickValue = mouseclickDiv.getAttribute("onclick");281String expectedOnClickValue = "displayMessage('mouse click');";282assertThat(onClickValue)283.as("Javascript code")284.isIn(285"javascript:" + expectedOnClickValue, // Non-IE286"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE287"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE288289WebElement mousedownDiv = driver.findElement(By.id("mousedown"));290assertThat(mousedownDiv.getAttribute("onclick")).isNull();291}292293@Test294void testGetAttributeDoesNotReturnAnObjectForSvgProperties() {295driver.get(pages.svgPage);296WebElement svgElement = driver.findElement(By.id("rotate"));297assertThat(svgElement.getAttribute("transform")).isEqualTo("rotate(30)");298}299300@Test301void testCanRetrieveTheCurrentValueOfATextFormField_textInput() {302driver.get(pages.formPage);303WebElement element = driver.findElement(By.id("working"));304assertThat(element.getAttribute("value")).isEmpty();305element.sendKeys("hello world");306shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));307}308309@Test310void testCanRetrieveTheCurrentValueOfATextFormField_emailInput() {311driver.get(pages.formPage);312WebElement element = driver.findElement(By.id("email"));313assertThat(element.getAttribute("value")).isEmpty();314element.sendKeys("[email protected]");315shortWait.until(ExpectedConditions.attributeToBe(element, "value", "[email protected]"));316}317318@Test319void testCanRetrieveTheCurrentValueOfATextFormField_textArea() {320driver.get(pages.formPage);321WebElement element = driver.findElement(By.id("emptyTextArea"));322assertThat(element.getAttribute("value")).isEmpty();323element.sendKeys("hello world");324shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));325}326327@Test328void testShouldReturnNullForNonPresentBooleanAttributes() {329driver.get(pages.booleanAttributes);330WebElement element1 = driver.findElement(By.id("working"));331assertThat(element1.getAttribute("required")).isNull();332WebElement element2 = driver.findElement(By.id("wallace"));333assertThat(element2.getAttribute("nowrap")).isNull();334}335336@Test337void testShouldReturnTrueForPresentBooleanAttributes() {338driver.get(pages.booleanAttributes);339WebElement element1 = driver.findElement(By.id("emailRequired"));340assertThat(element1.getAttribute("required")).isEqualTo("true");341WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));342assertThat(element2.getAttribute("required")).isEqualTo("true");343WebElement element3 = driver.findElement(By.id("inputRequired"));344assertThat(element3.getAttribute("required")).isEqualTo("true");345WebElement element4 = driver.findElement(By.id("textAreaRequired"));346assertThat(element4.getAttribute("required")).isEqualTo("true");347WebElement element5 = driver.findElement(By.id("unwrappable"));348assertThat(element5.getAttribute("nowrap")).isEqualTo("true");349}350351@Test352void testMultipleAttributeShouldBeNullWhenNotSet() {353driver.get(pages.selectPage);354WebElement element = driver.findElement(By.id("selectWithoutMultiple"));355assertThat(element.getAttribute("multiple")).isNull();356}357358@Test359void testMultipleAttributeShouldBeTrueWhenSet() {360driver.get(pages.selectPage);361WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));362assertThat(element.getAttribute("multiple")).isEqualTo("true");363}364365@Test366void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {367driver.get(pages.selectPage);368WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));369assertThat(element.getAttribute("multiple")).isEqualTo("true");370}371372@Test373void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {374driver.get(pages.selectPage);375WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));376assertThat(element.getAttribute("multiple")).isEqualTo("true");377}378379@Test380void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {381driver.get(pages.selectPage);382WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));383assertThat(element.getAttribute("multiple")).isEqualTo("true");384}385386@Test387void testGetAttributeOfUserDefinedProperty() {388driver.get(pages.userDefinedProperty);389WebElement element = driver.findElement(By.id("d"));390assertThat(element.getAttribute("dynamicProperty")).isEqualTo("sampleValue");391}392393@Test394void shouldTreatContenteditableAsEnumeratedButNotBoolean() {395checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");396}397398@Test399@NotYetImplemented(IE)400@NotYetImplemented(CHROME)401@NotYetImplemented(EDGE)402@NotYetImplemented(FIREFOX)403@NotYetImplemented(SAFARI)404public void shouldTreatDraggableAsEnumeratedButNotBoolean() {405checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");406}407408private void checkEnumeratedAttribute(String name, String... values) {409List.of(values)410.forEach(411value -> {412driver.get(413appServer.create(414new Page()415.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));416assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEqualTo(value);417});418419driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));420assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEmpty();421422driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));423assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isNull();424}425}426427428