Skip to content

Commit

Permalink
Fix HtmlUnitWebElement.getCssValue
Browse files Browse the repository at this point in the history
Return correct value for colors.

Signed-off-by: Luke Inman-Semerau <[email protected]>
  • Loading branch information
asashour authored and lukeis committed Aug 22, 2015
1 parent c8bc2ad commit c181d39
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public void authenticateUsing(Credentials credentials) {
public void setCredentials(Credentials credentials) {
}


@Override
public void handleAlert(Page page, String message) {
Queue<String> queue = queues.get(page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.concurrent.Callable;

import net.sourceforge.htmlunit.corejs.javascript.Undefined;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.ElementNotVisibleException;
Expand All @@ -45,6 +47,8 @@
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.internal.WrapsElement;
import org.openqa.selenium.support.Color;
import org.openqa.selenium.support.Colors;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;

Expand All @@ -69,8 +73,6 @@
import com.google.common.base.Strings;
import com.google.common.base.Throwables;

import net.sourceforge.htmlunit.corejs.javascript.Undefined;


public class HtmlUnitWebElement implements WrapsDriver,
FindsById, FindsByLinkText, FindsByXPath, FindsByTagName,
Expand Down Expand Up @@ -156,7 +158,6 @@ public void click() {
new HtmlUnitWebElement(parent, referencedElement).click();
}
}

}

@Override
Expand Down Expand Up @@ -864,7 +865,33 @@ protected void assertElementNotStale() {
public String getCssValue(String propertyName) {
assertElementNotStale();

return getEffectiveStyle((HtmlElement) element, propertyName);
String style = getEffectiveStyle((HtmlElement) element, propertyName);
return getColor(style);
}

private static String getColor(String name) {
if ("null".equals(name)) {
return "transparent";
}
if (name.startsWith("rgb(")) {
return Color.fromString(name).asRgba();
}

Colors colors = getColorsOf(name);
if (colors != null) {
return colors.getColorValue().asRgba();
}
return name;
}

private static Colors getColorsOf(String name) {
name = name.toUpperCase();
for (Colors colors : Colors.values()) {
if (colors.name().equals(name)) {
return colors;
}
}
return null;
}

private String getEffectiveStyle(HtmlElement htmlElement, String propertyName) {
Expand Down
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/htmlunit/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ java_library(name = "htmlunit",
"//java/client/src/org/openqa/selenium:webdriver-api",
"//java/client/src/org/openqa/selenium/interactions",
"//java/client/src/org/openqa/selenium/remote:common",
"//java/client/src/org/openqa/selenium/support:support",
"//third_party/java/htmlunit",
])
15 changes: 5 additions & 10 deletions java/client/test/org/openqa/selenium/CssValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@

package org.openqa.selenium;

import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.JavascriptEnabled;
import org.openqa.selenium.testing.NotYetImplemented;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;

import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.JavascriptEnabled;

public class CssValueTest extends JUnit4TestBase {

@JavascriptEnabled
@Ignore(MARIONETTE)
@NotYetImplemented(HTMLUNIT)
@Test
public void testShouldPickUpStyleOfAnElement() {
driver.get(pages.javascriptPage);
Expand All @@ -52,7 +49,6 @@ public void testShouldPickUpStyleOfAnElement() {

@JavascriptEnabled
@Ignore(MARIONETTE)
@NotYetImplemented(HTMLUNIT)
@Test
public void testGetCssValueShouldReturnStandardizedColour() {
driver.get(pages.colorPage);
Expand All @@ -68,7 +64,6 @@ public void testGetCssValueShouldReturnStandardizedColour() {
}

@JavascriptEnabled
@NotYetImplemented(HTMLUNIT)
@Test
public void testShouldAllowInheritedStylesToBeUsed() {
driver.get(pages.javascriptPage);
Expand Down

0 comments on commit c181d39

Please sign in to comment.