• [Selenium+Java] TestNG: Execute multiple Test Suites


    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 are started and executed simultaneously in different threads.

    Here we will see how to run multiple classes (aka different suites) using TestNG.

    Creating a TestNG.xml file for executing test

    In order to do that follow the below steps.

    1. Create a new project in eclipse
    2. Create two 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 classes
    4. Create a new file in your project and name it as testing.xml (Make sure you've installed testing plugin for eclipse, instructions 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 passwrod
    	String password = "";
    
    	// This method is to navigate flipkart URL
    	@BeforeClass
    	public void init() {
    		driver.manage().window().maximize();
    		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    		driver.navigate().to("https://www.flipkart.com");
    	}
    
    	// To log in flipkart
    	@Test
    	public void login() {
    		driver.findElement(By.partialLinkText("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);
    		driver.findElement(By.cssSelector(".submit-btn.login-btn.btn")).click();
    	}
    
    	// Search For product
    	@Test
    	public void searchAndSelectProduct() {
    		driver.findElement(By.id("fk-top-search-box")).sendKeys("moto g3");
    		driver.findElement(
    				By.cssSelector("search-bar-submit.fk-font-13.fk-font-bold"))
    				.click();
    
    		// select the first item in the search results
    		String css = ".gd-row.browse-grid-row:nth-of-type(1) > div:nth-child(1)>div>div:nth-child(2)>div>a";
    		driver.findElement(By.cssSelector(css)).click();
    	}
    
    	@Test
    	public void buyAndRemoveFromCart() {
    		driver.findElement(
    				By.cssSelector(".btn-express-checkout.btn-big.current"))
    				.click();
    		driver.findElement(By.cssSelector(".remove.fk-inline-block")).click();
    		Alert a = driver.switchTo().alert();
    		a.accept();
    	}
    
    	@Test
    	public 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();
    	}
    
    	@AfterClass
    	public 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 passwrod
    	String password = "";
    	String pinCode = "";
    
    	// This method is to navigate flipkart URL
    	@BeforeClass
    	public void init() {
    		driver.manage().window().maximize();
    		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    		driver.navigate().to("https://www.snapdeal.com");
    	}
    
    	// To log in flipkart
    	@Test
    	public void login() {
    		driver.findElement(By.xpath("//button[text()='Login']")).click();
    
    		driver.switchTo().frame("loginIframe");
    
    		driver.findElement(By.cssSelector("div[onClick='getLoginForm()']"))
    				.click();
    
    		driver.findElement(By.id("j_username")).sendKeys(username);
    		driver.findElement(By.id("j_password_login")).sendKeys(password);
    		driver.findElement(By.id("signin_submit")).click();
    
    		driver.switchTo().defaultContent();
    	}
    
    	// Search For product
    	@Test
    	public void searchAndSelectProduct() {
    		driver.findElement(By.cssSelector(".col-xs-20.searchformInput.keyword"))
    				.sendKeys("iphone 6s");
    		driver.findElement(By.cssSelector(".sd-icon.sd-icon-search")).click();
    
    		// select the first item in the search results
    		String css = ".product_grid_row:nth-of-type(1)>div:nth-child(1)";
    		driver.findElement(By.cssSelector(css)).click();
    	}
    
    	@Test
    	public void buyAndRemoveFromCart() {
    
    		driver.findElement(By.xpath("//li[contains(text(),'Silver')]")).click();
    		driver.findElement(By.id("pincode-check")).sendKeys(pinCode);
    		driver.findElement(By.id("buy-button-id")).click();
    		
    		driver.findElement(By.cssSelector("i[title='Delete Item']")).click();
    		Alert a = driver.switchTo().alert();	
    		a.accept();
    	}
    
    	@Test
    	public 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();
    		driver.findElement(By.linkText("Logout")).click();
    	}
    
    	@AfterClass
    	public 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,

    TestNG: Execute multiple Test Suites

    Parallel execution in TestNG

    After creating xml file as shown above, in next step, we will execute the parallel test. Below is the code.

    TestNG: Execute multiple Test Suites

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

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

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

    4) Parallel: To run scripts parallel, value can be 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 will get the following output.

    Output:

    TestNG: Execute multiple Test Suites

    1 name of the suite given in testng.xml

    TestNG: Execute multiple Test Suites

    2 name of the test given in testng.xml

    TestNG: Execute multiple Test Suites

    3 name of the class given in testng.xml

    TestNG: Execute multiple Test Suites

    4 method names annotated with @Test in .java file

    TestNG: Execute multiple Test Suites

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

    Conclusion:

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

  • 相关阅读:
    20200924-2 功能测试
    作业要求20200924-4 代码规范,结对要求
    20200924-1 每周例行报告
    20200929-git地址
    白名单
    作业要求 20200917-1 每周例行报告
    词频统计 SPEC
    20200910-1 每周例行报告
    20200924-3 单元测试,结对
    20200924-5 四则运算试题生成,结对
  • 原文地址:https://www.cnblogs.com/alicegu2009/p/9098749.html
Copyright © 2020-2023  润新知