Path: blob/trunk/java/test/org/openqa/selenium/ElementAttributeTest.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 java.util.logging.Logger;30import org.junit.jupiter.api.Test;31import org.openqa.selenium.environment.webserver.Page;32import org.openqa.selenium.support.ui.ExpectedConditions;33import org.openqa.selenium.testing.JupiterTestBase;34import org.openqa.selenium.testing.NotYetImplemented;3536class ElementAttributeTest extends JupiterTestBase {3738private static final Logger LOG = Logger.getLogger(ElementAttributeTest.class.getName());3940@Test41void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {42driver.get(pages.simpleTestPage);43WebElement head = driver.findElement(By.xpath("/html"));44String attribute = head.getAttribute("cheese");45assertThat(attribute).isNull();46}4748@Test49void testShouldReturnNullWhenGettingSrcAttributeOfInvalidImgTag() {50driver.get(pages.simpleTestPage);51WebElement img = driver.findElement(By.id("invalidImgTag"));52String attribute = img.getAttribute("src");53assertThat(attribute).isNull();54}5556@Test57void testShouldReturnAnAbsoluteUrlWhenGettingSrcAttributeOfAValidImgTag() {58driver.get(pages.simpleTestPage);59WebElement img = driver.findElement(By.id("validImgTag"));60String attribute = img.getAttribute("src");61assertThat(attribute).isEqualTo(appServer.whereIs("icon.gif"));62}6364@Test65void testShouldReturnAnAbsoluteUrlWhenGettingHrefAttributeOfAValidAnchorTag() {66driver.get(pages.simpleTestPage);67WebElement img = driver.findElement(By.id("validAnchorTag"));68String attribute = img.getAttribute("href");69assertThat(attribute).isEqualTo(appServer.whereIs("icon.gif"));70}7172@Test73void testShouldReturnEmptyAttributeValuesWhenPresentAndTheValueIsActuallyEmpty() {74driver.get(pages.simpleTestPage);75WebElement body = driver.findElement(By.xpath("//body"));76assertThat(body.getAttribute("style")).isEmpty();77}7879@Test80void testShouldReturnTheValueOfTheDisabledAttributeAsNullIfNotSet() {81driver.get(pages.formPage);82WebElement inputElement = driver.findElement(By.xpath("//input[@id='working']"));83assertThat(inputElement.getAttribute("disabled")).isNull();84assertThat(inputElement.isEnabled()).isTrue();8586WebElement pElement = driver.findElement(By.id("peas"));87assertThat(pElement.getAttribute("disabled")).isNull();88assertThat(pElement.isEnabled()).isTrue();89}9091@Test92void testShouldReturnTheValueOfTheIndexAttributeEvenIfItIsMissing() {93driver.get(pages.formPage);9495WebElement multiSelect = driver.findElement(By.id("multi"));96List<WebElement> options = multiSelect.findElements(By.tagName("option"));97assertThat(options.get(1).getAttribute("index")).isEqualTo("1");98}99100@Test101void testShouldIndicateTheElementsThatAreDisabledAreNotEnabled() {102driver.get(pages.formPage);103WebElement inputElement = driver.findElement(By.xpath("//input[@id='notWorking']"));104assertThat(inputElement.isEnabled()).isFalse();105106inputElement = driver.findElement(By.xpath("//input[@id='working']"));107assertThat(inputElement.isEnabled()).isTrue();108}109110@Test111void testElementsShouldBeDisabledIfTheyAreDisabledUsingRandomDisabledStrings() {112driver.get(pages.formPage);113WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));114assertThat(disabledTextElement1.isEnabled()).isFalse();115116WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));117assertThat(disabledTextElement2.isEnabled()).isFalse();118119WebElement disabledSubmitElement = driver.findElement(By.id("disabledSubmitElement"));120assertThat(disabledSubmitElement.isEnabled()).isFalse();121}122123@Test124@NotYetImplemented(SAFARI)125public void testShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisabledStrings() {126driver.get(pages.formPage);127WebElement disabledTextElement1 = driver.findElement(By.id("disabledTextElement1"));128assertThatExceptionOfType(InvalidElementStateException.class)129.isThrownBy(() -> disabledTextElement1.sendKeys("foo"));130assertThat(disabledTextElement1.getText()).isEmpty();131132WebElement disabledTextElement2 = driver.findElement(By.id("disabledTextElement2"));133assertThatExceptionOfType(InvalidElementStateException.class)134.isThrownBy(() -> disabledTextElement2.sendKeys("bar"));135assertThat(disabledTextElement2.getText()).isEmpty();136}137138@Test139void testShouldIndicateWhenATextAreaIsDisabled() {140driver.get(pages.formPage);141WebElement textArea = driver.findElement(By.xpath("//textarea[@id='notWorkingArea']"));142assertThat(textArea.isEnabled()).isFalse();143}144145@Test146void testShouldIndicateWhenASelectIsDisabled() {147driver.get(pages.formPage);148149WebElement enabled = driver.findElement(By.name("selectomatic"));150WebElement disabled = driver.findElement(By.name("no-select"));151152assertThat(enabled.isEnabled()).isTrue();153assertThat(disabled.isEnabled()).isFalse();154}155156@Test157void testShouldReturnTheValueOfCheckedForACheckboxOnlyIfItIsChecked() {158driver.get(pages.formPage);159WebElement checkbox = driver.findElement(By.xpath("//input[@id='checky']"));160assertThat(checkbox.getAttribute("checked")).isNull();161checkbox.click();162assertThat(checkbox.getAttribute("checked")).isEqualTo("true");163}164165@Test166void testShouldOnlyReturnTheValueOfSelectedForRadioButtonsIfItIsSet() {167driver.get(pages.formPage);168WebElement neverSelected = driver.findElement(By.id("cheese"));169WebElement initiallyNotSelected = driver.findElement(By.id("peas"));170WebElement initiallySelected = driver.findElement(By.id("cheese_and_peas"));171172assertThat(neverSelected.getAttribute("selected")).isNull();173assertThat(initiallyNotSelected.getAttribute("selected")).isNull();174assertThat(initiallySelected.getAttribute("selected")).isEqualTo("true");175176initiallyNotSelected.click();177assertThat(neverSelected.getAttribute("selected")).isNull();178assertThat(initiallyNotSelected.getAttribute("selected")).isEqualTo("true");179assertThat(initiallySelected.getAttribute("selected")).isNull();180}181182@Test183void testShouldReturnTheValueOfSelectedForOptionsOnlyIfTheyAreSelected() {184driver.get(pages.formPage);185WebElement selectBox = driver.findElement(By.xpath("//select[@name='selectomatic']"));186List<WebElement> options = selectBox.findElements(By.tagName("option"));187WebElement one = options.get(0);188WebElement two = options.get(1);189assertThat(one.isSelected()).isTrue();190assertThat(two.isSelected()).isFalse();191assertThat(one.getAttribute("selected")).isEqualTo("true");192assertThat(two.getAttribute("selected")).isNull();193}194195@Test196void testShouldReturnValueOfClassAttributeOfAnElement() {197driver.get(pages.xhtmlTestPage);198199WebElement heading = driver.findElement(By.xpath("//h1"));200String className = heading.getAttribute("class");201202assertThat(className).isEqualTo("header");203}204205@Test206void testShouldReturnTheContentsOfATextAreaAsItsValue() {207driver.get(pages.formPage);208209String value = driver.findElement(By.id("withText")).getAttribute("value");210211assertThat(value).isEqualTo("Example text");212}213214@Test215void testShouldReturnInnerHtml() {216driver.get(pages.simpleTestPage);217218String html = driver.findElement(By.id("wrappingtext")).getAttribute("innerHTML");219assertThat(html).contains("<tbody>");220}221222@Test223void testShouldTreatReadonlyAsAValue() {224driver.get(pages.formPage);225226WebElement element = driver.findElement(By.name("readonly"));227String readonly = element.getAttribute("readonly");228229assertThat(readonly).isNotNull();230231WebElement textInput = driver.findElement(By.name("x"));232String notReadonly = textInput.getAttribute("readonly");233234assertThat(readonly).isNotEqualTo(notReadonly);235}236237@Test238void testShouldReturnHiddenTextForTextContentAttribute() {239driver.get(pages.simpleTestPage);240241WebElement element = driver.findElement(By.id("hiddenline"));242String textContent = element.getAttribute("textContent");243244assertThat(textContent).isEqualTo("A hidden line of text");245}246247@Test248void testShouldGetNumericAttribute() {249driver.get(pages.formPage);250WebElement element = driver.findElement(By.id("withText"));251assertThat(element.getAttribute("rows")).isEqualTo("5");252}253254@Test255void testCanReturnATextApproximationOfTheStyleAttribute() {256driver.get(pages.javascriptPage);257258String style = driver.findElement(By.id("red-item")).getAttribute("style");259260assertThat(style.toLowerCase().contains("background-color")).isTrue();261}262263@Test264void testShouldCorrectlyReportValueOfColspan() {265driver.get(pages.tables);266267try {268Thread.sleep(1000);269} catch (InterruptedException e) {270LOG.severe("Error during execution: " + e.getMessage());271}272273WebElement th1 = driver.findElement(By.id("th1"));274WebElement td2 = driver.findElement(By.id("td2"));275276assertThat(th1.getAttribute("id")).isEqualTo("th1");277assertThat(th1.getAttribute("colspan")).isEqualTo("3");278279assertThat(td2.getAttribute("id")).isEqualTo("td2");280assertThat(td2.getAttribute("colspan")).isEqualTo("2");281}282283// This is a test-case re-creating issue 900.284@Test285void testShouldReturnValueOfOnClickAttribute() {286driver.get(pages.javascriptPage);287288WebElement mouseclickDiv = driver.findElement(By.id("mouseclick"));289290String onClickValue = mouseclickDiv.getAttribute("onclick");291String expectedOnClickValue = "displayMessage('mouse click');";292assertThat(onClickValue)293.as("Javascript code")294.isIn(295"javascript:" + expectedOnClickValue, // Non-IE296"function anonymous()\n{\n" + expectedOnClickValue + "\n}", // IE297"function onclick()\n{\n" + expectedOnClickValue + "\n}"); // IE298299WebElement mousedownDiv = driver.findElement(By.id("mousedown"));300assertThat(mousedownDiv.getAttribute("onclick")).isNull();301}302303@Test304void testGetAttributeDoesNotReturnAnObjectForSvgProperties() {305driver.get(pages.svgPage);306WebElement svgElement = driver.findElement(By.id("rotate"));307assertThat(svgElement.getAttribute("transform")).isEqualTo("rotate(30)");308}309310@Test311void testCanRetrieveTheCurrentValueOfATextFormField_textInput() {312driver.get(pages.formPage);313WebElement element = driver.findElement(By.id("working"));314assertThat(element.getAttribute("value")).isEmpty();315element.sendKeys("hello world");316shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));317}318319@Test320void testCanRetrieveTheCurrentValueOfATextFormField_emailInput() {321driver.get(pages.formPage);322WebElement element = driver.findElement(By.id("email"));323assertThat(element.getAttribute("value")).isEmpty();324element.sendKeys("[email protected]");325shortWait.until(ExpectedConditions.attributeToBe(element, "value", "[email protected]"));326}327328@Test329void testCanRetrieveTheCurrentValueOfATextFormField_textArea() {330driver.get(pages.formPage);331WebElement element = driver.findElement(By.id("emptyTextArea"));332assertThat(element.getAttribute("value")).isEmpty();333element.sendKeys("hello world");334shortWait.until(ExpectedConditions.attributeToBe(element, "value", "hello world"));335}336337@Test338void testShouldReturnNullForNonPresentBooleanAttributes() {339driver.get(pages.booleanAttributes);340WebElement element1 = driver.findElement(By.id("working"));341assertThat(element1.getAttribute("required")).isNull();342WebElement element2 = driver.findElement(By.id("wallace"));343assertThat(element2.getAttribute("nowrap")).isNull();344}345346@Test347void testShouldReturnTrueForPresentBooleanAttributes() {348driver.get(pages.booleanAttributes);349WebElement element1 = driver.findElement(By.id("emailRequired"));350assertThat(element1.getAttribute("required")).isEqualTo("true");351WebElement element2 = driver.findElement(By.id("emptyTextAreaRequired"));352assertThat(element2.getAttribute("required")).isEqualTo("true");353WebElement element3 = driver.findElement(By.id("inputRequired"));354assertThat(element3.getAttribute("required")).isEqualTo("true");355WebElement element4 = driver.findElement(By.id("textAreaRequired"));356assertThat(element4.getAttribute("required")).isEqualTo("true");357WebElement element5 = driver.findElement(By.id("unwrappable"));358assertThat(element5.getAttribute("nowrap")).isEqualTo("true");359}360361@Test362void testMultipleAttributeShouldBeNullWhenNotSet() {363driver.get(pages.selectPage);364WebElement element = driver.findElement(By.id("selectWithoutMultiple"));365assertThat(element.getAttribute("multiple")).isNull();366}367368@Test369void testMultipleAttributeShouldBeTrueWhenSet() {370driver.get(pages.selectPage);371WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));372assertThat(element.getAttribute("multiple")).isEqualTo("true");373}374375@Test376void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsBlank() {377driver.get(pages.selectPage);378WebElement element = driver.findElement(By.id("selectWithEmptyStringMultiple"));379assertThat(element.getAttribute("multiple")).isEqualTo("true");380}381382@Test383void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithoutAValue() {384driver.get(pages.selectPage);385WebElement element = driver.findElement(By.id("selectWithMultipleWithoutValue"));386assertThat(element.getAttribute("multiple")).isEqualTo("true");387}388389@Test390void testMultipleAttributeShouldBeTrueWhenSelectHasMultipleWithValueAsSomethingElse() {391driver.get(pages.selectPage);392WebElement element = driver.findElement(By.id("selectWithRandomMultipleValue"));393assertThat(element.getAttribute("multiple")).isEqualTo("true");394}395396@Test397void testGetAttributeOfUserDefinedProperty() {398driver.get(pages.userDefinedProperty);399WebElement element = driver.findElement(By.id("d"));400assertThat(element.getAttribute("dynamicProperty")).isEqualTo("sampleValue");401}402403@Test404void shouldTreatContenteditableAsEnumeratedButNotBoolean() {405checkEnumeratedAttribute("contenteditable", "true", "false", "yes", "no", "", "blabla");406}407408@Test409@NotYetImplemented(IE)410@NotYetImplemented(CHROME)411@NotYetImplemented(EDGE)412@NotYetImplemented(FIREFOX)413@NotYetImplemented(SAFARI)414public void shouldTreatDraggableAsEnumeratedButNotBoolean() {415checkEnumeratedAttribute("draggable", "true", "false", "yes", "no", "", "blabla");416}417418private void checkEnumeratedAttribute(String name, String... values) {419asList(values)420.forEach(421value -> {422driver.get(423appServer.create(424new Page()425.withBody(String.format("<div id=\"attr\" %s=\"%s\">", name, value))));426assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEqualTo(value);427});428429driver.get(appServer.create(new Page().withBody(String.format("<div id=\"attr\" %s>", name))));430assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isEmpty();431432driver.get(appServer.create(new Page().withBody("<div id=\"attr\">")));433assertThat(driver.findElement(By.id("attr")).getAttribute(name)).isNull();434}435}436437438