Header Ads

Assertion in Selenium Webdriver using C#

 
Check or Verify


Introduction

Selenium Webdriver would easily automate any scenario on web app with its feature rich API's for web automation. However when we are developing test automation framework we would require to assert on certain steps of the test case that is being automated. The responsibility of  such assertion methods are not part of Selenium Webdriver library and requires us to rely on external assertion frameworks or develop our own methods to able to assert on certain steps.


What is Assertion Library?

Assertion is to verify if the expected behaviour after certain action on application is as expected or correct as per the test step.

A library which provides this functionality without explicitly writing code with condition but by directly calling the supporting function of library which could satisfy the verification condition and also supports different data types or types as variation to same condition.

Examples:

  • NUnit.Framework
  • Microsoft.VisualStudio.TestTools.UnitTesting
  • assert.xunit
  • FluentAssertions


Types of Assert Methods?

Assert Methods are broadly classified into below categories. There are many methods in assert class with multiple overloaded variations in implementation. Below is example assertion class categories and simple example of each type. (NUnit example)


Assert Types

About Assert

Assert Syntax
Equality Asserts Two arguments are equal Assert.AreEqual( int expected, int actual );
Identity Asserts Same objects are referenced by the two arguments Assert.AreSame( object expected, object actual );
Condition Asserts Tests specific condition Assert.IsTrue( bool condition );
Comparison Asserts Tests one object is greater than than another Assert.Greater( int arg1, int arg2 );
Type Asserts Assertions about the type of an object Assert.IsInstanceOfType( Type expected, object actual );
Exception Asserts Verify that it throws a particular exception Exception Assert.Throws( Type expectedExceptionType, TestDelegate code );
Utility Methods Utility methods, Pass(), Fail(), Ignore() and Inconclusive()  Assert.Pass();
String Assert Examines string values StringAssert.Contains( string expected, string actual );
Collection Assert Examining collections  CollectionAssert.AllItemsAreInstancesOfType( IEnumerable collection,
Type expectedType );
File Assert Comparing two files FileAssert.AreEqual( Stream expected, Stream actual );
Directory Assert Asserts about file system directories DirectoryAssert.AreEqual( DirectoryInfo expected, DirectoryInfo actual );

Simillary, there are assert methods available in respective assertion frameworks of MS Test, XUnit and FluentAsserions.


Fluent Assertions:

Fluent assertions are extensive set of assertion methods which supports .Net Framework and are written in TDD, BDD style tests.

For example:

String actual = "AutomationGlance";

actual.Should().StartWith("Automation").And.EndWith("Glance").And.Contain("Au").And.HaveLength(16);


Best Practices of Assertion in UI Test Automation

  • Do not forget to add Assert call wherever applicable in a particular test case otherwise the test would always pass.
  • Giving a message as argument in Assert statement will make the result analysis easier when assert fails
  • There should be at least one assert method per test case, there can be multiple assertions in test case if there are multiple expected results in one manual test case that is automated.
  • If one assert fails in a test case then the remaining part after the assert call won't be executed and test will be marked as failed.

No comments

automationglance.com. Powered by Blogger.