Back to Test Automation
2026-03-286 min read

Adding New Assertions (Test Automation)

Learn Adding New Assertions (Test Automation) step by step with clear examples and exercises.

Why This Matters

In test automation, assertions play a crucial role in verifying that our application behaves as expected. They help us catch bugs early and ensure the quality of our software. In this lesson, we'll dive deeper into adding new assertions using JavaScript with popular test automation libraries like Selenium, Cypress, and Playwright.

Why This Matters

Test automation is an essential part of modern software development, as it allows us to run repetitive tasks efficiently and catch bugs early in the development lifecycle. Assertions are a fundamental aspect of test automation, providing a means to verify that our application behaves as expected under various conditions. By adding new assertions, we can extend the functionality of our tests and improve their effectiveness.

Prerequisites

To follow along with this lesson, you should have a basic understanding of:

  1. JavaScript (ES6)
  2. Test Automation (Selenium, Cypress, or Playwright)
  3. Understanding the basics of assertions and their importance in test automation
  4. Familiarity with your chosen test automation library's syntax and APIs
  5. Knowledge of the application you're testing and its expected behavior

Core Concept

Assertion Libraries

Assertion libraries provide a set of functions to verify the expected outcomes in our tests. Some popular JavaScript assertion libraries are:

  1. Chai: A BDD-style assertion library with a simple API and extensive functionality. It can be used with Selenium, Cypress, and Playwright.
  2. Jest: An all-purpose JavaScript testing framework that includes an assertion library.
  3. Mocha: Another popular JavaScript testing framework with built-in assertions.

Custom Assertions

In addition to the predefined assertion methods provided by libraries like Chai, Jest, and Mocha, we can also create custom assertions to suit our specific needs. Custom assertions allow us to validate complex conditions that may not be covered by existing methods.

Creating a Custom Assertion in Chai

const chai = require('chai');
const expect = chai.expect;

// Define custom assertion for checking if an array contains unique elements
function arrayContainsUniqueElements(array) {
const setArray = new Set(array);
return expect(setArray.size).to.equal(array.length);
}

chai.Assertion.addMethod('containsUniqueElements', arrayContainsUniqueElements);

In this example, we've created a custom assertion named containsUniqueElements for Chai that checks if an array contains unique elements. We then register the function as a new method on the Assertion object.

Assertion Methods

Assertion libraries offer various methods to check different conditions. Here are some common ones:

  1. expect(value).to.equal(expected): Checks if the value is equal to the expected outcome.
  2. expect(value).to.not.equal(expected): Verifies that the value is not equal to the expected outcome.
  3. expect(value).to.be.an('array'): Asserts that the value is an array.
  4. expect(value).to.be.a('string'): Checks if the value is a string.
  5. expect(value).to.have.lengthOf(expectedLength): Verifies the length of the value matches the expected length.
  6. expect(value).to.include(expectedSubstring): Asserts that the value includes the provided substring.
  7. expect(element).to.be.visible: Verifies that an HTML element is visible on the page (Cypress-specific).
  8. expect(element).to.have.text('Expected Text'): Checks if an HTML element's text content matches the expected text (Playwright-specific).

Assertion Chaining

Assertion libraries allow us to chain multiple assertions together, making our tests more readable and easier to understand. For example:

const chai = require('chai');
const expect = chai.expect;

describe('My Test', function() {
it('Checks if the value is correct', function() {
const value = 5;
expect(value).to.be.a('number')
.and.to.equal(5)
.and.to.not.be.above(10);
});
});

In this example, we're using Chai to chain multiple assertions on the same value. The test will pass if all conditions are met.

Worked Example

Let's create a simple test using Selenium with JavaScript that verifies the title of a webpage and checks if an array contains unique elements:

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

// Define custom assertion for checking if an array contains unique elements
function arrayContainsUniqueElements(array) {
const setArray = new Set(array);
return expect(setArray.size).to.equal(array.length);
}

chai.Assertion.addMethod('containsUniqueElements', arrayContainsUniqueElements);

describe('Test Webpage Title and Array Uniqueness', function() {
let driver;

beforeEach(async function() {
driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.example.com');
});

it('Verifies the webpage title', async function() {
const title = await driver.getTitle();
expect(title).to.equal('Example Domain');
});

it('Checks if an array contains unique elements', async function() {
const numbers = [1, 2, 3, 4];
expect(numbers).to.contain.only.members([1, 2, 3, 4]);
});

afterEach(async function() {
await driver.quit();
});
});

In this example, we've created a custom assertion for checking if an array contains unique elements and used it in our Selenium test to verify the title of a webpage and check the uniqueness of an array.

Common Mistakes

  1. Not waiting for the page to load: Always ensure that your tests wait for the necessary elements to appear before performing any actions or making assertions.
  2. Using incorrect assertion methods: Make sure you're using the appropriate assertion method for each condition you want to check.
  3. Ignoring error messages: If a test fails, carefully examine the error messages to understand why it failed and correct the issue.
  4. Not handling exceptions: Properly handle exceptions in your tests to ensure they don't cause the entire test suite to fail.
  5. Not using assertion chaining: Chaining multiple assertions together can make your tests more readable and easier to understand.
  6. Creating inefficient custom assertions: Make sure your custom assertions are efficient and don't introduce unnecessary complexity or performance issues.
  7. Not documenting custom assertions: Document your custom assertions so others can easily understand their purpose and usage.

Practice Questions

  1. Write a test using Cypress that verifies if a specific element exists on a webpage and checks if an array contains unique elements.
  2. Create a Playwright test that checks the text content of an HTML element and validates the value of a form field.
  3. Write a Selenium test using JavaScript that validates the existence of multiple elements on a webpage and verifies if an object's properties meet certain conditions.
  4. Implement a Jest test to verify if an array contains only specific values within a given range.

FAQ

  1. Why are assertions important in test automation? Assertions help us verify that our application behaves as expected, catch bugs early, and ensure the quality of our software.
  2. What is Chai and why is it commonly used with Selenium, Cypress, and Playwright? Chai is a BDD-style assertion library with a simple API and extensive functionality. It can be easily integrated with various test automation libraries like Selenium, Cypress, and Playwright.
  3. What are some common assertion methods available in JavaScript assertion libraries? Some common assertion methods include to.equal, to.not.equal, to.be.an, to.be.a, to.have.lengthOf, to.include, to.be.visible, and to.have.text.
  4. How can I chain multiple assertions together in my tests? Most JavaScript assertion libraries allow you to chain multiple assertions together using the and method, as demonstrated in the worked example.
  5. What are custom assertions, and why would I need them? Custom assertions are user-defined functions that extend the functionality of existing assertion libraries. They can be used to validate complex conditions or specific requirements not covered by predefined methods.
  6. How do I create a custom assertion in Chai? To create a custom assertion in Chai, define a function with the desired behavior and register it as a new method on the Assertion object using the addMethod method.
Adding New Assertions (Test Automation) | Test Automation | XQA Learn