Hard, Soft Assert and custom assert in selenium

Test Automation

TestNG provide wide range of assert options which helps an automation tester to validate actual and expected.
Most commonly, there are 2 types of assert in selenium-

1- Hard Assert- Which skips remaining script if Assert is failed, skipping remaining assert. Say there are 10 Assert inside a script and 2nd Assert is failed, in that case remaining assert will not be executed. To overcome this soft asserts are introduced .

Common Assert method-
1- Assert.fail(“Message”)
2- Assert.pass(“Pass”);
3- Assert.assertEquals(Expected, Actual, message)

2- Soft Assert- which validated all assert and at the end it checks whether all asserts are passed or not.

//Declaration of soft assert object
SoftAssert sostassert = new SoftAssert();

sostassert.assertEquals(Actualtext1, Expectedtext1, “Message”);
sostassert.assertEquals(Actualtext2, Expectedtext2, “Message”);
sostassert.assertEquals(Actualtext3, Expectedtext3, “Message”);

Suppose if second assert fails, script will continue unlike hard assert and at the very end below command is used which validate all softassert if any of them is failed then it failed overall softassert.
s_assert.assertAll();

Custom collection assert-One more way of using assert using collections which is almost work like softassert. This way of using assert is useful and effective where a lot of validation is required and validation result(output) is needed at some different test.

Create a collection object-
public static ArrayList CustomAssert = new ArrayList

Now whenever you need to validate any data, which means comparison of actual and expected and if there is any mismatch happened add that failed value in above declared collection object. and use that collection size to identify whether any failure happened during that particular flow.

example-

Step 1-
if(driver.findelement.gettext(“XYZ”).equals(“ABC”)
{

}
else
{
CustomAssert.add(“mismatch happened”)
}

Step 2
if(driver.findelement.gettext(“XYZ”).equals(“XYZ”)
{

}
else
{
CustomAssert.add(“mismatch happened”)
}

Step 3
if(driver.findelement.gettext(“XYZ”).equals(“abc”)
{

}
else
{
CustomAssert.add(“mismatch happened”)
}

In above 3 steps, 2 asserts are failed(first and third), first and third step therefore collection CustomAssert will have 2 values filled, At the end of the test size of collection(CustomAssert ) can be counted if it is greater than zero then hard assert can be used to make validation for these particular test steps as failed.

if(CustomAssert.size()>0)
{
Assert.fail(“There are “+ CustomAssert.size()+ happened during test execution.)
}

Summary- Using collection(above method) for validation can help us in finding out validations which happened in different classes. Using above custom Assert a tester can determine or use output of one class in other classes for different automation purposes

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).

I have experienced in automation framework designing, planning, doing maturity assessment of automation (F/W, scripts) and automation tool evaluation. Good understanding in various kinds of automation- that includes Application, and Process Automation. Implemented various CI tools, Used VSO, Jenkins, and Bamboo for implementing Test automation in CI/CD and robotic process automation using open source solution(Selenium). Skilled in leading automation team, managing vendors in automation & functional QA projects.

Tool Knowledge-
Selenium WebDriver, IBM RFT, HP UFT, Appium, Sahi Pro, Sikuli
Mobile Automation- Appium, Perfecto, Nokia Automation Tools- Auster, Phoenix, Trap
Performance Testing tool- IBM RPT, JMeter, Empirix, Neoload HttpWatch.
CI Tools- VSO, Jenkins and Bamboo

I Completed my B.Tech in computer science from UPTU University – Lucknow in the year 2007.

I like to write on selenium/Automation in my spare time . I started writing this blog 2 year ago and now my blog has grown upto 1000+Subscribers.

Drop me an email if you have any query or any help is required in Automation.

No Comments

Leave a Reply

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

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Test Automation
What is Headless browser automation

Hello Readers, In this post we will read about headless browser automation and how to use or invoke different headless browser using selenium. First of all lets understand What is a headless browser? A headless browser is a web-browser without a GUI-Graphical User Interface. The major difference with browser having …

Shares