Answer
Verifying CSS Properties in Selenium
getCssValue(propertyName) returns the computed CSS value of any property on an element. This is used to verify colors, fonts, borders, visibility, and other visual states.
Get Color Value
Java
WebElement submitBtn = driver.findElement(By.id("submitBtn"));
// Returns rgba format: "rgba(255, 87, 34, 1)"
String bgColor = submitBtn.getCssValue("background-color");
System.out.println("Background color: " + bgColor);
// Verify specific color
Assert.assertEquals(bgColor, "rgba(255, 87, 34, 1)", "Button color mismatch");
Convert RGBA to HEX (Helper Method)
Java
import org.openqa.selenium.support.Color;
public String rgbaToHex(String rgba) {
return Color.fromString(rgba).asHex().toUpperCase();
}
// Usage
String rgba = submitBtn.getCssValue("background-color");
String hex = rgbaToHex(rgba); // "#FF5722"
Assert.assertEquals(hex, "#FF5722");
Common CSS Properties to Verify
Java
WebElement element = driver.findElement(By.cssSelector(".error-message"));
// Text color
String color = element.getCssValue("color");
// Font size
String fontSize = element.getCssValue("font-size"); // "16px"
// Font family
String fontFamily = element.getCssValue("font-family");
// Font weight (bold = "700")
String fontWeight = element.getCssValue("font-weight");
// Border
String border = element.getCssValue("border");
// Visibility
String visibility = element.getCssValue("visibility"); // "visible" or "hidden"
// Display
String display = element.getCssValue("display"); // "block", "none", "flex"
// Opacity
String opacity = element.getCssValue("opacity"); // "1" or "0.5"
// Text decoration (underline)
String textDecoration = element.getCssValue("text-decoration");
// Cursor type
String cursor = element.getCssValue("cursor"); // "pointer", "not-allowed"
Real-World Validation Example
Java
@Test
public void verifyErrorMessageStyling() {
// Trigger error
driver.findElement(By.id("submitBtn")).click();
WebElement errorMsg = driver.findElement(By.cssSelector(".error-message"));
// Verify red color
String rgba = errorMsg.getCssValue("color");
String hex = Color.fromString(rgba).asHex();
Assert.assertEquals(hex.toUpperCase(), "#FF0000", "Error text should be red");
// Verify bold
Assert.assertEquals(errorMsg.getCssValue("font-weight"), "700", "Error should be bold");
// Verify visible
Assert.assertEquals(errorMsg.getCssValue("display"), "block", "Error should be visible");
}
Verify Hover State
Java
WebElement link = driver.findElement(By.cssSelector("nav a"));
// Before hover
String normalColor = link.getCssValue("color");
// Hover
new Actions(driver).moveToElement(link).perform();
// After hover (may need short wait for CSS transition)
Thread.sleep(300);
String hoverColor = link.getCssValue("color");
Assert.assertNotEquals(normalColor, hoverColor, "Color should change on hover");
Key Notes
- ✓Colors are always returned as
rgba(r, g, b, a)format regardless of how CSS defines them - ✓Use
org.openqa.selenium.support.Color.fromString(rgba).asHex()for hex comparison - ✓CSS transitions may need a short wait before reading the final value
- ✓
getCssValue("display")is more reliable thanisDisplayed()for hidden elements
