O'Reilly logo

Selenium Testing Tools Cookbook by Unmesh Gundecha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Checking an element's CSS values

Various styles are applied on elements displayed in a web application, so they look neat and become more usable. Developers add these styles using CSS (also known as Cascading Style Sheets). There may be tests that need to verify that correct styles have been applied to the elements. This can be done using WebElement class's getCSSValue() method, which returns the value of a specified style attribute.

In this recipe, we will use the getCSSValue() function to check the style attribute defined for an element.

How to do it...

Let's create a test which reads the CSS width attribute and verifies the value.

@Test public void testElementStyle() { WebElement message = driver.findElement(By.id("message")); String width = message.getCssValue("width"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required