While writing selenium tests we often come across verification points which deal with verifying attributes/properties of an html element. Consider following example
WebElement webElement = driver.findElement(By.id("testEElement"));
assert webElement.getAttribute("class").equalsIgnoreCase("highlight")
This example seems seemingly simple but there is catch. What if attribute class is missing?
In this case, evaluation of second statement will throw null pointer exception. Hence a better approach would be to compare String "hightlight" with attribute of webelement. that is -
assert "highlight".equalsIgnoreCase(webElement.getAttribute("class"))
And this will return false if element attribute is missing from element because of an application defect.
WebElement webElement = driver.findElement(By.id("testEElement"));
assert webElement.getAttribute("class").equalsIgnoreCase("highlight")
This example seems seemingly simple but there is catch. What if attribute class is missing?
In this case, evaluation of second statement will throw null pointer exception. Hence a better approach would be to compare String "hightlight" with attribute of webelement. that is -
assert "highlight".equalsIgnoreCase(webElement.getAttribute("class"))
And this will return false if element attribute is missing from element because of an application defect.
Another idea is that can we have precheck before asserting like whether it is not null?
ReplyDeleteprecheck how?
ReplyDeleteI think tulsi.tester meant to say Assert.assertNotNull(webElement.getAttribute("class"))
Delete