{"id":3734,"date":"2020-06-15T19:44:38","date_gmt":"2020-06-15T14:14:38","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3734"},"modified":"2025-04-02T06:57:37","modified_gmt":"2025-04-02T10:57:37","slug":"testng-tutorial","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/testng-tutorial\/","title":{"rendered":"TestNG Tutorial:"},"content":{"rendered":"\n<p><strong>TestNG Tutorial<\/strong>: TestNG is an automation testing framework inspired by JUnit and NUnit, adding new functionalities that make TestNG annotations more powerful and easier to use. An open-source framework, where NG stands for Next Generation, TestNG is similar to JUnit but designed with better features, especially for integrated class testing, making it more powerful. It provides the developer to write more powerful and flexible tests with the help of easy annotations, sequencing, grouping, and parametrizing.<\/p>\n\n\n\n<p>Using TestNG, you can easily generate proper reports, to easily come to know how many test cases are passed, test cases failed and skipped. You can separately execute the failed test cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Require TestNG In Selenium?<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing<\/a> do not generate test results in a proper format. Using TestNG we can generate the test results more properly.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TestNG will generate the reports in a proper format which includes the number of test cases run, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.<\/li>\n\n\n\n<li>The same case can be executed multiple numbers of times without using the loop just by using the <strong>invocation count<\/strong> keyword.<\/li>\n\n\n\n<li>We can group the multiple test cases more easily by grouping them into the testng.xml file. So you can prioritize which test case should be executed first.<\/li>\n\n\n\n<li>We can execute multiple test cases on multiple browsers. i.e, cross-browser testing.<\/li>\n\n\n\n<li>The <a href=\"https:\/\/www.h2kinfosys.com\/blog\/testng-test-priority-in-selenium\/\">TestNG framework<\/a> can be integrated easily with tools like Jenkins, Maven, etc.<\/li>\n\n\n\n<li>Annotations that are used in the testing are very easy to understand ex: @BeforeTest, @AfterTest, @BeforeMethod, @AfterMethod, @BeforeSuite, @AfterSuite.<\/li>\n\n\n\n<li>&nbsp;TestNg will generate the reports in a readable format.<\/li>\n\n\n\n<li>TestNG can simplify the testing code? There is no need for the main method in our tests.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>The Usual structure will look like below and somewhat difficult to read<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class myclass {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static String baseUrl = \u201c<a href=\"https:\/\/www.facebook.com\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.facebook.com\/<\/a>\u201c;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static WebDriver driver = new ChromeDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\ndriver.get(baseUrl);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verifyHomepageTitle();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.quit();\n}\npublic static void verifyHomepageTitle() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String expectedTitle = \u201cFacebook - Log In or Sign Up\u201d;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String actualTitle = driver.getTitle();\ntry {&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.assertEquals(actualTitle, expectedTitle);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\u201cTest Passed\u201d);\n&nbsp;&nbsp;&nbsp;&nbsp;} catch (Throwable e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\u201cTest Failed\u201d);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}\nThe TestNG structure will look like below and easier to understand.\npublic class myclass {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static String baseUrl = \u201chttps:\/\/www.facebook.com\/ \u201c;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public WebDriver driver;\n&nbsp;\n@BeforeTest\npublic void setBaseURL() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver = new ChromeDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(baseURL);\n}\npublic void verifyHomepageTitle() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String expectedTitle = \u201cFacebook - Log In or Sign Up\u201d;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String actualTitle = driver.getTitle();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.assertEquals(actualTitle, expectedTitle);\n}\n@AfterTest\npublic void endsession() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.quit();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Uncaught exceptions are handled automatically by TestNG without terminating the test. These exceptions will as failed in the report.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TestNG advantages over JUnit<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>TestNG Annotations are very easy to understand over JUnit<\/li>\n\n\n\n<li>Method name constraint is not present in TestNG like JUnit, you can specify any method names in TestNG.<\/li>\n\n\n\n<li>There is no constraint in TestNG like Junit, you have to declare @BeforeClass and @AfterClass which is present in JUnit.<\/li>\n\n\n\n<li>Grouping of Test cases in easy in TestNG which is not possible in JUnit.<\/li>\n\n\n\n<li>TestNG allows us to define the dependent test cases and each test case is independent of other test cases.<\/li>\n\n\n\n<li>Parallel execution is possible in TestNG.<\/li>\n\n\n\n<li>TestNG allows us to group test cases more easily.&nbsp;<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>First Test Case using annotations<\/strong><\/h3>\n\n\n\n<p>Before creating a test case, first, we need to create a new project in Eclipse.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Setting up of new TestNG Project<\/strong><\/h3>\n\n\n\n<p><strong>Step 1:<\/strong> Go to File -&gt; Click New -&gt; Click Java Project<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"525\" height=\"78\" alt=\"NewProject.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/iuGY5P6LLnQ0u52qXahFl7GPx829QiZNqI_kKQwSgaaYSwIELd7cq6VG-yBKlu-lX3wz72z88t_wMnFuntbxUTL2ENoZwRFbCDJwMygHgzHPqsZ-FWYNlv6bMaIgoJXvbcA_CC9Q4z2LX6UpaQ\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Enter Project Name as \u201cTestNGProject\u201d and click Next button<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/uIHR1ub7YgiwmEZlPHQtaC1ElosVTnyOYYoTtDNuanArcUIBDjsvsRu2w9azDIooa7tKc7yzMnhO1CZfAGITpZ1OwJu13cH2gHMIAAo0F4PYJMywDQmG4AEVTk6TcJhbqW-WRMDIJt8lx2UzaA\" alt=\"Projectname.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3: <\/strong>Click on <strong>Libraries<\/strong> tab to add the TestNG Libraries by clicking Add Library<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/guFPpjwk43b4hfMqOIydPhQTOKQO8cGnzNM3I1N6yWSm_fchU5gMBpK4w4HXeCWsrgpu2pAaVuDnRGlhRG3DK8LERX-jZTz-4VvQUc0pA_-A0KEfoItEgCuzU9lIRaTBER1IXlSTTYljpjr-0g\" alt=\"Libraries.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4: <\/strong>Choose \u201cTestNG\u201d on Add Library dialog box and click on Next.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/v9UnjkZ1yGF8W3J_vHWrl2o0IElwNCfoRBWa_k7nOgzZEGuiq8voNXZ1fDfMrfjWrjoTLsI1C6PFKv0ZFRgIu2ayvMlELRNwUxmScgps4StQ2OTnuAeoMyiA_EXjm_O82IR-gqTf9eJCiyt85g\" alt=\"Addlibrary.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 5: <\/strong>Click \u201cFinish\u201d button<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/tAJH5wv1DHuAnkv2sbNlTxtjO_ipH-sAREzzizAdofzgpwb-ySFQSV8alW_R4ysW3U2UDQ0O7FLXAtV7phZK3qm4z9xSItUS0TAmlMMLCixP4fOy1lG2tR2WNWI-OxulNrjKuqfc4H0MzgBXWw\" alt=\"Finish.PNG\" title=\"\"><\/figure>\n\n\n\n<p>You will notice in Libraries list that TestNG is included<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/4JCcafB9uZzpeuyra2r4jNyzdX-pOFcqNUQV3u8Ntwpx4-RcFWHOvFFXPoKs5IIeYlPe1ie8iz8oywA0vEqCutWCt04kT8UgqVrevV2ODxbRqy9N1herKKIJdcTMy6DiTB7EoEaPoNAioMKbvw\" alt=\"TestNG4.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 6: <\/strong>Now we will add JAR files that contain Selenium API. These files are found in Java client driver downloaded from <a href=\"https:\/\/www.selenium.dev\/downloads\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.selenium.dev\/downloads\/<\/a>. To add JAR files click on Add External JARs.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/yvRWmC5AsecO5njzoV_Mt2UmRRRw92DQUKjzkdZXC4OSd_yUQd5EXaeTD-9htb2dsu5iFyFx9hfWnh0b7LI0ypkTO3F6RI1lyylf6NN1a89ac39zb84UuQVEyKATlsFqFFJMq0S46m-6NFEIxg\" alt=\"Externaljars.PNG\" title=\"\"><\/figure>\n\n\n\n<p>This will navigate to<a href=\"https:\/\/www.h2kinfosys.com\/blog\/selenium-jars-download-configure-eclipse\/\"> Selenium JAR<\/a> files and add both jars files under the libs tab and click open.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/sP_sidVt9y8-FIm4ntK8O_eTICGMDtebWXfaXmjIAmjO_PUm4v-Lx_Dug3IdB8wfeF4me7MoU2G6nB8rpcR3yYAA9CSIuGD9jVTPhKKH-KK2PZmcYo3_2FZuyBEsLwcGdfwxNmQT5SDlEUWfjg\" alt=\"bothjars.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Your screen looks like below after adding the external JARs<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/G2f0Z-pb9E4iD2Rnts1ouDqJ4H8a026IiVsnD4VPyZ74qHiOIPoPARfI6nf_FAHpf0zc6YBQAe-k9wuRcNEotPDmTttK1g9YdM5G6Dhn2Uf7JUwAoRtKpt2eitY2Nrqh68bV-lV0IyRA7l8TBw\" alt=\"Librarries.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 7: <\/strong>Click the Finish button and you can see the created project is visible on the Eclipse Package Explorer window.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/z82cP5hYcEg6o0qq3F-gStU6jNd1hXzK04l7AjxBCD3vh6qVYCD5uQXCQW4JZACgZC-xTX7sgo1IHh_AYsq0CRXO8b33QfQ_bfqRraduk3z-1-jBXG7adDY7zCY_-JZhkVLvVR1-QdXyKbSVhA\" alt=\"Project.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Creating a New TestNG Test File<\/strong><\/p>\n\n\n\n<p>Let\u2019s create a New TestNG File<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Right-click on src package &gt; Select New &gt; Click on Other<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/VZMNLJx3zeolkstWEYTkTQcy-NWPunxSMURvh248hcv5vZZ2guCrGpgotbfzzsGH2GGjfISMZxrBek4ShtndrxEMQ9kW0HhBUpAbPltMUIzC-kp2AfEWLagTTYcDG-p4BA-YJXR1il7HUkJklg\" alt=\"New.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 2:<\/strong> Select the TestNG class under the TestNG folder and click on Next.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/f3qdJeMFwnhpKtahdokqSD0bGqsQ8hkwVC4UnZs8So4WNi4AjtVzGOQw_g2YjgErrhTzGX10MAg2uJaFvVl2p7GxpLm61PhQuog0wf2NF5slz_go4KOdT8e5xRm3CInpBAXnpXmGt29m1bGxmQ\" alt=\"Tesngclass.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Enter the values indicated below and click on the Finish button. Notice that we named our Java file name as \u201cTestNGProject\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/1wkWUOD3IFsxKWuVSk-1rxElBD5sSOkvlwfd2_0t1p84as1r1XbRTBC1_nF9TNQAIf--ffb__YPcr7h-4p_XsLuqKT9YlT_KkBdPHzHH94SsdGzKb_yULeGySbTGopVnTJZIDM2igDfQTxIV7Q\" alt=\"file.PNG\" title=\"\"><\/figure>\n\n\n\n<p>The eclipse will automatically create the template for our TestNG file as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/NixWXnYM4uSUD6hNZ-QeaoRDwcqyFsHhZLnWpZc3CQDC9rbkoX9nzwjwRIN2JNHz_GqNoac5G3H12gGshJiPCC2moYWC49aNQ50I4NZiDETQs_V8ZDpezXGPY60wop3p9pnx7NHEThJ9laJA5g\" alt=\"package.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example of our First Test Case<\/strong><\/h2>\n\n\n\n<p>Let us create our first Test Case that will check if the Facebook homepage is correct.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testngpackage;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.testng.Assert;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> firsttestngfile {\n\n<strong>public<\/strong> String baseUrl = \" https:\/\/www.facebook.com\/\";\n&nbsp;&nbsp;&nbsp;&nbsp;String driverPath = \"C:\\\\geckodriver.exe\";\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> WebDriver driver ;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;@Test\n&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> verifyHomepageTitle() {\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"launching chrome browser\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<em>setProperty<\/em>(\"webdriver.gecko.driver\", driverPath);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver = <strong>new<\/strong> ChromeDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(baseUrl);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String expectedTitle = \" Facebook - Log In or Sign Up\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String actualTitle = driver.getTitle();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.<em>assertEquals<\/em>(actualTitle, expectedTitle);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();\n&nbsp;\n&nbsp;&nbsp;}\n}<\/code><\/pre>\n\n\n\n<p>From the above code, we notice that<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TestNG does not require any main() method.<\/li>\n\n\n\n<li>Methods need not be static<\/li>\n\n\n\n<li>We used the @Test annotation.&nbsp;<strong>@Test annotation will tell that the method under it is a test case<\/strong>. In this case, we have set the verifyHomepageTitle() method as our test case, so we placed an &#8216;@Test&#8217; annotation above it.<\/li>\n\n\n\n<li>We need to import the package org.testng.annotations.*. Since we use annotations in TestNG.<\/li>\n\n\n\n<li>We used the Assert class in the code.&nbsp;<strong>This Assert class will conduct verification operations in TestNG<\/strong>. To use it, we imported the org.testng.Assert package.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Running the Test<\/strong><\/h2>\n\n\n\n<p>To run the test, Right-click on the program &gt; Run As &gt; TestNG Test. The eclipse will give two outputs \u2013 one in the TestNG Results window and the other is in the Console window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Generating the HTML Reports<\/strong><\/h2>\n\n\n\n<p>Using TestNG we can generate the reports in HTML format.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Right-click on our created project name (TestNGProject) in the Project Explorer window and click on the \u2018Refresh\u2019 option.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/J4GwjkJUZvNbz4Hykn--H7oa0tJzpXZdAsOUmQmXBcgaRMYx9KBBqaclE_M59hxKjj-3C5_PQGezEe8_ltE4aMAr5bml-6r5QVymhel3zevc0pM5D4Ese49GJZ5IpqK9CqnYk3mnzhwgvC4Stw\" alt=\"Refresh.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>You will notice that a &#8220;test-output&#8221; folder was created. Expand the test-output folder and we can see an index.html file. This HTML file will provide the report of the results of the most recent test run.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/kR7S1NVVPSAXLexTHipP7lwLu5whN95n2gTMJt5-2ME5KmWbX8p1Pe98rm92DAoPtoVGGhDdy4rfuZuMjFOiCsKpqH9kCt1Xtx5xImQevir1Dx8jDvJa67w7s0djU8mOmnMz9FvuAkRwh4i4ZQ\" alt=\"html.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Double-click on index.html file to open it within Eclipse built-in browser.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/wiAz1VLIr2xjBkjTOG-Xz2_E8tQa1Hg_rHktk4SZdZhTT_rlpb0wxuQVEyQno8lIIwMTgPu8_bAFg8Kxzi7NjVMqPK0YcRmYKCu7FN5wgDtCT8WtOBwwnYVGROA-vazCHW1cMgdmTEh_JNdR8w\" alt=\"testresult.PNG\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Annotations used in TestNG<\/strong><\/h3>\n\n\n\n<p>Here we go through more advanced annotations and their usages.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Multiple Test Cases<\/strong><\/h4>\n\n\n\n<p>In a single TestNG file we can use multiple @Test annotations. By default, @Test methods are executed in alphabetical order. In the&nbsp; below code the methods b_test, c_test, a_test are not arranged in alphabetical order, they will execute as such.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public class TestNGFile {\n@Test\npublic void b_test() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.fail();\n}\n@Test\npublic void c_test() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.assertTrue(true);\n}\n@Test\npublic void a_test() {\nthrow new SkipException(\u201c Skipping a_test...\u201d);\n}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Parameters<\/strong><\/h4>\n\n\n\n<p>We use the parameter \u201cpriority\u201d if you want the methods to be executed in a different order. Parameters are the keywords that will modify the annotations function.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parameters will require you to assign a value to them. Just we need to place a &#8220;=&#8221; next to them followed by the value.<\/li>\n\n\n\n<li>Parameters are enclosed with parentheses which are placed after the annotation like the code shown below.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/URkmfOOWn06sgj21U0zZYNNecV_gwMvcQOcFIwedRBzSxxcIlvSm8xPLzetKGDZMGdvkoPjLfawsNpkBcE2_7mqOThPn2PW3TdiOmHETXjj8i-h7SmOlkCYXXY91lLM3unZr4pxhkv4eLaKrpQ\" alt=\"parameter.PNG\" title=\"\"><\/figure>\n\n\n\n<p>TestNG will execute the @Test annotation with low priority value to the high priority value. There is no need that your priority values to be consecutive.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Y2o26ELVBgKSG35qGf-Dq7DoCIxutyJ5zMqdD8gL_TbncTWMb38d2A5T0YQeJvGe19UVCmv8DwInEjOK8MITfcBx7RuhoWyzK-58UMoKG5HgCKtV41dh8o6_IEZx7zMVw7opheCnIlGrMXz6Wg\" alt=\"priority.PNG\" title=\"\"><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Multiple Parameters<\/strong><\/h4>\n\n\n\n<p>Other than the priority parameter, @Test has another parameter called \u201calwaysrun\u201d which can set either to true or false. We need to separate with a comma if you want to use two or more parameters in a single annotation which is shown below<\/p>\n\n\n\n<p>@Test(priority = 0, alwaysRun = true)<\/p>\n\n\n\n<p><strong>@BeforeTest and @AfterTest Methods<\/strong><\/p>\n\n\n\n<p><strong>@BeforeTest:<\/strong> This method will execute prior to the first test case in the TestNG file.<strong>@AfterTest:<\/strong> This method will execute after all the test cases in the TestNG file are executed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testngpackage;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n\n<strong>import<\/strong> org.openqa.selenium.firefox.FirefoxDriver;\n<strong>import<\/strong> org.testng.Assert;\n<strong>import<\/strong> org.testng.annotations.AfterTest;\n<strong>import<\/strong> org.testng.annotations.BeforeTest;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> firsttestngfile {\n\n<strong>public<\/strong> String baseUrl = \"http:\/\/facebook.com\/\";\n&nbsp;&nbsp;&nbsp;&nbsp;String driverPath = \"D:\\\\Drivers\\\\geckodriver.exe\";\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> WebDriver driver ;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@BeforeTest\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> launchBrowser() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"launching Firefox browser\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<em>setProperty<\/em>(\"webdriver.gecko.driver\", driverPath);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver = <strong>new<\/strong> FirefoxDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(baseUrl);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Test\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> verifyHomepageTitle() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String expectedTitle = \" Facebook - Log In or Sign Up\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String actualTitle = driver.getTitle();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Assert.<em>assertEquals<\/em>(actualTitle, expectedTitle);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@AfterTest\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> terminateBrowser(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/code><\/pre>\n\n\n\n<p>When you run the above code it will execute the methods in the sequence<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1<sup>st<\/sup>&nbsp;&#8211; launchBrowser()<\/li>\n\n\n\n<li>2<sup>nd<\/sup>&nbsp;&#8211; verifyHomepageTitle()<\/li>\n\n\n\n<li>3<sup>rd<\/sup>&nbsp;&#8211; terminateBrowser()<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TestNG Annotations<\/strong><\/h2>\n\n\n\n<p><strong>@BeforeSuite<\/strong>: This annotation method will run before all tests in this suite run.<\/p>\n\n\n\n<p><strong>@AfterSuite<\/strong>: This annotation method will be run after all tests in this suite run.<\/p>\n\n\n\n<p><strong>@BeforeTest<\/strong>: It runs before any test method inside the test class<\/p>\n\n\n\n<p><strong>@AfterTest<\/strong>: This annotation method will run after any test method inside the test class.<\/p>\n\n\n\n<p><strong>@BeforeGroups<\/strong>: The groups of the list that this configuration method will run before.&nbsp;<\/p>\n\n\n\n<p><strong>@AfterGroups<\/strong>: The groups of lists that this configuration method will run after.&nbsp;<\/p>\n\n\n\n<p><strong>@BeforeClass<\/strong>: This method runs only one time before the first test method in the current class is invoked<\/p>\n\n\n\n<p><strong>@AfterClass<\/strong>: This method runs only once after all the methods in the test in the current class are executed.<\/p>\n\n\n\n<p><strong>@BeforeMethod<\/strong>: It<strong> <\/strong>runs<strong>&nbsp;<\/strong>before each test method in a program<strong>&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>@AfterMethod<\/strong>: It<strong> <\/strong>runs<strong>&nbsp;<\/strong>after each test method in a program<\/p>\n\n\n\n<p><strong>@Test<\/strong>: This annotation method is a part of a test case<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TestNG is a testing framework that is used for making <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-running-selenium-tests-on-chrome\/\">Selenium tests<\/a> to understand easily and for generating reports&nbsp;<\/li>\n\n\n\n<li>The advantages of TestNG over JUnit are.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>TestNG Annotations are very easy to understand over JUnit<\/li>\n\n\n\n<li>Method name constraint is not present in TestNG like JUnit, you can specify any method names in TestNG.<\/li>\n\n\n\n<li>There is no constraint in TestNG like Junit, you have to declare @BeforeClass and @AfterClass which is present in JUnit.<\/li>\n\n\n\n<li>Grouping of Test cases in easy in TestNG which is not possible in JUnit.<\/li>\n\n\n\n<li>TestNG allows us to define the dependent test cases and each test case is independent of other test cases.<\/li>\n\n\n\n<li>Parallel execution is possible in TestNG.<\/li>\n\n\n\n<li>TestNG allows us to group test cases more easily.&nbsp;<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TestNG will generate HTML-based reports.<\/li>\n\n\n\n<li>Annotations can use the parameters just like the usual Java methods.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>TestNG Tutorial: TestNG is an automation testing framework inspired by JUnit and NUnit, adding new functionalities that make TestNG annotations more powerful and easier to use. An open-source framework, where NG stands for Next Generation, TestNG is similar to JUnit but designed with better features, especially for integrated class testing, making it more powerful. It [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3743,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[988,156,1867,1865,1866,989,473,448,987,986,985],"class_list":["post-3734","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-annotations-in-testng","tag-automation-testing","tag-jar-files","tag-junit","tag-junit-and-nunit","tag-parameters","tag-selenium-jar","tag-selenium-webdriver","tag-test-case","tag-testng-in-selenium","tag-testng-tutorial"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3734"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3734\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3743"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}