top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Assertions in Selenium

While performing test automation, we come across many scenarios where we need to validate if the test case is passed or failed. This is where we use assertions. Assertions play an important role in selenium automation in identifying any flaws in the application.

What is Assertion?

Assertion is an expression which will verify the actual test outcome to the expected test outcome and provides an output whether the test is passed or failed. Assertions are really important because without assertions we won't get to know if the test is actually passed or failed, it is always either test is passed or test is failed.

Let us consider the below example to know why we need Assertions in our test cases.



In the above code after giving login details and logging into the application we are verifying the title of the page in the print statement. But we don't know if the page title is correct or not, here comes Assertions where we can give the expected title and validate it with the actual title of the page. We can use Assert in try..catch method, by using this we can capture details of failed test cases.

Types of Assertions in Selenium:

There are two types of assertions available in Selenium:

  1. Hard Assertions

  2. Soft Assertions

Hard Assertions in Selenium:

Hard assertions are used to test expected outcomes in a test case. In hard assertions, whenever an assert statement fails, it will throw an exception immediately and fail the test case and it will not execute any code after that failed assertion in that method. This is why it is known as “Hard” Assertion because if the expected outcome does not match actual outcome it will halt test execution. If an assertion fails “java,lang.AssertionError” is thrown.

There are various types of Hard Assertions. Some of them are as below:

  • AssertEquals: This Assertion method is used to compare expected and actual values. It will check if the actual value is the same as expected value. If the actual and expected values are the same then the test case is marked as passed. If the actual and expected values are not the same then it will fail the test case.

Syntax: Assert.assertEquals(actual, expected);

Example:


In the above example we can see that the title of the page is compared. As the actual and expected title of the page are the same so the test is passed.

  • AssertNotEquals: This Assertion method is opposite of AssertEquals(). In this method it compares the actual and expected values and if they are not the same then the test will be passed. If actual and expected values are the same then the test will be failed.

Syntax: Assert.assertNotEquals(actual, expected);

  • AssertTrue: This Assertion verifies if the boolean condition is true. If the boolean value is true then it will pass the test case. If the boolean value is false then it will fail the test case.

Syntax: Assert.AssertTrue(condition);

Example:


In the above example I am trying to verify if the "Open Menu" dropdown is there on the web page or not. So I am giving that condition in Assert.assertTrue() and verifyng it.

  • AssertFalse: This Assertion is opposite of AssertTrue(). If the boolean value of the test case is false then the test is passed else the test is failed.

Syntax: Assert.AssertFalse(condition);

  • AssertNull: This Assertion method is used to verify if a value is null. If the value is null then the test case will be passed else the test case will be failed.

Syntax: Assert.assertNull(object);

Example:



In the above example, String age value is null, so when I try to give age in AssertNull the test case is passed. If I give name then the test case will fail as the name value is "abc".

  • AssertNotNull: This Assertion method is used to verify if the value is not null. If The value is not null then the test case is failed else the test case is failed.

Syntax: Assert.assertNotNull(object);

Soft Assertions in Selenium:

In Soft Assertion WebDriver will not throw an exception immediately when the assert condition is failed. When an assertion is failed, test execution proceeds further with next steps of code.assertAll() method is invoked to throw all the exceptions that are caught during the process of Selenium test automation execution. Soft Assert should be used in the scenarios where failure of certain test scripts does not affect the execution of other test steps in that method.

Soft Assert uses methods that are used by Hard Assert such as,

  • AssertEquals

  • AssertTrue

  • AssertFalse

  • AssertNotEquals

At the end we have to call the assertAll() method to catch all the failed asserts.

Example:

In the above example, actual and expected tile is not the same, but an exception is not thrown immediately. All the lines of code that are below that failed assert statement are executed as we are using SoftAssert here. After calling the AssertAll() method, the assert throws the exception and the test is marked as failed.

Conclusion:

Assertions play a vital role when it comes to selenium test automation. Assertions are used to check if a test case is passed or failed. Hard assertions are used when we need to halt test execution when the assert condition is failed. On the other hand, soft assertions are used where the test execution continues even if the assert condition is failed and all those failed assertions are collected.






85 views0 comments

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev

Commentaires

Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page