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"); assertEquals("150px",width); }
How it works...
By passing the name of CSS attribute to the getCSSValue()
method, it returns the value of the CSS attribute. In this example, we are checking that the width attribute of...