{"id":39368,"date":"2016-08-30T09:39:13","date_gmt":"2016-08-30T04:09:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=39368"},"modified":"2016-08-30T10:01:30","modified_gmt":"2016-08-30T04:31:30","slug":"soft-assertions-in-selenium-using-testng","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/soft-assertions-in-selenium-using-testng\/","title":{"rendered":"Soft Assertions in Selenium using TestNG"},"content":{"rendered":"<p style=\"text-align: justify;\">Assertions are used to perform various kinds of validations in the tests and help us to decide whether the test has passed or failed. There are two types of assertions in <a title=\"automated testing services\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\">Selenium<\/a> that we can place in our test scripts using TestNG:<\/p>\n<ol style=\"text-align: justify;\">\n<li>Hard Assertions<\/li>\n<li>Soft Assertions<\/li>\n<\/ol>\n<p style=\"text-align: justify;\"><strong>Hard Assertions<\/strong>: As the name suggests, these assertions put a strict restriction on the test script in which it is placed. When using hard assertions in the test scripts, your test script will stop executing when the assertion fails, and the test will be failed in TestNG report.<\/p>\n<p style=\"text-align: justify;\">We will discuss following scenarios to understand hard assertions:<\/p>\n<p style=\"text-align: justify;\"><strong>Scenario 1:<\/strong> Let\u2019s assume you have a test case with multiple validations using hard assertions and all of them are passed. Here is the code:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39370\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/1-300x180.jpg\" alt=\"1\" width=\"300\" height=\"180\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/1-300x180.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/1.jpg 530w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">Now, let us analyze the output of the above code:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39372\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/2-300x168.jpg\" alt=\"2\" width=\"300\" height=\"168\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/2-300x168.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/2-624x350.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/2.jpg 819w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">As seen above, the console output contains the text \u201cLine executed after assertion passed\u201d after all the assertions have passed and the test is marked as passed as expected.<\/p>\n<p style=\"text-align: justify;\"><strong>Scenario 2:<\/strong> Now let\u2019s analyze the scenario when one of the hard assertions fails:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39373\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/3-300x180.jpg\" alt=\"3\" width=\"300\" height=\"180\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/3-300x180.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/3.jpg 548w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39374\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/4-300x97.jpg\" alt=\"4\" width=\"300\" height=\"97\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/4-300x97.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/4-624x201.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/4.jpg 853w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">As seen above, the text is not printed, and the code following the assertion is never executed after assertion failure.<\/p>\n<p style=\"text-align: justify;\"><strong>Soft Assertions:<\/strong> If you need to execute the remaining test case even after an assertion fails, and you also want to report assertion and test case failure in TestNG report, TestNG provides soft assertions for this. Unlike hard assertions, they do not bring the entire program to a halt, i.e. a test script does not stop running even if an assertion fails, but the test itself is failed to indicate the right result.<\/p>\n<p style=\"text-align: justify;\">The <strong><em>softAssert.assertAll() <\/em>method<\/strong> does the trick. This method collates all the failures and decides whether to pass the test case or not at the end. TestNG library itself offers the facility to perform Soft Assertions in your test without writing any custom logic. We will take help to understand how soft assertions are used in TestNG with the code snippets mentioned below:<\/p>\n<p style=\"text-align: justify;\"><strong>Scenario 1<\/strong>: When one of the soft assertions fails in a single test script:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39375\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/5-300x124.jpg\" alt=\"5\" width=\"300\" height=\"124\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/5-300x124.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/5.jpg 624w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39376\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/6-300x162.jpg\" alt=\"6\" width=\"300\" height=\"162\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/6-300x162.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/6-624x338.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/6.jpg 848w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see, the test is failed, and the text is successfully printed on console even after a previous assertion has failed thus ensuring complete test execution.<\/p>\n<p style=\"text-align: justify;\"><strong>Scenario 2: <\/strong>Soft Assertions with multiple test cases<strong>. <\/strong>When using soft assertions with multiple test cases the result is not comprehensive if you end up using the same object of Soft Assert class in multiple test cases as shown below:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39377\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/7-300x126.jpg\" alt=\"7\" width=\"300\" height=\"126\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/7-300x126.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/7-624x263.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/7.jpg 973w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">The same object \u2018<strong>softAssert<\/strong>\u2019 is used with multiple tests in the above case. The expected output is \u2018SoftAssertionTest\u2019 test should pass (as all assertions have passed) and \u2018SoftAssertionSecondTest\u2019 test should fail as one of its assertions has failed.<\/p>\n<p style=\"text-align: justify;\"><strong>But since the object is same in both test cases, both test cases will fail. WHY? The <em>assertAll()<\/em> method when collating assertion failures uses the Soft Assert class object. It will collate all the assertion failures for the same object at a single time.<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong>In the above case, since the same object is used in different test cases when the control executes assertAll () method of the \u2018SoftAssertionSecondTest\u2019 test case, it will evaluate all the above four assertions (even if they are in different test cases) at the same time.<\/strong> <strong>As a result, all the test cases using this same object of Soft Assert class will fail as shown below:<\/strong><\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39378\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/8-300x207.jpg\" alt=\"8\" width=\"300\" height=\"207\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/8-300x207.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/8-624x431.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/8.jpg 848w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\"><strong>Solution: Use different objects of \u2018Soft Assert\u2019 class in different test cases (best practice) as shown below:<\/strong><\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39379\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/9-300x212.jpg\" alt=\"9\" width=\"300\" height=\"212\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/9-300x212.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/9-624x442.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/9.jpg 819w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\"><strong>This coding pattern will restrict <em>assertAll()<\/em> method assertions failure collations to only current test method thus giving expected result as below:<\/strong><\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-39380\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/10-300x223.jpg\" alt=\"10\" width=\"300\" height=\"223\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/10-300x223.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/10-624x464.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/10.jpg 762w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">Using soft assertions in your test scripts is a good practice and an effective way of handling your test execution. However, if you are writing test cases for config, then placing hard assertions should be the way. I hope this blog provides you a solution.<\/p>\n<p style=\"text-align: justify;\">That\u2019s it folks Cheerio! J<\/p>\n<p style=\"text-align: justify;\">\n","protected":false},"excerpt":{"rendered":"<p>Assertions are used to perform various kinds of validations in the tests and help us to decide whether the test has passed or failed. There are two types of assertions in Selenium that we can place in our test scripts using TestNG: Hard Assertions Soft Assertions Hard Assertions: As the name suggests, these assertions put [&hellip;]<\/p>\n","protected":false},"author":972,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":58},"categories":[1818,1,1816],"tags":[3949,1561,3952,25,3951,3950],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39368"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/972"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=39368"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39368\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=39368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=39368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=39368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}