Member-only story

Assertions in Web Test Automation

Some common and useful assertions to use in your scripts

Courtney Zhan
2 min readJan 26, 2025

Non-Medium Members: You can read this article free on Substack.

This article will cover some common assertions along with accompanying examples. I’ll use RSpec’s syntax in this article, but other frameworks will have the same or similar approach.

1. Verify the Page Source Contains a Piece of Text

This is the most basic assertion — checking the raw HTML source of the current page.

html = "Text assertion with a  (<b>tab</b> before), and \n(new line before)!"
expect(driver.page_source).to include(html)

RSpec uses to include for text present, and the opposite assertion of checking text is not there is not_to include.

expect(driver.page_source).not_to include(html)

2. Verify an element’s text

This is the most common assertion I use. Instead of using driver.page_source, have the element’s locator’s text as the subject. In RSpec, use eq instead of include for exact matching.

expect(driver.find_element(:id, "label_1").text).to eq("First Label")

--

--

No responses yet