Assertions in TestNG : Selenium Webdriver

Assertions in selenium webdriver are nothing but the verifications or checkpoints during the execution of the program.

In Selenium we donot have any assertions, TestNG provides the assertions to verify a particular condition. Without assertions, we will not able to decide whether a testcase is failed or not.

But why we need assertions when we can get to know the details of the execution either using log4j logs or using System.out.println(), Assertion are mainly used for designing the TestNG reported or result.

If we log4j logs or System.out.println(), those will have no effect on the TestNG reporter / results. We use assertions in selenium to compare various values, like comparing strings, comparing objects, to perform relational operation results.

Important Note : If a assertions result is passed then that particular assertion will not have any effect on the Test results, only assertion failures affects the result. If there is no failure then TestNG by default considers the test case as passed.

A test case will be considered as failed, either if assert statement is resulted in fail or an exception is thrown inside a testcase.

AssertionError

When an assert statement results in failure, TestNG throws AssertionError to fail the testcase. Be aware that it is AssertionError not AssertionException. Also there is no such thing called as AssertionException

AssertionError is subclass to Error class, Error and Exception are subclass of Throwable, I hope you remember the difference between error and exception.

AssertionError class has 9 Overloaded constructors, all the Constructors accepts only one parameter but with different parameter type like : boolean, char, float, int, long, double, String, Object along with these, there is also a default Constructor (no-parameter constructor).

Types of Assertions :
There are two types of assertions present in the TestNG :

  • Assert class / Hard Assert
  • SoftAssert class / verify

1. Assert class / Hard Assert

Assert class is present in org.testng.Assert class in TestNG under Selenium webdriver, this assert also known as Hard assert.

Hard assert compares two items and results in pass or fail. But incase if the assert is failed then the code after the assert statement will not be executed and because of this only we are calling it as Hard Assert.

All the method present in Assert class are static methods so we can access all the methods using class reference, so no need to create object

Special Condition for Asserts : Asserts must be inside @Test method only, if we write assert statement inside a normal method or inside a main method then TestNG will not consider the failure or pass. Below image shows the example of failure and pass testng-assert-in-main-method

In above image you can see that I have compared two values 2 and 1 as we know both values are not same and the assert fails but it is not informed in the TestNG Result tab.
Similarly in case of pass also the result information will not be passed to TestNG tab.

Asserts present in the Assert Class/ Hard Asserts : I am explaining all the asserts present in the Assert Class Sequentially, This order may not follow on the eclipse/IDE suggestions

assertTrue :

this method gives option to the user to compare two items, if the comparison results in true then this Assert statement is passed otherwise fails the statement.

This statement will be useful when you evaluate more than one condition using relational operators.


										public class TestNGAssertTrue {
											@Test
											public void testAssertTrue()
											{
												Assert.assertTrue( 2 > 1 );
												System.out.println("compare 2 is greated than 1");
												
												Assert.assertTrue( "chercher tech".equals("karthiQ") );
												System.out.println("compared two not equal string & this should not be executed");
											}
										}
										

Output of Assert.asserTrue : assert-true-testng-selenium-webdriver

With this method we can also pass the failure message, saying why this condition is failed.

I hope you remember java concept of having same method with different parameters. If you know concept, please do comment at comment section.


										int i = 10;
										Assert.assertTrue(i < 300 && i > 5 , "Either 'i' is not less than '300' or not greater than '5'" );	
										


assertFalse :

Similar to assertTrue assertion, we have assertFalse statement also in TestNG assertions.

assertFalse statement evaluates the given condition and if the condition results in false then assertFalse statement will pass the assertion but if the evaluation results in true then assertFalse statement fails the testcase.

assertFalse is also an overloaded method


										public class testAssertFalse {
											@Test
											public void testAssertFalse()
											{
												Assert.assertFalse( 2 > 30 , "comparing 2 greater than 30");
												System.out.println("this print will be executed as condition results in false");

												Assert.assertFalse(true == true);
												System.out.println("this will not be executed as condition results in true");
											}
										}
										

Output of Assert.assertFalse : assert-false-testng-selenium-webdriver

fail :

Assert.fail method fails the current test with the given message, user not only can set the error message but also they can set the exception which caused the failure.


										public class TestNGFail {
											@Test
											public void testFail()
											{
												Assert.fail("test purpose", new NoSuchFrameException("user thrown exception"));
											}
										}
										

Output of the Assert.fail fail-custom-exception-testng-selenium-webdriver

assertEquals :

Assert.assertEquals method compares given two methods to check whether both are equal or not, If both expected and actual values are different then assertEquals method throws AssertionError.

assertEquals method not only compares the object but also compares the array, element by element will be compared in arrays.

Below example compares the empty list and the lists with a value.


										public class TestNGAssertEquals {
											public static void main(String[] args) {
												List l = new ArrayList();
												List l2 = new ArrayList();
												
												// compare the empty lists
												Assert.assertEquals(l, l2);
												
												l2.add(3);
												// compare the list with element
												Assert.assertEquals(l, l2, "both List are not same");
											}
										}
										

Output of Assert.assertEquals : assertequals-testng-selenium-webdriver

We can compare various items using assertEquals method in TestNG with selenium webdriver. Below are few overloaded methods present in TestNG

1. byte[] : actual method is assertEquals(byte[] actual, byte[] expected)
2. short[] -> Compares two short arrays
3. int[] -> Compares two in arrays
4. boolean[] -> Compares two boolean arrays
5. char[] -> Compares two char arrays
6. float[] -> Compares two float arrays
7. double[] -> Compares two double arrays
8. long[] -> Compares two long arrays
9. Object -> Compares two objects
10. String -> Compares two Strings
11. double Delta -> explained below.
12. float Delta
13. long ->Compares two long values
14. boolean -> Compares two boolean values
15. byte -> compares two byte values
16. char -> Compares two char values
17. short -> Compares two short values
18. int -> Compares two int values
19. Collection<?> -> Compares two Collections
20. Iterator<?> -> Compares two iterators
21. Iterable<?> -> Compares two Iterable
22. Set<?> -> Compares two sets
23. Map<?, ?> -> compares two maps


assertEquals : Delta

This method accepts three double values and perform a subtraction between value1-value2, if the result of subtraction is less than the third parameter then assertion is passed otherwise assertion failed and throws AssertionError


										public class TestNGAssertEquals {
											public static void main(String[] args) {
												double value1 = 50;
												double value2 = 10;
												double value3 =70;
												Assert.assertEquals(value1, value2, value3, "Subtraction deleta is less than expected delta");
											}
										}
										


assertNotNull :

assertNotNull method compares the given value with null, if given value is null then TestNG throws AssertionError error.


										public class TestNGAssertNotNull {
											static WebDriver driver ;
											public static void main(String[] args) {
												//check driver is not null
												Assert.assertNotNull(driver);
											}
										}
										

Output of Assert.assertNotNull : assertnotnull-testng-selenium-webdriver

assertNull :

assertNull method compares the given value/object with null, if the given value is null then this assertion is passed but given value is not null then this message throws AssertionError


										public class TestNGAssertNull {
											public static void main(String[] args) {
												// set the geckodriver.exe property
												System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");
														
												WebDriver driver = new FirefoxDriver();
												//check driver is null
												Assert.assertNull(driver);
											}
										}
										

Output of Assert.assertNull : assertnull-testng-selenium-webdriver

assertSame :

assertSame method compares the address of the objects, if the address of the object are same then assertion is passed otherwise TestNg throws an error AssertionError.

The difference between assertEquals and assertSame is, assertEquals compares the object contents where as assertSame compares the address.

Note : When we create a string using double Quotes, it point to the already existing same string address if any.

But when we create a String using new keyword, object will be created newly even though same string is present in memory.


									public class TestNGAssertSame {
										public static void main(String[] args) {
											String s1 = "Chercher tech";
											String s2 = "Chercher tech";
											// strings created with double quotes keyword will have same addresses if content is same
											Assert.assertSame(s1, s2, "Strings not sharing same address when we create using double quotes");
											
											String s3 = new String("Google");
											String s4 = new String("Google");
											// compare using Assert Equals, assertion passes
											Assert.assertEquals(s3,  s4, "string content comparision failed");
											// strings created with new keyword will have different addresses
											Assert.assertSame(s3, s4, "String as not sharing same address when created using 'new' keyword");
										}
									}
									

Output of Assert.assertSame program : assertsame-testng-selenium-webdriver

assertNotSame :

assertNotSame method compares the given objects' addresses are not same, if both the addresses are same then TestNG throws the AssertionError


									public class TestNGAssertNotSame {
										public static void main(String[] args) {
											String s1 = "Chercher tech";
											System.out.println("s1 address is : "+System.identityHashCode(s1));
											String s2 = "Chercher tech";
											System.out.println("s2 address is : "+System.identityHashCode(s2));
											// strings created with double quotes keyword will have same addresses if content is same
											Assert.assertNotSame(s1, s2, "Strings not sharing same address when we create using double quotes");
										}
									}
									

Output of Assert.assertNotSame : assertnotsame-testng-selenium-webdriver

Option Present or Not in Dropdown in seleium

assertEqualsNoOrder :

assertEqualsNoOrder method compares the two Object arrays irrespective of the order of values present inside.

If both arrays contains same values in different/same order then assertion is passed. If any extra value is present in ay of the array then assertion is failed.


										public class TestNGAssertEqualsNoOrder {
											public static void main(String[] args) {
												Object[] objArray1 =  new Object[3];
												objArray1[0] = 1;
												objArray1[1] = "chercher tech";
												objArray1[2] = false;
												
												Object[] objArray2 =  new Object[3];
												objArray2[0] = "chercher tech";
												objArray2[1] = false;
												objArray2[2] = 1;
												// compare two arrays
												Assert.assertEqualsNoOrder(objArray1, objArray2);
												System.out.println("Lists compared and AssertionError didnot occur");
											}
										}
										

output of the Assert.assertEqualsNoOrder : assertequalsnoorder-testng-selenium-webdriver

assertNotEquals :

assertNotEquals method compares and passes the assertion if both object are not same, Assertion results in failure if both objects are same. Similar to assertEquals method, this method is also overload with same kind of parameters.


										public class TestNGAssertNotEquals {
											public static void main(String[] args) {
												int i1 =10;
												int i2 = 10;
												// compare i1, i2
												Assert.assertNotEquals(i1, i2, "i1, i2 are same");
											}
										}
										

Output of Assert.assertNotEquals : assertnotequals-testng-selenium-webdriver

These all are the assertions present in the TestNG Assert class

SoftAssert / Verify

SoftAssert in TestNG is nothing but brother of Assert , the SoftAssert not only asserts a given conditions, but also gives a chance to user to continue the execution after Assertion failure.

It is not mandatory to continue the execution of test case after the assertion failure, but sometimes you might be checking a minor value. So just because minor verification failed you may not want to stop the execution of the testcase.

Anyways once the soft assertion is failed the TestNG throws AssertionError, instead of throwing error immediately TestNG throws the error just before completing the testcase.

Example : Consider you have a testcase which verifies the 100 CSS properties. Do you want to stop the Execution just because first CSS property is not matching ?, NO.

What we would like is atleast we want to execute the testcase for all th check points/ verification so that we will get to know how may properties are matching and how many are not matching.

All the assertion present in the SoftAssert class in TestNG are non-static methods.

Why people call the SoftAssert as Verify ?
There is no such thing as verify in Selenium webdriver, but there was a Selenium RC few years back, the checkpoints present in that RC are verify methods, so people remember those verify methods and they confuse it along with webdriver. So calling SoftAssert as a Verify is a mistake that we are continually making.

Methods present in SoftAssert :

Whoever knows Selenium Webdriver will say that SoftAssert class has all the methods present in Assert class, is that true ? No.

SoftAssert class has only two methods:
1. doAssert (protected method, we would not be using it)
2. assertAll()

You might have a doubt that I am able to see almost all the methods like Assert class inside SoftAssert class. Yes, You might have seen methods but have you ever heard a concept of Inheritance in Java

Architecture of SoftAssert :

All the assertion methods are present in Assertion class, the SoftAssert class extends the Assertion class. So through inheritance SoftAssert Class allows the user to access the all the methods present in the Assertion class.

I hope you have read all the assertions in testNG in hard Assert tutorials, so will skip to the assert present in the SoftAsssert.

assertAll :

We have SoftAssert to continue the testcase execution even when few assertions are failed. assertAll is the method which makes the SoftAssert class to execute even when there are assertion failures.

You cannot write assertAll method alone in a test case, assertAll method must be used along with some other assert method.

TestNG executes the teststeps only which are in between assert statement and assertAll statement, TestNG will not execute the statements which are present after the assertAll statement in Selenium Webdriver

So most of the people will use the assertAll statement as the last statement in the testcase.


										public class TestNgSoftAssert {
											public static void main(String[] args) {
												
												// set the geckodriver.exe property
												System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");
														
												WebDriver driver = new FirefoxDriver();
												driver.get("http://chercher.tech");
												
												SoftAssert sa = new SoftAssert();
												// compare the titles
												sa.assertEquals(driver.getTitle(), "Google", "title is not google");
												//try to print the statement
												System.out.println("this step should execute");
												System.out.println("this step also gets executed");
												// execute all the statement before below step
												sa.assertAll();
												// below step will not be executed
												System.out.println("This step will not be executed");
											}
										}
										

Output of assertAll method : assertall-testng-selenium-webdriver

Create QR Code

Custom Soft Assertions

We have so many assertions in TestNG, but sometimes we want to write our own assertion instead of using the existing TestNG assertion. In this tutorial we learn how to write custom TestNG assertion in selenium webdriver.

Steps to Write custom Assertions :

1. Create a class called CustomException and extend SoftAssert
2. We are extending the SoftAssert class so that we can use all the methods present in that class.
3. Create method called assertElementIsTextbox, the purpose of the method is to verify whether given element is TextBox or not. If it is not TextBox we should fail the testcase by raising the AssertionError, This method should accept the WebElement as parameter.
4. Get the tagname and Type of the element (I am considering that if an element is textbar then it will have tagname as 'input' and type as 'text'. You can have your own consideration for your element)
5. First verify whether given Element has tagname as 'input', type as 'text'
6. If 5 is not true raise AssertionError on else block.

										public class CustomAssertion extends SoftAssert{
											public void assertElementIsTextbox(WebElement element){
												// get the tag name and type attribute
												String tag = element.getTagName();
												String type = element.getAttribute("type");
												// verify whether tag is input
												if(tag.equals("input") && type.equals("text")){
													System.out.println("******Element is Textbar*******");
												}
												else{
													throw new AssertionError("Element isn't textbar, tag, type are donot belong to textbox");
												}
											}
										}
										


Create object to the CustomAssertion class and call the assertElementIsTextbox method and pass the webelement as parameter.


										public class UseCustomAssertion {
											public static void main(String[] args) {

												// set the geckodriver.exe property
												System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");
														
												WebDriver driver = new FirefoxDriver();
												driver.get("http://google.com");
												
												WebElement searchBar = driver.findElement(By.name("q"));
												
												CustomAssertion ca = new CustomAssertion();
												ca.assertElementIsTextbox(searchBar);
											}
										}
										

Above program is Pass scenario for Custom Assertion: customassertion-failure-selenium-webdriver

Failure scenario for custom Assertion, do change the element:


										WebElement searchBar = driver.findElement(By.tagname("a"));
										CustomAssertion ca = new CustomAssertion();
										
customassertion-failure-selenium-webdriver

Create Headless Firefox Browser in Selenium webdriver

Built-in Assertions in Java

Java provides built-in assertions to verify the conditions, you must enable assertion in Your IDE to use them. If assertion are not enable then assertion will not have any effect on your code.

To enable assertion Java with Eclipse, Go to VM arguments in Run Configuration and set values as -ea, ea is short form of enable assertions.

assert keyword is used during program development to create an assertion, which is a condition that should be true during the execution of the program.


										assert condition
										

For example, you might have a method that should always return a positive integer value. You might test this by asserting that the return value is greater than zero using an assert statement.

At run time, if the condition is true, no other action takes place. However, if the condition is false, then an AssertionError is thrown.

Assertions are often used during testing to verify that some expected condition is actually met. They are not usually used for code which is in production.

Similar to TestNG assertions Java assertion also will stop the execution the moment when assertion fails.

First form of the assert in java


										public class JavaAssert {
											public static void main(String[] args) {
												assert 1 > 9 ;
												System.out.println("hello");
											}
										}
										

Second form of the assert in java


										public static void main(String[] args) {
											assert 1 > 9  : "Error Message goes here";
											System.out.println("hello");
										}
										

Output of Java Assert : javaassert-selenium-webdriver

Assertions are introduced in JDK 1.4 and implemented using assert keyword in java.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech