{"id":878,"date":"2014-08-13T14:44:53","date_gmt":"2014-08-13T18:44:53","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=878"},"modified":"2015-04-12T08:43:31","modified_gmt":"2015-04-12T12:43:31","slug":"get-started-with-testng","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/get-started-with-testng\/","title":{"rendered":"Get started with TestNG"},"content":{"rendered":"<p>We wrote this post for testers who want to get started with <a href=\"http:\/\/testng.org\/doc\/index.html\">TestNG<\/a>. You will learn about some popular annotations used in TestNG. We also show you how to parameterize your automated checks. <\/p>\n<p><a href=\"http:\/\/testng.org\/doc\/index.html\">TestNG<\/a> is a testing framework influenced from JUnit and NUnit but has some great features which makes it more powerful and easier to use.  Some basic knowledge of automated checking using Java and <a href=\"http:\/\/www.seleniumhq.org\/\">Selenium<\/a> is a pre-requisite to follow this post. You should have <a href=\"https:\/\/java.com\/en\/download\/index.jsp\">Java<\/a>, <a href=\"https:\/\/www.eclipse.org\/downloads\/\">Eclipse<\/a> and <a href=\"http:\/\/www.seleniumhq.org\/download\/\">Selenium <\/a>installed. We estimate this post will take you about 60 minutes to read and try out on your own.<\/p>\n<hr>\n<h3>Getting started with TestNG<\/h3>\n<p>1. Install TestNG plug-in for Eclipse<br \/>\n2. Some important TestNG annotations<br \/>\n3. Select an application to test<br \/>\n4. Writing test using TestNG framework<br \/>\n5. Running the test<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 1: Install TestNG plug-in for Eclipse<\/span><br \/>\nIf your Eclipse does not have an inbuilt TestNG plug-in you can install it from eclipse IDE. Select <em>Help \/ Install New Software updates<\/em>. Search for TestNg or directly enter the url mentioned below.<br \/>\nFor Eclipse 3.4 and above, enter <a href=\"http:\/\/beust.com\/eclipse\">http:\/\/beust.com\/eclipse<\/a>.<br \/>\nFor Eclipse 3.3 and below, enter <a href=\"http:\/\/beust.com\/eclipse1\">http:\/\/beust.com\/eclipse1<\/a>.<br \/>\nEclipse will then guide you through the rest of the process.<\/p>\n<p>Note:\u00a0If you are wondering about the URL beust, <a href=\"http:\/\/beust.com\/weblog\/\">C\u00e9dric Beust<\/a> is the creator of the TestNG framework.<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Install-TestNG.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-885\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Install-TestNG-300x233.jpg\" alt=\"Install TestNG\" width=\"300\" height=\"233\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Install-TestNG-300x233.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Install-TestNG.jpg 880w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Once you have installed TestNg, add it to your Project by going to your project Properties\/Add Library and select TestNG Library which you added earlier<\/p>\n<p><a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Add-TestNG-Library.jpg\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-886\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Add-TestNG-Library-300x213.jpg\" alt=\"Add TestNG Library\" width=\"300\" height=\"213\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Add-TestNG-Library-300x213.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Add-TestNG-Library.jpg 1009w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 2: Some important TestNG Annotations<\/span><br \/>\nThere are many <a href=\"http:\/\/testng.org\/doc\/documentation-main.html#annotations\">TestNG annotations<\/a>. Below are a list of annotations which we will be using in our test<\/p>\n<ul>\n<li><strong>@BeforeTest<\/strong>: The annotated method will be run before any test method belonging to the classes inside the tag is run.<\/li>\n<li><strong>@AfterTest<\/strong>: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.<\/li>\n<li><strong>@BeforeMethod<\/strong>: The annotated method will be run before each test method.<\/li>\n<li><strong>@AfterMethod<\/strong>: The annotated method will be run after each test method.<\/li>\n<li><strong>@DataProvider<\/strong>: Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.<\/li>\n<\/ul>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 3: Select an application to test<\/span><br \/>\nSince we love Chess here at <a href=\"http:\/\/qxf2.com\/\">Qxf2 Services<\/a>, we will use <a href=\"http:\/\/www.chess.com\/\">http:\/\/www.chess.com\/<\/a> to automate our test using TestNG framework. We will parameterize the login functionality and assert that the username is displayed properly once we Login.<\/p>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 4: Writing test using TestNG framework<\/span><br \/>\nWe will write a Selenium test using TestNG framework to check the login functionality of chess.com using multiple id.<\/p>\n<pre lang=\"java\">\/***\r\nTest case to Parameterize the Login functionality of chess.com using TestNG annotation \"@DataProvider\".\r\nSome other annotations like @BeforeTest to setup driver, @AfterTest to teardown the driver and @BeforeMethod to navigate to the test url are used.\r\n**\/\r\n\r\nimport java.io.IOException;\r\nimport java.util.concurrent.TimeUnit;\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.firefox.FirefoxDriver;\r\nimport org.testng.Assert;\r\nimport org.testng.annotations.AfterTest;\r\nimport org.testng.annotations.BeforeMethod;\r\nimport org.testng.annotations.BeforeTest;\r\nimport org.testng.annotations.DataProvider;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class Chess {\r\n\tpublic static WebDriver driver;\r\n\t\t\r\n\t@Test(dataProvider=\"LoginData\")\r\n\t\r\n\tpublic static void Test_Login(String Username, String Password) throws InterruptedException{\r\n\t\t\r\n\t\tdriver.findElement(By.xpath(\"\/\/input[@id='loginusername']\")).sendKeys(Username);\r\n\t\tdriver.findElement(By.xpath(\"\/\/input[@id='loginpassword']\")).sendKeys(Password);\r\n\t\tdriver.findElement(By.xpath(\"\/\/button[@id='c2']\")).click();\r\n\t\tThread.sleep(5000);\r\n\t\tString userId= driver.findElement(By.xpath(\"\/\/div[@class='user-nick']\/h2\")).getText();\r\n\t\tAssert.assertEquals(userId, Username);\r\n\t\tdriver.findElement(By.xpath(\"\/\/li[@class='logout']\/a\")).click();\r\n\t\t\t\t\t\r\n\t}\r\n\t\r\n\t@DataProvider \/\/Supplies data for the test method\r\n\tpublic Object[][] LoginData() throws IOException{\r\n\t\tObject[][] data = new Object[3][2];\r\n\t\t\r\n\t\t\/\/First row data\r\n\t\tdata[0][0]=\"TestUser10\";\r\n\t\tdata[0][1]=\"chess.com\";\r\n\t\t\t\t\r\n\t\t\/\/Second row data\r\n\t\tdata[1][0]=\"TestUser12\";\r\n\t\tdata[1][1]=\"chess.com\";\r\n\t\t\r\n\t\t\/\/Third row data\r\n\t\tdata[2][0]=\"TestUser14\";\r\n\t\tdata[2][1]=\"chess.com\";\r\n\t\t\r\n        return data;\r\n\t\t\r\n\t}\r\n\t\r\n\t@BeforeMethod \/\/Method will run before executing every test\r\n\t  public void launchChess() {\r\n\t\t\/\/ Launch the chess.com url\r\n\t\tdriver.get(\"http:\/\/www.chess.com\/\");\r\n\t\r\n\t}\r\n\t\r\n\t @BeforeTest \/\/Method will run before any test method belonging to the class\r\n\t  public void setUp() {\r\n\t\t  \/\/Initialize driver\r\n\t\t driver= new FirefoxDriver();\r\n\t\t \/\/Implicit wait for 10 sec for element to load\r\n\t\t driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); \r\n\t\t\r\n\t  }\r\n\r\n\t  @AfterTest \/\/Method will run after executing all test method belonging to the class\r\n\t  public void tearDown() {\r\n\t\t  \/\/Close all the driver Instance\r\n\t\t  driver.quit();\r\n\t\t  \t\t  \r\n\t  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"font-weight: bold; font-style: inherit;\">STEP 5: Running the test<\/span><br \/>\nYou can run the TestNG test by clicking RunAs and selecting TestNG Test. Once the Tests runs you can view the result as shown below<br \/>\n<a href=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Run-TestNG.jpg\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-904\" src=\"http:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Run-TestNG-300x151.jpg\" alt=\"Run Test\" width=\"300\" height=\"151\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Run-TestNG-300x151.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Run-TestNG-1024x517.jpg 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2014\/07\/Run-TestNG.jpg 1353w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nIn case you have multiple tests to be run, you can use testng.xml to run the Test. Below is a sample testng.xml which has one test name. You can create multiple tests and add it to testng.xml to run it. The &#8220;class name&#8221; in the testng.xml should be your &#8220;PackageName.ClassName&#8221;.<\/p>\n<pre lang=\"xml\">\r\n<!DOCTYPE suite SYSTEM \"http:\/\/testng.org\/testng-1.0.dtd\">\r\n<suite name=\"My Sample Suite\">\r\n<test name=\"ChessTest\">\t\r\n <classes>\r\n <class name=\"Test.Chess\" ><\/class>\r\n <\/classes>\r\n<\/test>\r\n<\/suite>\r\n<\/pre>\n<hr>\n<p>There you have it. A crash course on getting started with TestNG. Happy automated checking!    <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We wrote this post for testers who want to get started with TestNG. You will learn about some popular annotations used in TestNG. We also show you how to parameterize your automated checks. TestNG is a testing framework influenced from JUnit and NUnit but has some great features which makes it more powerful and easier to use. Some basic knowledge [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,53,30,60],"tags":[],"class_list":["post-878","post","type-post","status-publish","format-standard","hentry","category-automation","category-java","category-selenium","category-testng"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":34,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":1151,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/878\/revisions\/1151"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}