How To Use Asserts In NUnit Using Selenium?

himanshusheth004

himanshuseth004

Posted on March 26, 2021

How To Use 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

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 ☺.

Note- This CRC32B hash calculator lets you quickly generate the CRC32B checksum hash from a given string. In addition, you can generate CRC32B hashes via your web browser.

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.

@NUnit

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.

public class Assert
Enter fullscreen mode Exit fullscreen mode

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
Assert.That( myString, Is.EqualTo("LambdaTest") );
Enter fullscreen mode Exit fullscreen mode

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

Note- Lorem lpsum Generator is a tool that allows you to generate random IP addresses conveniently. Lorem Ipsum is a placeholder text commonly used for graphic and web design. The purpose of its use is to allow the designer to see the layout of a piece of work before it's finalized or made live.

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:

Is.EqualTo( object expected )
Enter fullscreen mode Exit fullscreen mode

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

FileName – 1_Equal_To_Constraint.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com/";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            String actual_url = brow.Geturl;

            Console.WriteLine("url " + actual_url);

            // Raise an assert if the URL's do not match
            Assert.That(actual_url, Is.EqualTo(test_url));

            Console.WriteLine("Test Passed");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.That(actual_url, Is.EqualTo(test_url));
Enter fullscreen mode Exit fullscreen mode

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:

Is.Not.EqualTo( object expected )
Enter fullscreen mode Exit fullscreen mode

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

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            String actual_url = brow.Geturl;

            Console.WriteLine("url " + actual_url);

            // Raise an assert if the URL's match
            // The URL on which testing is done is "https://www.lambdatest.com"
            // Whereas the get URL will return "https://www.lambdatest.com/"
            // Hence the above test will pass
            Assert.That(actual_url, Is.Not.EqualTo(test_url));

            Console.WriteLine("Test Passed");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

Is.GreaterThan( object expected )
Enter fullscreen mode Exit fullscreen mode

Example:

Assert.That(arg1, Is.Greater.Than(arg2));
Enter fullscreen mode Exit fullscreen mode

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

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    class Assert_Demo_1
    {
        [Test]
        public void test_comparison()
        {
            Int32 actual_value = 45;

            // Raise an assert if the GreaterThan is not successful
            Assert.That(actual_value, Is.Not.GreaterThan(30));

            Console.WriteLine("Test for comparison Passed");

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

Is.GreaterThanOrEqualTo( object expected )
Enter fullscreen mode Exit fullscreen mode

Sample Usage:

Assert.That(arg1, Is.GreaterThanOrEqualTo(arg2))
Enter fullscreen mode Exit fullscreen mode

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

Assert.That(arg1, Is.Not.GreaterThanOrEqualTo(arg2))
Enter fullscreen mode Exit fullscreen mode

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:

Is.LessThan( object expected )
Enter fullscreen mode Exit fullscreen mode

Example:

FileName – 4_Less_Than_Constraint.cs

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    class Assert_Demo_1
    {
        [Test]
        public void test_comparison()
        {
            Int32 actual_value = 25;

            // Raise an assert if the LessThan is not successful
            Assert.That(actual_value, Is.LessThan(30));

            Console.WriteLine("Test for comparison Passed");

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

Is.LessThanOrEqualTo( object expected )
Enter fullscreen mode Exit fullscreen mode

Example:

Assert.That(arg1, Is.LessThanOrEqualTo(arg2));
Assert.That(arg1, Is.Not.LessThanOrEqualTo(arg2));
Enter fullscreen mode Exit fullscreen mode

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:

Is.InRange( object from, object to )
Enter fullscreen mode Exit fullscreen mode

Example:

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    class Assert_Demo_1
    {
        [Test]
        public void test_comparison()
        {
            Int32 actual_value = 25;

            // Raise an assert if the InRange is not successful
            Assert.That(actual_value, Is.InRange(25,30));

            Console.WriteLine("Test for InRange Passed");

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

// Data type of the input value is string
Is.EqualTo( object expected )
Is.Not.EqualTo( object expected )
Enter fullscreen mode Exit fullscreen mode

Example:

FileName – 6_String_EqualTo_Constraint.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            String expected_title = "Cross Browser Testing Tools | Free Automated Website Testing | LambdaTest";

            String site_title = brow.Title;

            Console.WriteLine("The site title is " + site_title);

            // Raise an assert if the strings i.e. site titles do not match
            Assert.That(expected_title, Is.EqualTo(site_title));

            Console.WriteLine("String Constraint Test Passed");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

IgnoreCase { get; }
Enter fullscreen mode Exit fullscreen mode

Example:

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

Assert.That(expected_title, Is.EqualTo(site_title).IgnoreCase);
Enter fullscreen mode Exit fullscreen mode

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:

IgnoreCase { get; }
Enter fullscreen mode Exit fullscreen mode

Example:

FileName – 7_SubString_Constraint.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            String expected_title = "Cross Browser Testing Tools | Free Automated Website Testing | LambdaTest";
            String substring_title = "lambdaTest";

            String site_title = brow.Title;

            Console.WriteLine("The site title is " + site_title);

            // Raise an assert if the substring LambdaTest is present
            Assert.That(site_title, Does.Contain(substring_title).IgnoreCase);

            Console.WriteLine("Substring is present, Test passed");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

// Raise an assert if the substring LambdaTest is not present
Assert.That(site_title, Does.Contain(substring_title).IgnoreCase);
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(arg1, Is.Empty);
Enter fullscreen mode Exit fullscreen mode

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

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        ..............................
        ..............................
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            .....................................................
            .....................................................

            String site_title = brow.Title;

            Console.WriteLine("The site title is " + site_title);

            Assert.That(site_title, Is.Not.Empty);

            Console.WriteLine("Title not empty, Test passed");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);

            .....................................................
            .....................................................
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(str1, Does.StartWith("str2"));
Assert.That(str1, Does.Not.StartWith("str2"));
Enter fullscreen mode Exit fullscreen mode
Assert.That(str1, Does.EndWith("str2"));
Assert.That(str1, Does.Not.EndWith("str2"));
Enter fullscreen mode Exit fullscreen mode

Example:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            String start_with_str = "cross";
            String end_with_str = "lambdatest";

            String site_title = brow.Title;

            Console.WriteLine("The site title is " + site_title);

            Assert.That(site_title, Does.StartWith(start_with_str).IgnoreCase);
            Console.WriteLine("Site title " + site_title + " starts with " + start_with_str);

            Assert.That(site_title, Does.EndWith(end_with_str).IgnoreCase);
            Console.WriteLine("Site title " + site_title + " ends with " + end_with_str);

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Assert.That(site_title, Does.StartWith(start_with_str).IgnoreCase);
Console.WriteLine("Site title " + site_title + " starts with " + start_with_str);

Assert.That(site_title, Does.EndWith(end_with_str).IgnoreCase);
Console.WriteLine("Site title " + site_title + " ends with " + end_with_str);
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(result, Does.Match("regex expression"));
Assert.That(result, Does.Not.Match("regex expression"));
Enter fullscreen mode Exit fullscreen mode

Example:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Assert_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public string Geturl
        {
            get { return webDriver.Url; }
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class Assert_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.lambdatest.com";

        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }

        [Test]
        public void test_asserturl()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);

            // Site Title - Cross Browser Testing Tools | Free Automated Website Testing | LambdaTest

            string pattern = @"\b[Cross]\w+";

            String site_title = brow.Title;
            Console.WriteLine("The site title is " + site_title);

            Assert.That(site_title, Does.Match(pattern));
            Console.WriteLine("Site title match successful");

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

string pattern = @"\b[Cross]\w+";
Enter fullscreen mode Exit fullscreen mode

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

Assert.That(site_title, Does.Match(pattern));
Enter fullscreen mode Exit fullscreen mode

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

Assert.That(arg1, Is.Empty);

Assert.That(arg1, Is.Not.Empty);
Enter fullscreen mode Exit fullscreen mode

Example

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Assert_Demo
{
    class Assert_Demo_1
    {
        String test_url = "https://accounts.lambdatest.com/login";
        IWebDriver webDriver;

        [SetUp]
        public void start_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();

        }

        [Test]
        public void test_null()
        {
            String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]";

            webDriver.Url = test_url;
            System.Threading.Thread.Sleep(4000);

            //IWebElement submit_element = webDriver.FindElement(By.XPath("//*[@id='app']/section/form/div/div/button"));

            IWebElement user_name_element = webDriver.FindElement(By.XPath(user_name_xpath));

            user_name_element.SendKeys("abc@gmail.com");

            String user_name = user_name_element.GetAttribute("value");

            Console.WriteLine("User Name" + user_name);

            Assert.That(user_name, Is.Empty);

            // submit_element.Submit();
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            webDriver.Quit();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]";

IWebElement user_name_element = webDriver.FindElement(By.XPath(user_name_xpath));

user_name_element.SendKeys("abc@gmail.com");
String user_name = user_name_element.GetAttribute("value");
Console.WriteLine("User Name" + user_name);

Assert.That(user_name, Is.Empty);
Enter fullscreen mode Exit fullscreen mode

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

Is.Null(object expected);
Enter fullscreen mode Exit fullscreen mode

c. Boolean

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

Syntax

Is.Null(object expected);
Enter fullscreen mode Exit fullscreen mode

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:

int[] arr = new int[] {6, 7, 8};
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(arr, Is.All.GreaterThan(4));
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(arr, Is.All.LessThan(20));
Enter fullscreen mode Exit fullscreen mode

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:

Assert.That(arr, Is.All.InstanceOf<Int32>());
Enter fullscreen mode Exit fullscreen mode

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

// Assert is raised if arr does not contain 3 items
Assert.That(arr, Has.Exactly(3).Items);
Enter fullscreen mode Exit fullscreen mode

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:

// Assert is raised if arr does not contain unique values
Assert.That(arr, Is.Unique);
Enter fullscreen mode Exit fullscreen mode

g. Empty

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

Example:

Assert.That(arr, Is.Not.Empty);
Assert.That(arr, Is.Empty);
Enter fullscreen mode Exit fullscreen mode

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:

//Check if a particular element is present in the collection
Assert.That(array, Contains.Item(6));
Enter fullscreen mode Exit fullscreen mode

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.

Note- JSON Validator & Formater beautifies and debugs JSON data in an elegant polygonal interface. This JSON Validator by LambdaTest is free, easy to use, and doesn't have any ads or popups. Try now!

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

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using OpenQA.Selenium.Support.UI;

namespace Assert_Demo
{
    class Assert_Demo_1
    {
        String test_url = "https://lambdatest.github.io/sample-todo-app/";
        IWebDriver driver;

        [SetUp]
        public void start_Browser()
        {
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();

        }

        [Test]
        public void test_assert()
        {
            driver.Url = test_url;
            String itemName = "Adding item to the list";
            String url_title = "Sample page - lambdatest.com";

            System.Threading.Thread.Sleep(4000);

            Assert.That(url_title, Is.EqualTo(driver.Title));

            // Click on First Check box
            IWebElement firstCheckBox = driver.FindElement(By.Name("li1"));
            firstCheckBox.Click();

            // Click on Second Check box
            IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
            secondCheckBox.Click();

            // Enter Item name
            IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
            textfield.SendKeys(itemName);

            // Click on Add button
            IWebElement addButton = driver.FindElement(By.Id("addbutton"));
            addButton.Click();

            // Verified Added Item name
            IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
            String getText = itemtext.Text;

            // Check if the newly added item is present or not using
            // Condition constraint (Boolean)
            Assert.That((itemName.Contains(getText)), Is.True);

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }

        [TearDown]
        public void close_Browser()
        {
            driver.Quit();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
textfield.SendKeys(itemName);

// Click on Add button
IWebElement addButton = driver.FindElement(By.Id("addbutton"));
addButton.Click();
Enter fullscreen mode Exit fullscreen mode

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.

// Verified Added Item name
IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
String getText = itemtext.Text;

// Check if the newly added item is present or not using
// Condition constraint (Boolean)
Assert.That((itemName.Contains(getText)), Is.True);
Enter fullscreen mode Exit fullscreen mode

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.

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

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.IE;
using System.Configuration;
/* For using Remote Selenium WebDriver */
using OpenQA.Selenium.Remote;

namespace NUnit_Demo_1
{
    class NUnitDemo_1
    {
        IWebDriver driver;

        [SetUp]
        public void startBrowser()
        {
            String username = "user-name";
            String accesskey = "access-key";
            String gridURL = "@hub.lambdatest.com/wd/hub";

            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.SetCapability("user", "user-name");
            capabilities.SetCapability("accessKey", "access-key");
            capabilities.SetCapability("build", "Assert Test using LambdaTest (C#)");
            capabilities.SetCapability("name", "Assert Test using LambdaTest (C#)");
            capabilities.SetCapability("platform", "Windows 10");
            capabilities.SetCapability("browserName", "Internet Explorer");
            capabilities.SetCapability("version", "11.0");
            capabilities.SetCapability("ie.compatibility", 11001);

            driver = new RemoteWebDriver(new Uri("https://" + username + ":" + accesskey + gridURL), capabilities, TimeSpan.FromSeconds(600));

            driver.Manage().Window.Maximize();
        }

        [Test, Order(1)]
        public void test_nunit_assertion_1()
        {
            driver.Url = "https://www.lambdatest.com";
            System.Threading.Thread.Sleep(4000);

            IWebElement login_button = driver.FindElement(By.XPath("//*[@id='navbarSupportedContent']/ul/li[6]/a"));
            login_button.Click();

            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);

            String expected_title = "login - Lambdatest";

            String actual_title = driver.Title;

            Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase);

            Console.WriteLine("Test 1 Passed");
        }

        [Test, Order(2)]
        public void test_nunit_assertion_2()
        {
            driver.Url = "https://accounts.lambdatest.com/login";

            String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]";
            String password_xpath = "//*[@id='app']/section/form/div/div/input[2]";
            String submit_button_xpath = "//*[@id='app']/section/form/div/div/button";

            System.Threading.Thread.Sleep(4000);

            IWebElement user_name_element = driver.FindElement(By.XPath(user_name_xpath));
            user_name_element.SendKeys("validuser@gmail.com");

            // Raise assert if user-name is Empty
            String user_name = user_name_element.GetAttribute("value");
            Console.WriteLine("User Name" + user_name);
            Assert.That(user_name, Is.Not.Empty);

            IWebElement password_field = driver.FindElement(By.XPath(password_xpath));
            password_field.SendKeys("Valid_Password");

            // Raise assert if password is Empty
            String password = password_field.GetAttribute("value");
            Assert.That(password, Is.Not.Empty);

            //Click the Login button
            IWebElement submit_element = driver.FindElement(By.XPath(submit_button_xpath));

            submit_element.Submit();

            System.Threading.Thread.Sleep(2000);

            // Since the login is successful, we check if the Welcome page is launched
            String expected_title = "Welcome - LambdaTest";

            String actual_title = driver.Title;

            Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase);

            Console.WriteLine("Test 2 Passed");
        }

        [TearDown]
        public void closeBrowser()
        {
            driver.Quit();
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

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.

String username = "user-name";
String accesskey = "access-key";
.......................................................................
.......................................................................
driver = new RemoteWebDriver(new Uri("https://user-name:access-key@hub.lambdatest.com/wd/hub"), capabilities, TimeSpan.FromSeconds(600));
Enter fullscreen mode Exit fullscreen mode

The browser capabilities are generated using the LambdaTest capabilities generator.

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("user", "user-name");
capabilities.SetCapability("accessKey", "access-key");
capabilities.SetCapability("build", "Assert Test using LambdaTest (C#)");
capabilities.SetCapability("name", "Assert Test using LambdaTest (C#)");
.......................................................................
Enter fullscreen mode Exit fullscreen mode

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.

IWebElement login_button = driver.FindElement(By.XPath("//*[@id='navbarSupportedContent']/ul/li[6]/a"));
login_button.Click();
Enter fullscreen mode Exit fullscreen mode

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.

String expected_title = "login - Lambdatest";
String actual_title = driver.Title;
Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase);
Enter fullscreen mode Exit fullscreen mode

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.

String user_name_xpath = "//*[@id='app']/section/form/div/div/input[1]";
..............................................................................
..............................................................................
IWebElement user_name_element = driver.FindElement(By.XPath(user_name_xpath));
user_name_element.SendKeys("validuser@gmail.com");
..............................................................................
Enter fullscreen mode Exit fullscreen mode

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.

String expected_title = "Welcome - LambdaTest";
String actual_title = driver.Title;

Assert.That(expected_title, Is.EqualTo(actual_title).IgnoreCase);
Enter fullscreen mode Exit fullscreen mode

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 ☺

💖 💪 🙅 🚩
himanshusheth004
himanshuseth004

Posted on March 26, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related