Checking an element's attribute values
Developers configure various attributes of elements displayed on the web page during design or at runtime to control the behavior or style of elements when they are displayed in the browser. For example, the <input>
element can be set to read-only by setting the readonly
attribute.
There will be tests that need to verify that element attributes are set correctly. We can retrieve and verify an element's attribute by using the getAttribute()
method of the WebElement
class.
In this recipe, will check the attribute value of an element by using the getAttribute()
method.
How to do it...
Create a test which locates an element and check its attribute value as follows:
@Test public void testElementAttribute() { WebElement message = driver.findElement(By.id("message")); assertEquals("justify",message.getAttribute("align")); }
How it works...
By passing the name of the attribute to the getAttribute()
method, it returns the value of the attribute back to the...