Back to Test Automation
2026-03-237 min read

assertions (Test Automation)

Learn assertions (Test Automation) step by step with clear examples and exercises.

Title: Mastering Assertions in Test Automation using JavaScript (Selenium, Cypress, Playwright)

Why This Matters

Assertions play a crucial role in ensuring the reliability and accuracy of test automation by verifying that the application behaves as expected. They help catch bugs early and provide confidence in your automated testing process. Understanding how to effectively use assertions in popular test automation frameworks like Selenium, Cypress, and Playwright can significantly improve the efficiency and effectiveness of your testing process.

Prerequisites

Before diving into assertions, you should have a good understanding of:

  • JavaScript (ES6) basics
  • One or more test automation frameworks (Selenium, Cypress, Playwright)
  • Familiarity with the application under test
  • Basic concepts of testing such as test cases, test suites, and test runners
  • Understanding of HTML, CSS, and browser interactions

Key Concepts to Understand

  • Test Case: A single unit of testing that verifies a specific functionality or behavior.
  • Test Suite: A collection of test cases that are related to each other and are executed together.
  • Test Runner: A tool that executes the test suite, reports test results, and handles test configuration.
  • HTML Document Object Model (DOM): The structure of an HTML document as a tree of nodes representing elements, attributes, and their relationships.

Core Concept

Assertions are used to verify that the actual outcome of a test matches the expected outcome. They can compare values, check for the presence of elements, and validate the state of the application. In JavaScript, assertion libraries like Chai, Jest, and Mocha provide various methods to make these comparisons.

In Selenium, you can use assert functions provided by WebDriverJS or TestNG. Cypress has built-in assertion methods, while Playwright offers a similar API to Selenium.

Assertion Methods

Here's an overview of some common assertion methods:

  1. Equal (assertEqual, expect(value).toEqual(), etc.): Compares two values for equality.
  2. Not Equal (assert.notEqual(), expect(value).not.toEqual(), etc.): Compares two values to ensure they are not equal.
  3. Strict Equal (assertEqualWithOptions({strict: true}), expect(value).toStrictEqual(), etc.): Compares two values for strict equality, considering their type as well.
  4. Not Strict Equal (assert.notStrictEqual(), expect(value).not.toStrictEqual(), etc.): Compares two values to ensure they are not strictly equal.
  5. Less Than (assert.isLessThan(), expect(value).toBeLessThan(), etc.): Checks if a value is less than another value.
  6. Greater Than (assert.isGreaterThan(), expect(value).toBeGreaterThan(), etc.): Checks if a value is greater than another value.
  7. Contains String (assert.contains(), expect(string).toContain(), etc.): Verifies if one string contains another string.
  8. Matches RegExp (assert.matches(), expect(value).toMatchRegExp(), etc.): Checks if a value matches a regular expression pattern.
  9. Exists (element.isDisplayed(), cy.get('selector').should('exist'), etc.): Verifies that an element exists on the page.
  10. Is Visible (element.isVisible(), cy.get('selector').should('be.visible'), etc.): Checks if an element is visible on the page.
  11. Enabled (element.isEnabled(), cy.get('selector').should('be.enabled'), etc.): Verifies that an element is enabled and can be interacted with.
  12. Selected (element.isSelected(), cy.get('selector').should('be.selected'), etc.): Checks if a select option, checkbox, or radio button is selected.
  13. Contains Class/Attribute (element.hasClass(), cy.get('selector').should('have.class'), etc.): Verifies that an element has a specific class or attribute.
  14. Has Text (cy.contains(), etc.): Checks if an element contains a specific text.
  15. DOM Property Assertions (element.getProperty('propertyName')): Verifies the value of a specific property for an HTML element, such as innerHTML, textContent, or value.
  16. Element State Assertions (element.isEnabled(), element.isSelected(), etc.): Checks the state of an HTML element, such as whether it is enabled, selected, or disabled.

Worked Example

Let's create a test using Selenium, Cypress, and Playwright to verify that the title of a webpage is "Test Automation".

Selenium (WebDriverJS)

const { Builder, By, Key } = require('selenium-webdriver');

(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('http://www.example.com');
const title = await driver.getTitle();
expect(title).toEqual('Test Automation');
} catch (error) {
console.error(`Error: ${error}`);
} finally {
await driver.quit();
}
})();

Cypress

describe('Title test', function() {
it('Verifies the title', function() {
cy.visit('http://www.example.com');
cy.title().should('eq', 'Test Automation');
});
});

Playwright

const { chromium, firefox, webkit } = require('playwright');

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('http://www.example.com');
const title = await page.title();
expect(title).toEqual('Test Automation');
await browser.close();
})();

Common Mistakes

  1. Not using assertions: Neglecting to use assertions can lead to tests that don't catch errors or validate the correct behavior of your application.
  2. Incorrect usage of assertion methods: Misusing assertion methods, such as comparing strings with === instead of toEqual(), can result in false positives and incorrect test results.
  3. Ignoring timeouts: Failing to set appropriate timeouts for page loads or element interactions can cause tests to fail unnecessarily.
  4. Not handling exceptions: Properly handling exceptions, such as network errors or element not found, is crucial for robust testing.
  5. Overcomplicating assertions: Using complex assertion methods or unnecessary nested assertions can make your tests harder to read and maintain.
  6. Not using proper waits: Failing to use appropriate waits before performing actions or making assertions can cause tests to fail due to elements not being ready.
  7. Not checking for errors: Not checking for errors in the console or logs can lead to missed issues that affect test results.
  8. Not cleaning up resources: Failing to clean up resources like cookies, local storage, and temporary files between tests can cause unexpected behavior in subsequent tests.
  9. Ignoring browser-specific quirks: Different browsers may have unique behaviors or limitations that need to be accounted for when writing tests.
  10. Not using parameterized tests: Manually creating multiple test cases for the same functionality can lead to duplicated code and maintenance issues. Using parameterized tests can help simplify your test suite.
  11. Ignoring element state assertions: Failing to check the state of elements, such as whether they are enabled or selected, can lead to incorrect test results.
  12. Not using DOM property assertions: Verifying the value of specific properties for HTML elements can help ensure that your tests account for changes in the DOM structure.

Practice Questions

  1. Write a test using Selenium to verify that the login form on www.example.com is visible after navigating to the page.
  2. Create a Cypress test to ensure that the "Sign Up" button on www.example.com has the correct text and is clickable.
  3. Write a Playwright test to verify that the content of the body tag on www.example.com matches a specific HTML structure.
  4. Modify the previous examples to include appropriate waits before making assertions or performing actions.
  5. Write a parameterized test using Cypress to validate multiple email addresses for login functionality.
  6. Create a Selenium test that verifies the correct behavior of a dropdown menu on www.example.com.
  7. Implement error handling in a Playwright test to handle network errors or elements not found during testing.
  8. Write a Cypress test to validate the functionality of a form with multiple input fields and submit button.
  9. Create a Selenium test that verifies the correct behavior of a modal dialog on www.example.com.
  10. Implement a cleanup function in your tests to remove temporary files or clear cookies/local storage between tests.
  11. Write a Playwright test that verifies the presence and value of specific attributes for HTML elements on www.example.com.
  12. Create a Cypress test that validates the functionality of an autocomplete input field with suggested options.

FAQ

  1. Why are assertions important in test automation? Assertions help ensure that your tests verify the correct behavior of your application and catch bugs early, providing confidence in your automated testing process.
  2. What is a Test Case? A Test Case is a single unit of testing that verifies a specific functionality or behavior.
  3. What is a Test Suite? A Test Suite is a collection of test cases that are related to each other and are executed together.
  4. What is a Test Runner? A Test Runner is a tool that executes the test suite, reports test results, and handles test configuration.
  5. How do I handle exceptions in my tests? Properly handling exceptions, such as network errors or element not found, is crucial for robust testing. You can use try-catch blocks to catch exceptions and perform appropriate actions.
  6. What are DOM property assertions? DOM property assertions verify the value of specific properties for HTML elements, such as innerHTML, textContent, or value.
  7. Why should I use parameterized tests? Using parameterized tests can help simplify your test suite by reducing duplicated code and maintenance issues when testing multiple scenarios with similar functionality.
assertions (Test Automation) | Test Automation | XQA Learn