{"id":418,"date":"2021-05-20T16:02:42","date_gmt":"2021-05-20T16:02:42","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/05\/20\/assert-and-verify-methods-in-selenium\/"},"modified":"2021-05-20T16:02:42","modified_gmt":"2021-05-20T16:02:42","slug":"assert-and-verify-methods-in-selenium","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/05\/20\/assert-and-verify-methods-in-selenium\/","title":{"rendered":"Assert and Verify Methods in Selenium"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"418\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Asserts <\/strong>and <strong>Verify <\/strong>methods are commonly used in Selenium for verifying or validating applications. In this tutorial, we will learn the why, when, and how of using these methods to make Selenium tests more efficient.<\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper tve-toc tve-elem-scroll tve-toc-expandable tcb-local-vars-root\" data-animation=\"slide\" data-bullet-style='{\"0\":\"tve-u-17399ff41d4\",\"1\":\"tve-u-17399ffc502\",\"2\":\"tve-u-17399ffedb7\"}' data-columns=\"1\" data-css=\"tve-u-1798a7b9319\" data-ct=\"toc-60733\" data-ct-name=\"Table of Contents 13\" data-distribute=\"false\" data-element-name=\"Table of Contents\" data-headers=\"h1,h3,h5\" data-heading-style='{\"0\":\"tve-u-1798a7b9321\",\"1\":\"tve-u-1798a7b9322\",\"2\":\"tve-u-1798a7b9323\"}' data-highlight=\"heading\" data-number-style='{\"0\":\"tve-u-17399fecc2c\",\"1\":\"tve-u-173dc8687ce\",\"2\":\"tve-u-173dc86929b\"}' data-numbering=\"none\" data-state-default=\"expanded\" data-state-default-d=\"expanded\" data-transition=\"slide\" style=\"\">\n<div class=\"thrive-colors-palette-config\" style=\"display: none !important\"><\/div>\n<div class=\"tve-toc-divider\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\">\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgb(217, 217, 217)\" data-style=\"tve_sep-1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<p><svg class=\"toc-icons\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-0\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-1\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-2\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><\/svg><\/p>\n<div class=\"tve-content-box-background\" data-css=\"tve-u-1798a7b931a\" style=\"\"><\/div>\n<div class=\"thrv_wrapper tve-toc-title tcb-icon-display reverse tve-no-dropzone tve-prevent-content-edit\" data-css=\"tve-u-1798a7b931b\" style=\"\">\n<div class=\"tve-content-box-background\" style=\"\"><\/div>\n<div class=\"tve-cb\" style=\"\">\n<div class=\"tve-toc-title-icon\" data-icon-code=\"icon-chevron-down-solid\" style=\"\"><svg class=\"tcb-icon\" data-id=\"icon-chevron-down-solid\" data-name=\"\" viewbox=\"0 0 24 24\"><path d=\"M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z\"><\/path><\/svg><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve_no_icons\">\n<div class=\"tcb-plain-text\" data-css=\"tve-u-1798a7b931c\" style=\"\">table of contents<\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<div class=\"tve-cb tve-toc-content tve-prevent-content-edit\" style=\"\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\" data-css=\"tve-u-1798a7b931d\" style=\"\">\n<div class=\"tve-content-box-background\" data-css=\"tve-u-1798a7b931e\" style=\"\"><\/div>\n<div class=\"tve-cb\"><\/div>\n<\/div>\n<div class=\"thrv_wrapper tve-toc-list tcb-no-delete tcb-no-save tcb-no-clone tve-no-dropzone\" data-css=\"tve-u-1798a7b931f\" style=\"\">\n<div class=\"tve-content-box-background\" data-css=\"tve-u-1798a7b9320\" style=\"\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"tve_ct_content tve_clearfix\">\n<div class=\"ct_column\">\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867807\" jump-animation=\"smooth\">Assertions<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867808\" jump-animation=\"smooth\">Types of Assertions<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867809\" jump-animation=\"smooth\">Hard Assertions<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867810\" jump-animation=\"smooth\">Verify in Selenium (also known as Soft Assertion)<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867811\" jump-animation=\"smooth\">Hard vs Soft Asserts in Selenium<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-1798a7b9322\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1621525867812\" jump-animation=\"smooth\">Difference between Assert and Verify in selenium<\/a><\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgb(217, 217, 217)\" data-style=\"tve_sep-1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1621525867807\"><strong>Assertions<\/strong><\/h3>\n<p>In Selenium, Asserts are validations or checkpoints for an application. Assertions state confidently that application behaviour is working as expected. One can say that Asserts in Selenium are used to validate the test cases. They help testers understand if tests have passed or failed.<\/p>\n<p><strong> <\/strong><\/p>\n<h3 class=\"\" id=\"t-1621525867808\"><strong>Types of Assertions<\/strong><\/h3>\n<ol>\n<li>Hard Assertions.<\/li>\n<li>Soft Assertions.<\/li>\n<\/ol>\n<h3 class=\"\" id=\"t-1621525867809\"><strong>Hard Assertions<\/strong><\/h3>\n<p>Hard Assertions are ones in which test execution is aborted if the test does not meet the assertion condition. The test case is marked as failed. In case of an assertion error, it will throw the \u201c<strong><em>java.lang.AssertionError<\/em><\/strong>\u201d exception.<\/p>\n<p>Let us explore different types of hard assertions with examples.<\/p>\n<p><strong>assertEquals()<\/strong> is a method that takes a minimum of 2 arguments and compares actual results with expected results. If both match, then the assertion is passed and the test case is marked as passed. assertEquals() can compare Strings, Integers, Doubles and many more variables.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a7cdda0\" style=\"\">\n<li data-css=\"tve-u-1798a84d675\" style=\"\">package tests;\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.Assert;<\/p>\n<p>public class Asserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String actualTitle = driver.getTitle();<br \/>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0String expectedTitle = &#8220;Practice Page&#8221;;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Assert.<em>assertEquals<\/em>(actualTitle,expectedTitle);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<\/p>\n<p>\u00a0\u00a0\u00a0 }<br \/>}<\/li>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>assertNotEquals()<\/strong> is a method that does the opposite of the assertEquals() method. In this case, the method compares the actual and expected result. But if the assertion condition is met if the two are not identical. If actual and expected results are not the same, the test case is marked as passed.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a7d8794\" style=\"\">\n<li data-css=\"tve-u-1798a7e0ea8\" style=\"\"><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb1\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb3\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb4\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb6\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb7\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" data-css=\"tve-u-1798a7e0eb8\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" style=\"display: none; line-height: 0;\"><\/span><span class=\"fr-marker\" style=\"display: none; line-height: 0;\"><\/span>package tests;\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.Assert;<\/p>\n<p>public class Asserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String actualTitle = driver.getTitle();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String expectedTitle = &#8220;Practice Page demo&#8221;;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Assert.<em>assertNotEquals<\/em>(actualTitle,expectedTitle);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<\/p>\n<p>\u00a0\u00a0\u00a0 }<br \/>}<\/li>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>assertTrue()<\/strong>: This Assertion verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a7e9dc1\" style=\"\">\n<p data-css=\"tve-u-1798a7ee62d\" style=\"\">package tests;<\/p>\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.Assert;<\/p>\n<p>public class Asserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 boolean verifyTitle = driver.getTitle().equalsIgnoreCase(&#8220;Practice Page&#8221;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Assert.<em>assertTrue<\/em>(verifyTitle);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<\/p>\n<p>\u00a0\u00a0\u00a0 }<br \/>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>assertFalse()<\/strong>: This method works opposite of that of assertTrue(). The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, then the assertion passes the test case.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a7f5e12\" style=\"\">\n<li data-css=\"tve-u-1798a7fa922\" style=\"\">package tests;\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.Assert;<\/p>\n<p>public class Asserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 boolean verifyTitle = driver.getTitle().equalsIgnoreCase(&#8220;Practice Page&#8221;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Assert.<em>assertFalse<\/em>(verifyTitle);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<\/p>\n<p>\u00a0\u00a0\u00a0 }<br \/>}<\/li>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>assertNull(): <\/strong>This method verifies if the expected output is null. If not, the value returned is false.<\/p>\n<p><strong>assertNotNull():<\/strong> This method works opposite to that of the assertNull() method. The assertion condition is met when the method validates the expected output to be not null.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a80384a\" style=\"\">\n<li data-css=\"tve-u-1798a8077c1\" style=\"\">package tests;\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.Assert;<\/p>\n<p>public class Asserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 boolean verifyTitle = driver.getTitle().equalsIgnoreCase(&#8220;Practice Page&#8221;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Assert.<em>assertNotNull<\/em>(verifyTitle);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<\/p>\n<p>\u00a0\u00a0\u00a0 }<br \/>}<\/li>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1621525867810\"><strong>Verify in Selenium (also known as Soft Assertion)<\/strong><\/h3>\n<p>In a hard assertion, when the assertion fails, it terminates or aborts the test. If the tester does not want to terminate the script they cannot use hard assertions. To overcome this, one can use soft assertions.<\/p>\n<p>Let us now go through different types of soft assertion with example.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-1798a8122f2\" style=\"\">\n<p data-css=\"tve-u-1798a815ec1\" style=\"\">package tests;<\/p>\n<p>import org.openqa.selenium.WebDriver;<br \/>import org.openqa.selenium.chrome.ChromeDriver;<br \/>import org.testng.asserts.SoftAssert;<\/p>\n<p>public class SoftAsserts {<br \/>\u00a0\u00a0\u00a0 public static void main(String[] args) {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<em>setProperty<\/em>(&#8220;webdriver.chrome.driver&#8221;, &#8220;C:\\chromedriver.exe&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebDriver driver = new ChromeDriver();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to maximize chrome browser<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.manage().window().maximize();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String baseURL = &#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.get(baseURL);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SoftAssert softAssert = new SoftAssert();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String getActualTitle = driver.getTitle();<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 boolean verifyTitle = driver.getTitle().equalsIgnoreCase(&#8220;Practice Page&#8221;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 softAssert.assertEquals(getActualTitle, &#8220;Practice Page&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 softAssert.assertNotEquals(getActualTitle, &#8220;Practice Page&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 softAssert.assertNull(verifyTitle);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 softAssert.assertNotNull(verifyTitle);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 softAssert.assertAll();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/code to close chrome driver<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 driver.close();<br \/>\u00a0\u00a0\u00a0 }<br \/>}<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1621525867811\"><strong>Hard vs Soft Asserts in Selenium<\/strong><\/h3>\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"355\">\n<p><strong>Hard Assertions<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p><strong>Soft Assertions<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"355\">\n<p>Test Execution will be aborted if assert condition is not met<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Test execution will continue till the end of the test case even if assert condition is not met<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"355\">\n<p>Does not have to invoke any methods to capture the assertions<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>To view assertions result at the end of the test, tester has to invoke assertAll()<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\" id=\"t-1621525867812\"><strong>Difference between Assert and Verify in selenium<\/strong><\/h3>\n<p>In the case of assertions, if the assert condition is not met, test case execution will be aborted. The remaining tests are skipped, and the test case is marked as failed. These assertions are used as checkpoints for testing or validating business-critical transactions.<\/p>\n<p>In case of verify, tests will continue to run until the last test is executed even if assert conditions are not met. Verify or Soft Asserts will report the errors at the end of the test. Simply put, tests will not be aborted if any condition is not met. Testers need to invoke the <strong>assertAll()<\/strong> method to view the results.<\/p>\n<p>Both Hard and Soft Assertions are very important for designing and running Selenium WebDriver tests. They are instrumental in verifying application behavior at critical stages. By using assertions, testing teams can determine if an application is working as it is expected to.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Asserts and Verify methods are commonly used in Selenium for verifying or validating applications. In this tutorial, we will learn the why, when, and how of using these methods to make Selenium tests more efficient. table of contents Assertions Types of Assertions Hard Assertions Verify in Selenium (also known as Soft Assertion) Hard vs Soft [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-selenium","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}