Search:

Assertion Basics in Test Automation

About various assertions and examples with Selenium WebDriver.

A Guide to Assertion Basics in Test Automation - kloia Blog

In test automation, assertion is the validation step that determines whether the automated test case succeeded or not. Automated test cases should supported by assertion steps. Otherwise, we can’t guarantee that the actual and expected result are equal to each other. Without this control, the automated test cases will cause more harm than benefit.. To prevent this, there are different methods for assertion, such as text content assertion, UI web element assertion, or location assertion. If the assertion step fails, the test will be aborted and the remaining test steps will not be executed.

In this post, I’m going to tell about various assertions and give examples with Selenium WebDriver.

UI Web Element Assertion

The code below waits to see “name” element “q” on the web page for 10 seconds.

 

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.navigate.to('https://www.google.com/')

wait = Selenium::WebDriver::Wait.new(timeout: 10)

wait.until { driver.find_element(:name, 'q') }

 

If the web element does not appear on the page in 10 seconds, web driver throws a TimeOutError exception.

`until': timed out after 10 seconds (no such element: Unable to locate element: {"method":"name","selector":"q"} (Selenium::WebDriver::Error::TimeOutError)

 

Text Content of A Web Element Assertion

Text Content of Web Element Assertion is quite similar to UI web element assertion. The difference is checking the text or value of a web element on the web page. You can reach text of a web element by “.text” easily. (for value “.value”)

 

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.navigate.to('https://www.google.com/')

wait = Selenium::WebDriver::Wait.new(timeout: 10)

element = driver.find_element(:id, 'gb_70')

wait.until { element }

element.text == 'Sign in' ? (puts 'succeeded') : (puts 'failed')

 

Text Content Assertion

Text Content is similar to the two previous examples too. Text Content assertion does not care about web elements, it just cares about whether a text content which you want to check is on the web page or not.

 

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.navigate.to('https://www.google.com/')

driver.page_source.match('Sign in') ? (puts 'succeeded') : (puts 'failed')

 

Location Assertion

As test developer engineers, we check the page URL or path especially after operations that redirect the web page. To check the page URL, you can use “current_url” method on “driver” object.

 

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.navigate.to('https://www.google.com/')

driver.current_url == 'https://www.google.com/' ? (puts 'succeeded') : (puts driver.current_url)

 

Don’t Forget to Add Assertion Steps

Test automation projects help software teams deliver products with fewer bugs. If you place the automated test processes into your CI/CD pipeline, you will have a chance to find bugs faster and to fix them early. But for higher efficiency and larger state coverage, we recommend filling your test automation project with different types of assertion steps. This variation allows you to check your application thoroughly, so that you can avoid a deadly harmful situation in the production environment.

Burak Koyuncu

Experienced Software Test Consultant with a demonstrated history of working in the finance and e-commerce industry.