Selenium C# Tutorial: Assertion in Selenium WebDriver using C#
Updated: Mar 5
Selenium WebDriver is a testing framework that would easily automate any test scenario on a web application with its feature-rich API for web automation. However, when creating tests in an automation project, we would require to assert specific steps of the test case we wish to automate. Now, it is easy to make a mistake and think that the responsibility of such assertion methods is part of the Selenium Webdriver suite. Still, it's not, as it requires us to use external assertion frameworks to assert on specific steps.

Assertion Library
Assertion (AKA: Asserts) is used to verify if the expected behavior after particular action on the application Is as expected per predefined conditions. To use the assertion library in selenium, we need to integrate with a library that provides this functionality without writing code with the condition explicitly but by directly calling the supporting function in the library. The most known Assertion libraries are:
Microsoft.VisualStudio.TestTools.UnitTesting
NUnit.Framework
Assert.xunit
Why use assertions?
Below is a list that highlights the use of assertions:
Help to validate that test cases deliver the results we expect to see.
Provide fast feedback about the code quality.
Validate that the implementation of code is correct.
It's easy to use and reduces code complexity.
Let's explore different types of assertions with examples.
assertEquals() - is a method that takes a minimum of 2 arguments and compares actual results with expected results. If both statements are equal, the assertion is passed, and the test is marked as a success. The assertEquals() method can compare different operators, including strings, integers, and many more variables.
[TestMethod]
public void AssertPageTitle()
{
//Test will pass only when the two strings are equal
Assert.AreEqual("Expected Results", driver.Title);
}
assertNotEquals() - is a method that does the opposite of the assertEquals() method. In this case, the method compares the actual and expected result. But if the assertion condition is met if the two are not identical. If actual and expected results are not the same, the test case is marked as passed.
[TestMethod]
public void AssertNotEqual()
{
//Test will fail only when the two numbers are equal
Random rnd = new Random();
int rand_num_1 = rnd.Next(1, 10000000);
int rand_num_2 = rnd.Next(1, 10000000);
Assert.AreNotEqual(rand_num_1, rand_num_2);
}
Result:

assertTrue() – This assertion is used to verify the Boolean value returned by a given condition. If the Boolean value is true, the assertion passes the test case.
[TestMethod]
public void AssertIsTrue_example_1()
{
//Test will pass only when the condition returns "True"
Assert.IsTrue(driver.Url.Contains("agile"));
}
[TestMethod]
public void AssertIsTrue_example_2()
{
//Here we expect to see failure and the notification printed in the test results
Assert.IsTrue(driver.Url.Contains("FalseString"), "Url does not contain the string, change it :)");
}
Result:

assertFalse() – This method works opposite compared to assertTrue(). The assertion verifies the boolean value returned by the condition. If the Boolean value is false, then the assertion passes the test.
[TestMethod]
public void AssertIsFalse()
{
//Test will pass because the condition returns "False"
Assert.IsFalse(driver.Title.Contains("FalseString"));
}
·assertNull(): This method verifies if the expected output is null. If not, the value returned is false.
[TestMethod]
public void AssertIsNull()
{
string value = null;
//Test will pass because the condition output is null.
Assert.IsNull(value);
}
·assertNotNull(): This method works opposite to that of the assertNull() method. The assertion condition is met when the method returns another value than null.
[TestMethod]
public void AssertIsNotNull()
{
string value = "null";
//Test will pass because the condition output is not null.
Assert.IsNotNull(value);
}