Beginner Chapter 7 · 9 min read

Assertions & Verification

Verify page state with assertions — check text, attributes, visibility, element state, page title, URL, and element counts.

Assertions & Verification

Assertions are the proof that your tests actually verify something. Without them, your tests just exercise code without confirming correctness. Both TestNG (Java) and pytest (Python) provide powerful assertion mechanisms.

What to Assert

  • Text content — Element text, page title, alert text
  • Attributes — Value, href, class, data-* attributes
  • Visibility/state — isDisplayed, isEnabled, isSelected
  • URL — Current URL after navigation
  • Element count — Number of matching elements

Soft Assertions

Regular assertions stop the test on first failure. Soft assertions collect all failures and report them at the end — useful when you want to check multiple things in one test.

AssertionsTest.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;
import java.util.List;

public class AssertionsTest {

    WebDriver driver;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(
            java.time.Duration.ofSeconds(10));
        driver.get("https://www.selenium.dev/selenium/web/web-form.html");
    }

    @Test
    public void testTextAssertions() {
        String title = driver.getTitle();
        Assert.assertEquals(title, "Web form");
        Assert.assertTrue(title.contains("form"));
        Assert.assertFalse(title.isEmpty());
    }

    @Test
    public void testElementStateAssertions() {
        WebElement input = driver.findElement(By.name("my-text"));
        Assert.assertTrue(input.isDisplayed(), "Input should be visible");
        Assert.assertTrue(input.isEnabled(), "Input should be enabled");

        WebElement disabled = driver.findElement(By.name("my-disabled"));
        Assert.assertFalse(disabled.isEnabled(), "Should be disabled");
    }

    @Test
    public void testAttributeAssertions() {
        WebElement input = driver.findElement(By.name("my-text"));
        Assert.assertEquals(input.getAttribute("type"), "text");
        Assert.assertNotNull(input.getAttribute("name"));
        Assert.assertEquals(input.getAttribute("myprop"), "myvalue");
    }

    @Test
    public void testUrlAssertions() {
        String url = driver.getCurrentUrl();
        Assert.assertTrue(url.contains("web-form"));
        Assert.assertTrue(url.startsWith("https://"));
    }

    @Test
    public void testElementCountAssertions() {
        List<WebElement> inputs = driver.findElements(By.tagName("input"));
        Assert.assertTrue(inputs.size() > 0, "Should find inputs");
        Assert.assertTrue(inputs.size() >= 5,
            "Should have at least 5 inputs");
    }

    @Test
    public void testSoftAssertions() {
        SoftAssert soft = new SoftAssert();

        soft.assertEquals(driver.getTitle(), "Web form");
        soft.assertTrue(driver.getCurrentUrl().contains("web-form"));

        WebElement input = driver.findElement(By.name("my-text"));
        soft.assertTrue(input.isDisplayed(), "Input visible");
        soft.assertTrue(input.isEnabled(), "Input enabled");
        soft.assertEquals(input.getAttribute("type"), "text");

        // All failures reported together at assertAll()
        soft.assertAll();
    }

    @AfterMethod
    public void teardown() { if (driver != null) driver.quit(); }
}
test_assertions.py
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest

@pytest.fixture
def driver():
    d = webdriver.Chrome()
    d.implicitly_wait(10)
    d.get("https://www.selenium.dev/selenium/web/web-form.html")
    yield d
    d.quit()


def test_text_assertions(driver):
    title = driver.title
    assert title == "Web form"
    assert "form" in title
    assert len(title) > 0


def test_element_state(driver):
    input_el = driver.find_element(By.NAME, "my-text")
    assert input_el.is_displayed(), "Input should be visible"
    assert input_el.is_enabled(), "Input should be enabled"

    disabled = driver.find_element(By.NAME, "my-disabled")
    assert not disabled.is_enabled(), "Should be disabled"


def test_attribute_assertions(driver):
    input_el = driver.find_element(By.NAME, "my-text")
    assert input_el.get_attribute("type") == "text"
    assert input_el.get_attribute("name") is not None
    assert input_el.get_attribute("myprop") == "myvalue"


def test_url_assertions(driver):
    url = driver.current_url
    assert "web-form" in url
    assert url.startswith("https://")


def test_element_count(driver):
    inputs = driver.find_elements(By.TAG_NAME, "input")
    assert len(inputs) > 0, "Should find inputs"
    assert len(inputs) >= 5, "Should have at least 5 inputs"


def test_multiple_assertions(driver):
    """pytest collects assertion details on failure"""
    assert driver.title == "Web form"
    assert "web-form" in driver.current_url

    input_el = driver.find_element(By.NAME, "my-text")
    assert input_el.is_displayed()
    assert input_el.is_enabled()
    assert input_el.get_attribute("type") == "text"

    # For soft-assertion style, use pytest-check plugin:
    # pip install pytest-check
    # from pytest_check import check
    # with check:
    #     assert False, "This won't stop the test"
    # with check:
    #     assert True, "This still runs" 

Selenium Beginner Assertions & Verification

Written by PV

© 2026 All Rights Reserved