[Selenium+java] Testng:execute multiple Test Suites

Source: Internet
Author: User
Tags xpath testng guru99

Original URL: https://www.guru99.com/testng-execute-multiple-test-suites.html

Testng:execute multiple Test Suites


TestNG enables you to run test methods, test classes and test cases in parallel inside your project. By performing parallel execution, we can reduce the ' execution time ' as tests is started and executed simultaneously in D Ifferent threads.

Here we'll see how to run Multiple classes (aka different suites) using TestNG.

Creating a testng.xml file for executing test

In order to does that follow the below steps.

    1. Create a new project in eclipse
    2. Create packages in the projects (name them as com.suite1 and Com.suite2)
    3. Create a class in each package (name them as Flipkart.java and Snapdeal.java) and copy the below code in respective Classe S
    4. Create a new file in your project and name it as Testing.xml (make sure "VE installed testing plugin for Eclipse, instr Uctions available here). Testng.xml contains all configuration (Classnames, Testnames, and Suitnames.

Flipkart.java

Package Com.suite1;import Java.util.concurrent.timeunit;import Org.openqa.selenium.alert;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.interactions.actions;import Org.testng.annotations.afterclass;import Org.testng.annotations.beforeclass;import org.testng.annotations.Test; public class Flipkart{webdriver Driver = new Firefoxdriver (); String username = ""; Change to your username and passwrodstring password = "";//This method was to navigate Flipkart Url@beforeclasspublic V OID init () {driver.manage (). window (). Maximize ();d river.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Driver.navigate (). to ("https://www.flipkart.com");} To log in flipkart@testpublic void login () {driver.findelement ("login"). Click (); Driver.findelement (By.cssselector (". Fk-input.login-form-input.user-email")). SendKeys (username); Driver.findelement (by.csssElector (". Fk-input.login-form-input.user-pwd")). SendKeys (password);d river.findelement (By.cssselector (". Submit-btn.login-btn.btn ")). Click (); Search for product@testpublic void Searchandselectproduct () {driver.findelement (By.id ("Fk-top-search-box")). SendKeys ("Moto G3");d river.findelement (By.cssselector ("Search-bar-submit.fk-font-13.fk-font-bold")). Click ();// Select the first item in the search resultsstring CSS = ". Gd-row.browse-grid-row:nth-of-type (1) > Div:nth-child (1) >d Iv>div:nth-child (2) >div>a ";d river.findelement (By.cssselector (CSS)). Click (); @Testpublic void Buyandremovefromcart () {driver.findelement (By.cssselector (". btn-express-checkout.btn-big.current "). Click ();d river.findelement (By.cssselector (". Remove.fk-inline-block ")). Click (); Alert A = Driver.switchto (). alert (); A.accept ();} @Testpublic void Logout () {actions s = new Actions (driver); Webelement user = Driver.findelement (by.partiallinktext (username)); s.movetoelement (user). Build (). Perform (); Driver.findelement (by.LinkText ("Logout")). Click (); @AfterClasspublic void Quit () {driver.close ();}}

Snapdeal.java

Package Com.suite2;import Java.util.concurrent.timeunit;import Org.openqa.selenium.alert;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.interactions.actions;import Org.testng.annotations.afterclass;import Org.testng.annotations.beforeclass;import org.testng.annotations.Test; public class Snapdeal {Webdriver Driver = new Firefoxdriver (); String username = ""; Change to your username and passwrodstring password = ""; String pincode = "";//This method was to navigate Flipkart url@beforeclasspublic void init () {driver.manage (). window (). Max Imize ();d river.manage (). Timeouts (). implicitlywait (timeunit.seconds);d river.navigate (). to ("https://www.snapdeal.com");} To log in flipkart@testpublic void login () {driver.findelement (By.xpath ("//button[text () = ' login ']"). Click (); Driver.switchto (). FRAME ("Loginiframe");d river.findelement (By.cssselector ("div[onclick=" GetlOginform () "). Click ();d river.findelement (by.id (" J_username ")). SendKeys (username);d river.findelement (By.id (" J _password_login ")). SendKeys (password);d river.findelement (by.id (" Signin_submit ")). Click ();d River.switchto (). Defaultcontent ();} Search for product@testpublic void Searchandselectproduct () {driver.findelement (By.cssselector ("). Col-xs-20.searchforminput.keyword ")). SendKeys (" iphone 6s ");d River.findelement (By.cssselector (". Sd-icon.sd-icon-search "). Click ();//Select the first item in the search resultsstring CSS =". Product_grid_row:nth-of-ty PE (1) >div:nth-child (1) ";d river.findelement (By.cssselector (CSS)). Click (); @Testpublic void Buyandremovefromcart () {driver.findelement (By.xpath ("//li[contains (Text (), ' Silver ')]). Click (); Driver.findelement (By.id ("Pincode-check")). SendKeys (Pincode);d river.findelement (by.id ("Buy-button-id")). Click ( );d river.findelement (By.cssselector ("i[title= ' Delete Item ']"). Click (); Alert A = Driver.switchto (). alert (); A.accept ();} @Testpublic void Logout () {DrivEr.findelement (By.linktext ("START shopping Now"). Click (); Actions s = new actions (driver); Webelement user = Driver.findelement (By.cssselector (". Sd-icon.sd-icon-user")); s.movetoelement (user). Build (). Perform ();d river.findelement (By.linktext ("Logout")). Click (); @AfterClasspublic void Quit () {driver.close ();}}

Testng.xml

<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite thread-count= "1" verbose= "1" name= "Gmail Suite" annotations= "JDK" parallel= "tests" >           <test name= "Flipkart" > <classes>   <class name= " Com.suite1.Flipkart "/> </classes>   </test>     <test name=" Myntra ">     <classes>       <class name= "com.suite2.SnapDeal"/>     </classes>   </test></suite>

FINAL project structure looks like below,

Parallel execution in TestNG

After creating XML file as shown above, in next step, we'll execute the parallel test. Below is the code.

1) thread-count: This is used for parallel execution and based on the number script. It'll execute in parallel or sequential order.

2) verbose: It is used to log the execution details in the console. The value should is 1-10. The log details in the console window would get more detailed and clearer as you increase the value of the verbose attribut E in the Testng.xml configuration file.

3) name: Name of the suite. Here it's "Gmail Suite"

4) Parallel: To run scripts Parallel, value can is tests/classes/methods/suites. Default value is none

Right click on the Testing.xml and select Run as testing, once successful you'll see all the results

When you execute the above code, you'll get the following output.

Output:

1 Name of the suite given in Testng.xml

2 name of the test given in Testng.xml

3 name of the class given in Testng.xml

4 method names annotated with @Test in. java file

Likewise, it would execute test suite for snap deal as well.

Conclusion:

Here we have the seen how to use Testng to execute parallel test. TestNG gives an option to the Execute multiple test in parallel with a single configuration file (XML).

[Selenium+java] Testng:execute multiple Test Suites

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.