How To Use Asserts In NUnit Using Selenium?

Posted by Himanshu Sheth | February 8, 2021
Selenium C# •

27456 Views | 20 Min Read

Using Asserts In NUnit Using Selenium

When you are working on unit tests, there are scenarios where you wished that the execution should have stopped but alas, the most important part, ‘The Assertion,’ was missed. While developing tests using different test frameworks, my fellow developers frequently asked one question: ‘Should I use an assert or an exception in my test code’? It depends on what the test code should do after a certain condition is met.

By the end of this article, you will get in-depth insights about NUnit asserts and how these asserts can be used with C# & Selenium. I will walk you through the Assertions’ basics and when assertions should be used to get started. We will be covering the following constraints (discussed later) used with NUnit assert:

  1. Equal Constraints
  2. Comparison Constraints
  3. String Constraints
  4. Condition Constraints
  5. Collection Constraints

Source

If you already know the constraints, you can head straight to the sections (NUnit Asserts – In Action and NUnit Asserts – Cross Browser Testing) that demonstrate usage of NUnit assert in the context of Selenium automation testing.

What are Assertions?

In very simple terms, Assertions should be used to check something (a condition) that should never occur, whereas an exception should be used to check something (a condition) that might occur. For example, when a division operation is used, an exception can be raised if you get the ‘Divide by 0’ error. On the other hand, when using Selenium automation testing with C# (or another programming language), an assertion can be raised if the Page Title does not match the expected Title.

Below is a detailed list that highlights the use of assertions:

  • Provide feedback about the source code to the development team.
  • Check whether the implementation is correct, else there could be a severe issue with the code.
  • Check preconditions and postconditions that can occur in the code.

What if you don’t assert at the right place? Well, it is like the situation shown below – walking on stairs where steps are broken, you are bound to get hurt ☺.

Source

The NUnit Framework

The NUnit framework is a widely used open-source test framework used with C# and Selenium. It is based on the JUnit framework. The latest version is NUnit3.

When using C# for development & testing, I tried other test frameworks, MSTest/Visual Studio, xUnit.NET, etc., the advantages of NUnit over-power the different frameworks. Annotations in NUnit help speed the development and execution of the tests with a wide range of inputs.

If you plan to use TDD (Test Driven Development) for testing, you should try out the NUnit framework. In case you are looking for in-depth coverage on the NUnit framework with Selenium along with steps for installation on Visual Studio, you should check out the NUnit tutorial on setting up NUnit environment with Visual Studio in more detail.

Arrange, Act, and Assert

The AAA (Arrange, Act, and Assert) is a common practice for writing unit tests.

  • Arrange – This section describes the setup required to execute the test.
  • Act – This section executes the unit being tested, with the output being a store for evaluation.
  • Assert – This section verifies whether the tests behaved as per expectations.

Many developers use ‘Given,’ ‘When,’ and ‘Then’ instead of AAA as it gives more descriptive meaning to these phrases, and many BDD tools also use these phrases to name their testing keywords.

The advantage of the AAA approach is that tests become more modular, thereby improving the tests’ readability.

Assertions in NUnit

NUnit has a rich set of assertions, which are static methods of the Assert Class.

However, many developers have multiple asserts in one test, but that is not a good programming practice since the tests following the first NUnit assert do not get executed. Hence, it is recommended to have only one NUnit assert per test.

Before NUnit 2.4, Classic Model was used for using NUnit asserts. In this model, each NUnit assert was used via a separate method of the Assert Class. From NUnit 2.4 onwards, Constraint Model was introduced where a single Assert Class method was used. Constraint Object is passed as an argument with the details of the test that needs to be performed.

Helper Classes

Helper classes in C# are classes that provide methods that ‘assist/help’ get something done. Below are the helper classes to provide constraints to the assert method.

  1. Is
  2. Has
  3. Throws
  4. Contains
  5. Does

In the example that was shown above, Is is the helper class, and EqualTo is the constraint.

Constraints in NUnit assert

There are eight broad categories of constraints:

1. Equal Constraints

This category of constraint is used to compare the expected result with the actual result. The Equal operation can be for equality (EqualTo) and non-equality (NotEqualTo)

a. EqualTo

It is used to check whether the actual result is the same as the expected result. If the results are not the same, assert is raised.

Syntax:

Here is a Selenium automation testing example that demonstrates the use of EqualTo constraint.

FileName – 1_Equal_To_Constraint.cs

In the above code, we have provided two parameters in the Assert.That method, which is an expected result and EqualTo constraint. The actual URL is compared with the Test URL (i.e., https://www.lambdatest.com/”).

Assert error will be thrown if the values do not match, and the program execution will get terminated at this same line, i.e., the assertion statement itself.

b. NotEqualTo

As the name suggests, NoEqualTo does the opposite of EqualTo. It is used to check whether the actual result is not the same as the expected result. Assert will be raised if the results match.

Syntax:

Here is a Selenium automation testing example that demonstrates the use of Not.EqualTo constraint. The implementation remains the same as the previous example, except that the check is now done for inequality.

FileName – 2_Equal_To_Constraint.cs

In the above code, we have provided two parameters in the Assert.That method, which is an expected result and Not.EqualTo constraint. Actual URL is compared with the Test URL (i.e., https://www.lambdatest.com”). Though the URLs are the same, the command driver.Url will return https://www.lambdatest.com/, which does not match the Test URL; hence, the test passes and assert is not raised.

2. Comparison Constraints

This category of constraint in NUnit assert is used to test whether one object is greater than the other. They are designed in a way that they can be read in mathematical order. The comparison operation can be for equality GreaterThan (or GreaterThanOrEqualTo), LessThan (or LessThanOrEqualTo), or checking if the result lies in a particular range (InRange).

Except for the InRange constraint, all the other comparison constraints can be used along with Not, i.e., NotGreaterThan/NotLessThan/etc.

a. GreaterThan

It is used to check whether the result is greater than the one supplied in the GreaterThan constraint. This constraint can be used with input values of data types like integer, float, double, long, etc.

Syntax:

Example:

To demonstrate the usage of the Nunit assert using Greater.Than constraint, we take a simplified example where Assert is raised if the actual value is greater than the value against which the test is performed.

FileName – 3_Greater_Than_Constraint.cs

As shown in the above example, assert is raised if the actual value is greater than 30. As you can see, the parameter inputted to the constraint is 45, which is greater than 30; hence, the test fails and assert is raised.

b. GreaterThanOrEqualTo

It is used to check whether the result is greater or equal to the one supplied in the GreaterThanEqualTo constraint.

Syntax:

Sample Usage:

It can also be used along with the Not constraint, as shown below.

c. LessThan

It is used to check whether the result is less than the one supplied in the LessThan constraint. Assert will be raised if the input value is greater than the expected value. It can also be used along with Not where the intent of the operation is negated.

Syntax:

Example:

FileName – 4_Less_Than_Constraint.cs

In the example shown above, assert will be raised if the actual value is not less than the LessThan constraint’s value. Since the input value (25) is less than the expected value (30), no assert is raised.

d. LessThanOrEqualTo

It is used to check whether the result is less or equal to the one supplied in the LessThan constraint. Assert will be raised if the input value is greater than the expected value.

Syntax:

Example:

e. InRange

When using Selenium automation testing, there could be scenarios where you may want to test if a value lies within a particular range. The InRange constraint can be used to check in such a scenario.
It is a combination of two constraints – LessThanOrEqualTo and GreaterThanOrEqualTo.

Syntax:

Example:

As shown in the above example, the input value, i.e., 25, is within the range of 25~30. Hence, no assert is raised, and the test passes.

3. String Constraints

These set of constraints are used when NUnit assert has to be raised when examining string values. String values can be checked for equality (with or without ignoring case), whether a particular sub-string is present in the input string, whether a string is empty (or not), etc.

a. String Equal or Not Equal

This constraint is used to check whether the resultant string is the same (or not) as the string supplied in the Is.EqualTo or Is.Not.EqualTo constraint. The comparison is case-sensitive.

Syntax:

Example:

FileName – 6_String_EqualTo_Constraint.cs

In the above example that demonstrates the usage of NUnit assert with Selenium automation testing, we open the URL under test on the Chrome browser. An instance of Chrome WebDriver is instantiated in the [SetUp] annotation, and actual business logic is placed in the [Test] annotation.

The site title (site_title) is extracted using the Web browser command driver.Title. A string-based comparison is made with the expected title. Assert is raised if the titles do not match. As seen in the output below, the string comparison passes. Hence, no assert is raised.

b. String Equal with IgnoreCase

This is a case-insensitive version of String Equal as the case is ignored using the IgnoreCase constraint.

Syntax:

Example:

The logic in the previous example can be changed to ignore case-sensitivity. The change will be in the EqualTo constraint.

c. DoesContain

The DoesContain (and DoesNotContain) constraints are used to check if a sub-string is present in a given string. Assert is raised if the search is not successful.

It makes use of the Does Helper function, and the sub-string search can be case-insensitive.

Syntax:

Example:

FileName – 7_SubString_Constraint.cs

In the above example, the sub-string to be searched is ‘lambdatest.’ The search for the sub-string is case-insensitive. If the sub-string is present in the site title (URL under test is https://www.lambdatest.com), no assert is raised, and the test passes.

As the sub-string (i.e., lambdatest) is present in the site title, no assert is raised.

d. Empty

This constraint is used to check if the resultant string is Empty or not. If Is.Empty is used, assert is raised if the result is not empty. If Is.Not.Empty is used, assert is raised if the result is empty i.e., Blank string.

Syntax:

Example:

We modify the previous example that demonstrated the DoesContain constraint to check if the Site title for https://www.lambdatest.com is empty or not. Assert should be raised if the Site title is empty. Below is the code snippet since the majority of the implementation from the perspective of NUnit assert remains the same.

Empty_Constraint.cs

e. StartWith and EndWith

The StartWith and EndWith constraints are used with the IS helper functions to check if a resultant string starts with or ends with a particular string respectively. Assert is raised if the conditions for the search are not met. You can also make use of Not to negate the intent of the search.

Syntax:

Example:

For demonstrating the use of StartWith and EndWith constraints, we again make use of the example where the site title is checked whether assertion should be raised or not.

A case-insensitive check is done on the Site title, whether it starts with “cross” and whether it ends with “lambdatest”. If either of these operations are not successful, assert is raised.

The test passes since the strings are present at the respective positions i.e. Start and End.

f. Regex (Does.Match)

In case you plan to use regular expressions as a part of NUnit assert, you can make use of DoesMatch (or DoesNotMatch) constraint. It validates if the value matches a regular expression.

Syntax:

Example:

For demonstration, we create a regular expression to match whether the title starts with “Cross.”

A match against the regular expression, and assert raised if the Site title does not start with “Cross.”

4. Condition Constraints

Condition constraints are used to check if a specific condition is satisfied or not. It could be a Boolean condition, Null, or Empty.

a. Empty

This constraint is used to check for Empty value. Assert is raised depending on whether you are checking if the field should be Empty or not.

Syntax

Example

In the above Selenium automation testing demonstrating the usage of NUnit assert, the input URL is https://accounts.lambdatest.com/login. Using the Inspect tool, we locate the XPath of the email text box and fill the email text box with email-address (using SendKeys method).

We check whether the email-address text box is EMPTY or not? Assert is raised if the text box is not EMPTY.

The above test fails since it was expecting the email-address text box to be empty.

b. Null

This constraint in NUnit assert is used to check if a particular object is NULL or not. The object could be array, string, etc.

Syntax

c. Boolean

As the name indicates, it can be used to check if a particular condition is satisfied or not (True/False).

Syntax

5. Collection Constraints

The Collection Constraints are useful when you want to examine collections and contents inside it or for comparing two collections. Array is one example of a collection. Since this category of constraint would be more useful for testing the back-end business logic (databases, etc.), we won’t cover it in more detail in this NUnit tutorial.

For highlighting the syntax of different constraints, we will use an array of integers:

a. Greater than

It is used to check if the values in a collection (array in this case) are greater than a ‘comparison’ value. NUnit assert is raised if any one of the items is less than the comparison value.

Example:

b. Less Than

It is used to check if the values in a collection (array in this case) are lesser than a ‘comparison’ value. NUnit assert is raised if any one of the items is greater than the comparison value.

Example:

c. Not Null

As the name signifies, it is used to check if there are no NULL values in the collection. You can even use this when performing a NULL check on string arrays. Assert is raised if any one of the items in the collection is found to be NULL.

Example:

Assert.That(arr, Is.All.Not.Null);

d. Instance Of

It is used to check if the items in a collection belong to one particular data-type (Int32, String, Float, etc.).

Example:

e. Exactly X Items

Exactly constraint should be used to check if a collection contains an ‘X’ number of items. Assert is raised if the collection does not contain exactly X items.

Example

f. Unique Items

It can be used to verify if all the items in a collection have a unique value. Assert is raised if any one of the items is not unique.

Example:

g. Empty

Like the Empty condition constraint, this constraint also does the same job except that it acts on a collection.

Example:

h. Contains

Contains can be instrumental when you want to perform a non-linear search for a particular element in a collection. Assert is raised if the item (i.e. value) is not present in the collection.

Example:

Apart from these constraints, there are many other constraints (Directory/File constraints, Type/Reference constraints, and Exceptions constraints) that may not be relevant in the context of cross browser testing or Selenium automation testing. Hence, those are not covered as a part of this article.

NUnit Asserts – In Action

To demonstrate the usages of NUnit assert in Selenium automation testing, we take an example of a Sample ToDo app on LambaTest. We make use of the NUnit framework along with Selenium, relevant asserts would be used for testing & verification.

Test Case

  1. Open the Sample ToDo App – https://lambdatest.github.io/sample-todo-app/
  2. Assert if the Driver title does not match the Expected title
  3. Select the first two check-box items
  4. Find textbox with id = sampletodotext and add a new item to the list
  5. Verify whether the item has been added else raise an assert

Implementation

Test_Case_Assertion.cs

Code Walkthrough

We get the details about the check-box items via their name, i.e. li1, li2, etc., using the Inspect tool of the Chrome browser.

We then locate the text-box element where the text for the new element has to be added and add a button which enables the addition of the new text to the list.

Once the new element has been added, we check whether it is a part of the list by verifying the text box’s content, i.e., it should not be empty. We make use of NUnit assert with Boolean Condition Constraint to verify the same.

Shown below is the execution snapshot where we can see that the new item is added to the list.

NUnit Asserts – Cross Browser Testing

As a web developer, you have performed cross browser testing on a local setup available for pursuit. Well Done! You feel that your job is complete, and your product is good to go live.

Source

What if the first customer experience itself gives you sleepless nights☹. It is true that no one can control customer’s choices. What if the customer uses a web browser/OS combination on which you have never performed testing? It is time to rethink your cross browser testing strategy by focusing on cloud-based cross browser testing rather than locally. There are many advantages; the most significant is acceleration in the test process since tests can be performed parallel. You can verify your product’s functionalities (especially those that are browser/OS/device specific) on different browser combinations, thereby improving the test coverage.

LambdaTest is a cloud based cross browser testing platform where you can perform testing on close to 2000+ browsers and OS combinations. Once you can create an account on LambdaTest, you should choose a billing plan as per your requirements. We were performing local Selenium automation testing, but now it has to be ported to LambdaTest’s remote Selenium Grid. Doing so does not require much effort as the changes are related to infrastructure.

Browser capabilities can be generated using the LambdaTest capabilities generator. For using the platform, you would need a user-name & access-key, and this can be accessed from the LambdaTest Profile Page. You are all set to perform web browser testing on the remote Selenium Grid.

We implement two test cases to demonstrate the usage of NUnit assert on the remote Selenium Grid.

Test Case – 1

  1. Navigate to the URL https://www.lambdatest.com
  2. Locate the Login button and click on the same
  3. Verify the Title of the page and raise assert if the Title is not matching the expected value

Test Case – 2

  1. Navigate to the URL https://accounts.lambdatest.com/login
  2. Enter the user-name and password by locating the respective elements
  3. Raise assert if either of them is empty
  4. Click the Login Button
  5. Once logged-in, verify the title of the page else raise assert

Implementation

LT_Test_Case_Assertion.cs

Code Walkthrough

The combination of user-name and access-token is used to login to the platform. These are passed to the remote URL on which the Selenium automation testing will be performed.

The browser capabilities are generated using the LambdaTest capabilities generator.

Test Case 1 – We locate the login button using XPath locator. Once the element is located, a Click action is performed to navigate to the target page.

Once the LambdaTest login page opens, the title of the page is compared with the expected title using EqualTo constraint. If they do not match, assert is raised.

Test Case 2 – Like Test case (1), required elements (user name & password text boxes and login button) are located on the LambdaTest login web page. Once those elements are located, the respective actions are performed on those elements, i.e., user-name and password are entered in the boxes, and the login button is clicked.

Once it navigates to the target page, i.e., LambdaTest dashboard, the title is checked. If it does not match the expected title, assert is raised.

We visit the AUTOMATION LOGS section to check the status of the test. As seen in the snapshot, the test status is Completed, i.e., the test has passed.

It’s a Wrap

In this brief NUnit tutorial with a focus on NUnit assert, we looked at different asserts and constraints helpful in Selenium automation testing. The intent of using assert in test code is to halt the execution as soon as assert is encountered. To improve test coverage and verify product functionalities across different browser & operating system combinations, developers can explore Selenium testing on the cloud. The advantage is that the core business logic remains intact, with minimal changes required to port the NUnit test code to the remote Selenium Grid.

Do leave the frequently used NUnit asserts in the comments section to learn from each other’s experience.

Happy Testing ☺

Written by Himanshu Sheth

With 7+ years of experience in test automation, BDD, TDD, Himanshu Sheth work as an automation engineer with LambdaTest. He is also an avid blogger.

Related Articles

Scraping Dynamic Web Pages

Scraping Dynamic Web Pages Using Selenium And C#

25877 Views | 16 Min Min Read

Leave a Reply

Your email address will not be published. Required fields are marked *