### Question:
AttributeMatchesCondition extends Condition<WebElement> { @Override public boolean apply(final WebElement element) { try { String attributeValue = element.getAttribute(attribute); Pattern pattern = flags == 0x00 ? Pattern.compile(regex) : Pattern.compile(regex, flags); Matcher matcher = pattern.matcher(attributeValue); return matcher.matches(); } catch (NullPointerException | PatternSyntaxException | WebDriverException e) { return false; } } AttributeMatchesCondition(final String attribute, final String regex);  AttributeMatchesCondition(final String attribute, final String regex, final int flags); @Override boolean apply(final WebElement element); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }### Answer:
@Test public void shouldMatchConditionWhenAttributeMatchesRegex() { when(mockElement.getAttribute("class")).thenReturn("myclass modifier1 modifier2"); AttributeMatchesCondition condition = new AttributeMatchesCondition("class", "(.*)modifier1(.*)"); assertThat(condition.apply(mockElement), equalTo(true)); }
@Test public void shouldNotMatchConditionWhenAttributeDoesNotMatchRegex() { when(mockElement.getAttribute("clas")).thenReturn("myclass modifier3 modifier4"); AttributeMatchesCondition condition = new AttributeMatchesCondition("class", "(.*)modifier1(.*)"); assertThat(condition.apply(mockElement), equalTo(false)); }
@Test public void shouldHandleNullReturn() { when(mockElement.getAttribute("class")).thenReturn(null); AttributeMatchesCondition condition = new AttributeMatchesCondition("class", "(.*)modifier1(.*)"); assertThat(condition.apply(mockElement), equalTo(false)); }
@Test public void shouldHandleExceptionWhenElementHasStaleReference() { when(mockElement.getAttribute("class")).thenThrow(new StaleElementReferenceException("stale")); AttributeMatchesCondition condition = new AttributeMatchesCondition("class", "(.*)modifier1(.*)"); assertThat(condition.apply(mockElement), equalTo(false)); }