{"id":189,"date":"2015-05-03T18:55:00","date_gmt":"2015-05-03T13:25:00","guid":{"rendered":"http:\/\/scrolltest.com\/?p=189"},"modified":"2019-01-11T16:51:09","modified_gmt":"2019-01-11T11:21:09","slug":"selenium-test-cases-in-java-with-testng","status":"publish","type":"post","link":"https:\/\/scrolltest.com\/selenium-test-cases-in-java-with-testng\/","title":{"rendered":"Selenium Test Cases in Java with TestNG Tutorial"},"content":{"rendered":"<p>In this TestNG tutorial, We are going to cover How you can Start the TestNG in Selenium Java.<\/p>\n<p><a href=\"http:\/\/testng.org\" target=\"_blank\" rel=\"noopener\">TestNG <\/a>is next generation testing framework like <a href=\"http:\/\/junit.org\/\" target=\"_blank\" rel=\"noopener\">JUnit<\/a> &amp; NUnit but with extra features(Read keywords, Generate Test Reports, Generate logs) and easy to use. TestNG framework can cover different type of the tests like Integration, Unit, end-to-end, etc.<\/p>\n<p><span style=\"font-size: x-large;\"><strong>\u00a0 Installing TestNG in Eclispe:<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Open Eclipse , Select<i> Help \/ Software updates \/ Find and Install.<\/i><\/span><\/li>\n<li><i><span style=\"color: #000000;\">Search for new features to install.<\/span><\/i><\/li>\n<li><i><span style=\"color: #000000;\">New remote site.<\/span><\/i><\/li>\n<li><span style=\"color: #000000;\">For Eclipse 3.4 and above, enter <\/span><span style=\"color: #000000;\">http:\/\/beust.com\/eclipse<\/span><span style=\"color: #000000;\">.<br \/>\n<\/span><\/li>\n<li><span style=\"color: #000000;\">For Eclipse 3.3 and below, enter <\/span><a href=\"http:\/\/beust.com\/eclipse1\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #000000;\">http:\/\/beust.com\/eclipse1<\/span><\/a><span style=\"color: #000000;\">.<br \/>\n<\/span><\/li>\n<li><span style=\"color: #000000;\">Make sure the checkbox next to URL is checked and click <i>Next<\/i>.<br \/>\n<\/span><\/li>\n<li><span style=\"color: #000000;\">Eclipse will then guide you through the process<\/span>.<\/li>\n<\/ul>\n<p>We are going to Test a Demo website \u201c<a title=\"http:\/\/demoaut.com\/mercuryregister.php\" href=\"http:\/\/demoaut.com\/mercuryregister.php\" target=\"_blank\" rel=\"noopener\">http:\/\/demoaut.com\/mercuryregister.php<\/a>\u201d and will write a Test case<br \/>\nto Do a Registration in it.<\/p>\n<p><span style=\"color: #3a4145; font-size: x-large;\"><strong>Project Structure:<br \/>\n<\/strong><strong><br \/>\n<\/strong><\/span><\/p>\n<p><strong>Step1<\/strong>:<br \/>\nCreate a project in the eclipse. Add the <a href=\"http:\/\/selenium-release.storage.googleapis.com\/2.45\/selenium-server-standalone-2.45.0.jar\" target=\"_blank\" rel=\"noopener\">Selenium StandAlone jar<\/a> and <a href=\"http:\/\/search.maven.org\/remotecontent?filepath=org\/testng\/testng\/6.8.21\/testng-6.8.21.jar\" target=\"_blank\" rel=\"noopener\">TestNG jar<\/a> into a new libs\u00a0 folder.<br \/>\nMake sure you add them to Build Path by Right Click \u2013 Build Path \u2013&gt; <a href=\"http:\/\/www.wikihow.com\/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29\" target=\"_blank\" rel=\"noopener\">Add to Build Path.<\/a><\/p>\n<p><strong>Step2<\/strong>:<br \/>\nCreate a Base Class with Test Fixture methods e.g. Setup and Tear Down. <strong>Note: I have added function name<\/strong><br \/>\n<strong> as JUnit you can name them as you want)<\/strong>, Add annotations as Before Method and After Method.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:solarized-dark lang:java decode:true \">package com.scrolltest.testng;\r\n\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.firefox.FirefoxDriver;\r\nimport org.testng.annotations.AfterMethod;\r\nimport org.testng.annotations.BeforeMethod;\r\n\r\npublic class BaseClass {\r\n\r\nWebDriver driver;\r\n\r\n@BeforeMethod\r\npublic void setUp() {\r\ndriver = new FirefoxDriver();\r\n\r\n}\r\n\r\n@AfterMethod\r\npublic void tearDown() {\r\n\/\/ driver.close();\r\ndriver.quit();\r\n}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step3<\/strong> :<\/p>\n<p>Create a Separate Class with Name Registration .java to write a Test case for Registration of the<br \/>\nDemo:\u00a0Website <a title=\"http:\/\/demoaut.com\/mercuryregister.php\" href=\"http:\/\/demoaut.com\/mercuryregister.php\" target=\"_blank\" rel=\"noopener\">http:\/\/demoaut.com\/mercuryregister.php<\/a><\/p>\n<pre class=\"theme:solarized-dark lang:java decode:true \">package com.scrolltest.testng;\r\n\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.WebElement;\r\nimport org.openqa.selenium.support.ui.Select;\r\nimport org.testng.Reporter;\r\n\r\npublic class RegisterationPageObect {\r\n\r\nprivate WebDriver driver;\r\n\r\npublic RegisterationPageObect(WebDriver driver) {\r\nthis.driver = driver;\r\n}\r\n\r\npublic void RegisterUser() {\r\ndriver.findElement(By.name(\"firstName\")).sendKeys(\"Pramod\");\r\ndriver.findElement(By.name(\"lastName\")).sendKeys(\"Dutta\");\r\ndriver.findElement(By.name(\"phone\")).sendKeys(\"981457452\");\r\nWebElement t = driver.findElement(By.id(\"userName\"));\r\n\r\ndriver.findElement(By.id(\"userName\")).sendKeys(\"a@c.com\");\r\nReporter.log(\"\"+t.getAttribute(\"maxlength\"));\r\n\/\/ #Drop Down\r\nnew Select(driver.findElement(By.name(\"country\")))\r\n.selectByVisibleText(\"ANGUILLA\");\r\n\/\/ register\r\ndriver.findElement(By.name(\"register\")).click();\r\n}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Step4: Now let&#8217;s create a Final class and <a href=\"https:\/\/github.com\/PramodDutta\/Selenium-Testcases-in-Java-with-TestNG\/blob\/master\/src\/testng.xml\" target=\"_blank\" rel=\"noopener\">TestNG xml file<\/a>.<\/p>\n<p><strong>MainClass.java<\/strong><\/p>\n<pre class=\"theme:solarized-dark lang:java decode:true \">package com.scrolltest.testng;\r\n\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.support.ui.Select;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class MainClass extends BaseClass {\r\n\r\n@Test\r\npublic void testRegisterationInDemoAUT() throws InterruptedException {\r\n\r\nRegistrationPageObect pageOject = new RegistrationPageObect(driver);\r\ndriver.get(\"http:\/\/demoaut.com\/mercuryregister.php\");\r\npageOject.RegisterUser();\r\n\r\nassert driver\r\n.findElement(By.tagName(\"body\"))\r\n.getText()\r\n.contains(\r\n\"Thank you for registering. You may now sign-in using the user name and password you've just entered.\");\r\n\r\nassert driver.findElement(By.tagName(\"body\")).getText()\r\n.contains(\"Dear Pramod Dutta,\");\r\n\r\n}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Step5:<br \/>\nNow Run the Test by Creating new TestNG configuration by Clicking on the Project-&gt;Run Configuration, Select<br \/>\nthe TestSuite and browse the XML file(<a href=\"https:\/\/github.com\/PramodDutta\/Selenium-Testcases-in-Java-with-TestNG\/blob\/master\/src\/testng.xml\" target=\"_blank\" rel=\"noopener\">testng.xml<\/a>). and Now Run the Project.<\/p>\n<p>Step6:<br \/>\nAfter run successfully, If you refresh you project you will see the test-output folder , Open \u201cindex.html\u201d and see that You Test Suite Results.\u00a0Click on the Report output link you can see max length also.(Used as Logging)<\/p>\n<p align=\"center\"><strong><span style=\"font-size: x-large;\"><u><a href=\"https:\/\/github.com\/PramodDutta\/Selenium-Testcases-in-Java-with-TestNG\" target=\"_blank\" rel=\"noopener\">Source code Github<\/a><\/u><\/span><\/strong><\/p>\n<p align=\"center\">You may also want to checkout<\/p>\n<p align=\"center\"><a href=\"https:\/\/scrolltest.com\/2018\/12\/15\/api-testing-tutorial\/\">The Definitive Guide to Do API Testing in 2019<\/a><\/p>\n<p align=\"center\"><a href=\"https:\/\/scrolltest.com\/2018\/11\/30\/how-to-do-local-testing-and-what-is-it\/\">How to do Local testing and what is it?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this TestNG tutorial, We are going to cover How you can Start the TestNG in Selenium Java. TestNG is next generation testing framework like JUnit &amp; NUnit but with extra features(Read keywords, Generate Test Reports, Generate logs) and easy to use. TestNG framework can cover different type of the tests like Integration, Unit, end-to-end,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[4,6],"tags":[12,16,636,638,637],"class_list":["post-189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-selenium","tag-java","tag-selenium","tag-testng","tag-testng-tutorial","tag-testng-tutorials"],"taxonomy_info":{"category":[{"value":4,"label":"Java"},{"value":6,"label":"Selenium"}],"post_tag":[{"value":12,"label":"java"},{"value":16,"label":"selenium"},{"value":636,"label":"testng"},{"value":638,"label":"testng tutorial"},{"value":637,"label":"testng tutorials"}]},"featured_image_src_large":["https:\/\/scrolltest.com\/wp-content\/uploads\/2019\/01\/test-automation-1-1024x535.jpg",1024,535,true],"author_info":{"display_name":"Promode","author_link":"https:\/\/scrolltest.com\/author\/admin\/"},"comment_info":0,"category_info":[{"term_id":4,"name":"Java","slug":"java","term_group":0,"term_taxonomy_id":4,"taxonomy":"category","description":"","parent":0,"count":4,"filter":"raw","cat_ID":4,"category_count":4,"category_description":"","cat_name":"Java","category_nicename":"java","category_parent":0},{"term_id":6,"name":"Selenium","slug":"selenium","term_group":0,"term_taxonomy_id":6,"taxonomy":"category","description":"","parent":0,"count":38,"filter":"raw","cat_ID":6,"category_count":38,"category_description":"","cat_name":"Selenium","category_nicename":"selenium","category_parent":0}],"tag_info":[{"term_id":12,"name":"java","slug":"java","term_group":0,"term_taxonomy_id":12,"taxonomy":"post_tag","description":"","parent":0,"count":9,"filter":"raw"},{"term_id":16,"name":"selenium","slug":"selenium","term_group":0,"term_taxonomy_id":16,"taxonomy":"post_tag","description":"","parent":0,"count":23,"filter":"raw"},{"term_id":636,"name":"testng","slug":"testng","term_group":0,"term_taxonomy_id":636,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"},{"term_id":638,"name":"testng tutorial","slug":"testng-tutorial","term_group":0,"term_taxonomy_id":638,"taxonomy":"post_tag","description":"","parent":0,"count":2,"filter":"raw"},{"term_id":637,"name":"testng tutorials","slug":"testng-tutorials","term_group":0,"term_taxonomy_id":637,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":0,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/media\/1679"}],"wp:attachment":[{"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scrolltest.com\/wp-json\/wp\/v2\/tags?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}