{"id":3766,"date":"2020-06-16T22:22:48","date_gmt":"2020-06-16T16:52:48","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3766"},"modified":"2024-10-04T18:07:15","modified_gmt":"2024-10-04T12:37:15","slug":"how-to-run-multiple-test-suites-in-selenium-testng","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/how-to-run-multiple-test-suites-in-selenium-testng\/","title":{"rendered":"How to Run Multiple Test Suites in Selenium-TestNG"},"content":{"rendered":"\n<p>TestNG enables you to execute test methods, test classes, and test cases in parallel, enhancing the efficiency of your testing process. It allows us to run multiple tests at the same time across multiple environments instead of running tests one by one or in sequential order. Additionally, <strong><a href=\"https:\/\/www.h2kinfosys.com\/blog\/testng-tutorial\/\">TestNG<\/a><\/strong> supports multiple test suites, which means you can group various tests into different suites and execute them simultaneously. This feature is particularly beneficial for large projects, as it helps reduce overall execution time and improves resource utilization. <\/p>\n\n\n\n<p>With TestNG, you can easily manage and organize your testing efforts, ensuring that all aspects of your application are thoroughly tested on time. Hence, it is called parallel test execution in Selenium. <a href=\"https:\/\/www.browserstack.com\/guide\/parallel-testing-with-selenium\" rel=\"nofollow noopener\" target=\"_blank\">Parallel testing<\/a> helps us to run classes, test methods, and tests in parallel. We can reduce the execution time as tests will get executed simultaneously by using parallel test execution. Furthermore, TestNG allows you to configure the degree of parallelism through its XML configuration file, where you can specify whether to run tests at the method, class, or suite level. <\/p>\n\n\n\n<p>This flexibility ensures optimal resource utilization and faster feedback on test results. By leveraging these capabilities, teams can significantly enhance their testing efficiency and ensure robust application performance across various scenarios. Additionally, implementing best practices such as ensuring thread safety and managing resource allocation will further improve the reliability and speed of your testing process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 1: Creating a TestNG.xml file for executing test<\/strong><\/h2>\n\n\n\n<p><strong>Follow the below steps to handle above scenario<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a new project in eclipse<\/li>\n\n\n\n<li>Create two packages name com.sampletestpackage and com.sampletestpackage<\/li>\n\n\n\n<li>Create a class in each package (name them as Flipkart.java and SnapDeal.java) and copy the below code in respective classes<\/li>\n\n\n\n<li>Create a new file in your project and name it as testing.xml.<\/li>\n<\/ol>\n\n\n\n<p><strong><em>\u00a0Amazon.java<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> com.sampletestpackage;\n\n<strong>import<\/strong> java.util.concurrent.TimeUnit;\n\n<strong>import<\/strong> org.openqa.selenium.Alert;\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.openqa.selenium.interactions.Actions;\n<strong>import<\/strong> org.testng.annotations.AfterClass;\n<strong>import<\/strong> org.testng.annotations.BeforeClass;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> Flipkart {\nWebDriver driver = <strong>new<\/strong> ChromeDriver();\nString username = \"\"; \/\/ Change to your username and passwrod\nString password = \"\";\n\n\/\/ This method is to navigate flipkart URL\n@BeforeClass\n<strong>public<\/strong> <strong>void<\/strong> init() {\ndriver.manage().window().maximize();\ndriver.manage().timeouts().implicitlyWait(60, TimeUnit.<strong><em>SECONDS<\/em><\/strong>);\ndriver.navigate().to(\"https:\/\/www.flipkart.com\");\n}\n\n\/\/ To log in flipkart\n@Test\n<strong>public<\/strong> <strong>void<\/strong> login() {\ndriver.findElement(By.<em>partialLinkText<\/em>(\"Login\")).click();\ndriver.findElement(\nBy.<em>cssSelector<\/em>(\"input&#91;placeholder='Enter email\/mobile']\"))\n.sendKeys(username);\ndriver.findElement(\nBy.<em>cssSelector<\/em>(\"input&#91;placeholder='Enter password']\"))\n.sendKeys(password);\ndriver.findElement(By.<em>cssSelector<\/em>(\"input&#91;value='Login']&#91;class='submit-btn login-btn btn']\")).click();\n}\n\n\/\/ Search For product\n@Test\n<strong>public<\/strong> <strong>void<\/strong> searchAndSelectProduct() {\n\/\/driver.findElement(By.id(\"fk-top-search-box\")).sendKeys(\"moto g3\");\ndriver.findElement(By.<em>name<\/em>(\"q\")).sendKeys(\"moto g3\");\ndriver.findElement(\nBy.<em>cssSelector<\/em>(\"search-bar-submit.fk-font-13.fk-font-bold\"))\n.click();\n\n\/\/ select the first item in the search results\nString css = \".gd-row.browse-grid-row:nth-of-type(1) > div:nth-child(1)>div>div:nth-child(2)>div>a\";\ndriver.findElement(By.<em>cssSelector<\/em>(css)).click();\n}\n\n@Test\n<strong>public<\/strong> <strong>void<\/strong> addAndRemoveFromCart() {\ndriver.findElement(\nBy.<em>cssSelector<\/em>(\".btn-express-checkout.btn-big.current\"))\n.click();\ndriver.findElement(By.<em>cssSelector<\/em>(\".remove.fk-inline-block\")).click();\nAlert a = driver.switchTo().alert();\na.accept();\n}\n\n@Test\n<strong>public<\/strong> <strong>void<\/strong> logout() {\nActions act = <strong>new<\/strong> Actions(driver);\nWebElement user = driver.findElement(By.<em>partialLinkText<\/em>(username));\nact.moveToElement(user).build().perform();\ndriver.findElement(By.<em>linkText<\/em>(\"Logout\")).click();\n}\n\n@AfterClass\n<strong>public<\/strong> <strong>void<\/strong> quit() {\ndriver.close();\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong><em>SnapDeal.Java<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> com.sampletestpackage;\n\n<strong>import<\/strong> java.util.concurrent.TimeUnit;\n\n<strong>import<\/strong> org.openqa.selenium.Alert;\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.openqa.selenium.interactions.Actions;\n<strong>import<\/strong> org.testng.annotations.AfterClass;\n<strong>import<\/strong> org.testng.annotations.BeforeClass;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> SnapDeal {\nWebDriver driver = <strong>new<\/strong> ChromeDriver();\nString username = \"\"; \/\/ Change to your username and passwrod\nString password = \"\";\nString pinCode = \"\";\n\n\/\/ This method is to navigate snapdeal URL\n@BeforeClass\n<strong>public<\/strong> <strong>void<\/strong> init() {\ndriver.manage().window().maximize();\ndriver.manage().timeouts().implicitlyWait(60, TimeUnit.<strong><em>SECONDS<\/em><\/strong>);\ndriver.navigate().to(\"https:\/\/www.snapdeal.com\");\n}\n\n\/\/ To log in flipkart\n@Test\n<strong>public<\/strong> <strong>void<\/strong> login() {\ndriver.findElement(By.<em>xpath<\/em>(\"\/\/button&#91;text()='Login']\")).click();\n\ndriver.switchTo().frame(\"loginIframe\");\n\ndriver.findElement(By.<em>cssSelector<\/em>(\"div&#91;onClick='getLoginForm()']\"))\n.click();\n\ndriver.findElement(By.<em>id<\/em>(\"userName\")).sendKeys(username);\ndriver.findElement(By.<em>id<\/em>(\"j_password_login_uc\")).sendKeys(password);\ndriver.findElement(By.<em>id<\/em>(\"submitLoginUC\")).click();\n\ndriver.switchTo().defaultContent();\n}\n\n\/\/ Search For product\n@Test\n<strong>public<\/strong> <strong>void<\/strong> searchAndSelectProduct() {\ndriver.findElement(By.<em>id<\/em>(\"inputValEnter\")).sendKeys(\"iphone 6s\");\ndriver.findElement(By.<em>cssSelector<\/em>(\".sd-icon.sd-icon-search\")).click();\n\n\/\/ select the first item in the search results\nString css = \".product_grid_row:nth-of-type(1)>div:nth-child(1)\";\ndriver.findElement(By.<em>cssSelector<\/em>(css)).click();\n}\n\n@Test\n<strong>public<\/strong> <strong>void<\/strong> addAndRemoveFromCart() {\n\ndriver.findElement(By.<em>xpath<\/em>(\"\/\/li&#91;contains(text(),'Silver')]\")).click();\ndriver.findElement(By.<em>id<\/em>(\"pincode-check\")).sendKeys(pinCode);\ndriver.findElement(By.<em>id<\/em>(\"buy-button-id\")).click();\n\ndriver.findElement(By.<em>cssSelector<\/em>(\"i&#91;title='Delete Item']\")).click();\nAlert a = driver.switchTo().alert();\na.accept();\n}\n\n@Test\n<strong>public<\/strong> <strong>void<\/strong> logout() {\n\ndriver.findElement(By.<em>linkText<\/em>(\"START SHOPPING NOW\")).click();\nActions act = <strong>new<\/strong> Actions(driver);\nWebElement user = driver.findElement(By.<em>cssSelector<\/em>(\".sd-icon.sd-icon-user\"));\nact.moveToElement(user).build().perform();\ndriver.findElement(By.<em>linkText<\/em>(\"Logout\")).click();\n}\n\n@AfterClass\n<strong>public<\/strong> <strong>void<\/strong> quit() {\ndriver.close();\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong><em>TestNg.xml<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE suite SYSTEM \"http:\/\/testng.org\/testng-1.0.dtd\">\n\u00a0\n&lt;suite thread-count=\"1\" verbose=\"1\" name=\"Gmail Suite\" annotations=\"JDK\" parallel=\"tests\">\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0&lt;test name=\"Flipkart\">\n&lt;classes>\n\u00a0 &lt;class name=\"com.sampletestpackage.Flipkart\"\/>\n&lt;\/classes>\n\u00a0\u00a0\u00a0&lt;\/test>\n\u00a0\u00a0\u00a0\n\u00a0\u00a0&lt;test name=\"SnapDeal\">\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;classes>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;class name=\"com.sampletestpackage.SnapDeal\"\/>\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/classes>\n\u00a0\u00a0\u00a0&lt;\/test>\n&lt;\/suite><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\"><img fetchpriority=\"high\" decoding=\"async\" width=\"728\" height=\"90\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Certification-Online-Course-3.jpeg\" alt=\"selenium certificaton\" class=\"wp-image-6671\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Certification-Online-Course-3.jpeg 728w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Certification-Online-Course-3-300x37.jpeg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>TestNG enables you to execute test methods, test classes, and test cases in parallel, enhancing the efficiency of your testing process. It allows us to run multiple tests at the same time across multiple environments instead of running tests one by one or in sequential order. Additionally, TestNG supports multiple test suites, which means you [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3771,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[156,996,1878,995,968,1879,974,1880],"class_list":["post-3766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-automation-testing","tag-executing-test","tag-multiple-test","tag-multiple-test-suites","tag-parallel-test-execution","tag-selenium-testng","tag-testng","tag-testng-xml-file"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3766","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=3766"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3766\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3771"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}