Skip to main content

Element Validations

assertThat vs verifyThat

assertThat (Hard)verifyThat (Soft)
Failure behaviourStops the test immediatelyContinues; collects all failures
Use forBusiness-critical checkpointsMultiple validations in one test
tip

Element assertion and verification chains execute eagerly when the validation condition is selected.


Text Validations

text().isEqualTo()

TextEquals.java
driver.assertThat(locator).text().isEqualTo("Add to cart");
driver.verifyThat(locator).text().isEqualTo("Add to cart");

text().contains()

TextContains.java
driver.assertThat(locator).text().contains("Welcome");

text().equalsIgnoringCaseSensitivity()

TextEqualsIgnoreCase.java
driver.assertThat(locator).text().equalsIgnoringCaseSensitivity("SHAFT ENGINE");

textTrimmed()

Validates text after stripping leading and trailing whitespace:

TextTrimmed.java
driver.assertThat(locator).textTrimmed().isEqualTo("Hello World");

Existence & Visibility

exists() / doesNotExist()

Existence.java
driver.assertThat(locator).exists();
driver.assertThat(locator).doesNotExist();

isVisible() / isHidden()

Visibility.java
driver.assertThat(locator).isVisible();
driver.assertThat(locator).isHidden();

Element State

isEnabled() / isDisabled()

EnabledDisabled.java
driver.assertThat(locator).isEnabled();
driver.assertThat(locator).isDisabled();

isSelected() / isNotSelected()

SelectedNotSelected.java
driver.assertThat(locator).isSelected();
driver.assertThat(locator).isNotSelected();

isChecked() / isNotChecked()

CheckedNotChecked.java
driver.assertThat(locator).isChecked();
driver.assertThat(locator).isNotChecked();

Attribute & CSS Validations

attribute()

Attribute.java
driver.assertThat(locator).attribute("class").contains("active");
driver.assertThat(locator).attribute("href").isEqualTo("https://example.com");
driver.assertThat(locator).attribute("aria-label").contains("Submit");

cssProperty()

CssProperty.java
driver.assertThat(locator).cssProperty("color").contains("rgb(0, 128, 0)");
driver.assertThat(locator).cssProperty("display").isEqualTo("none");

Visual Validation

matchesReferenceImage()

On the first run, SHAFT saves a baseline screenshot. On subsequent runs it compares against the baseline.

VisualValidation.java
driver.assertThat(locator).matchesReferenceImage();
driver.assertThat(locator).doesNotMatchReferenceImage();

Baseline images are saved in src/test/resources/DynamicObjectRepository/. See Visual Testing → for engine options.


Custom Report Messages

Add a business-readable message to any validation:

CustomMessage.java
driver.assertThat(By.id("cart-badge"))
.text().isEqualTo("3")
.withCustomReportMessage("Cart should contain exactly 3 items after adding the product");

Complete Example

src/test/java/tests/CheckoutValidationTest.java
import com.shaft.driver.SHAFT;
import org.openqa.selenium.By;
import org.testng.annotations.*;

public class CheckoutValidationTest {
private SHAFT.GUI.WebDriver driver;

private final By addToCartBtn = By.cssSelector("[data-test='add-to-cart']");
private final By cartBadge = By.className("shopping_cart_badge");
private final By checkoutBtn = By.id("checkout");
private final By orderSummary = By.id("order-summary");
private final By placeOrderBtn = By.id("place-order");

@Test
public void checkoutFlow() {
driver.browser().navigateToURL("https://example.com/products");

// Add item and check badge
driver.element().click(addToCartBtn);
driver.assertThat(cartBadge)
.text().isEqualTo("1")
.withCustomReportMessage("Cart badge must show 1 after adding an item");

// Proceed to checkout
driver.element().click(checkoutBtn);
driver.assertThat().browser().url().contains("/checkout");

// Verify order summary is displayed
driver.assertThat(orderSummary)
.isVisible()
.withCustomReportMessage("Order summary must be visible on checkout page");

// Verify place order button is enabled
driver.assertThat(placeOrderBtn)
.isEnabled()
.withCustomReportMessage("Place Order button must be enabled");
}

@BeforeMethod
public void setup() {
driver = new SHAFT.GUI.WebDriver();
}

@AfterMethod
public void teardown() {
driver.quit();
}
}

See Also