[Selenium+Java] TestNG: Execute multiple Test Suites

Posted alicegu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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,

技术分享图片

Parallel execution in TestNG

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

技术分享图片

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:

技术分享图片

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 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).

以上是关于[Selenium+Java] TestNG: Execute multiple Test Suites的主要内容,如果未能解决你的问题,请参考以下文章

Selenium2(java)TestNG的使用 七

Selenium+TestNG+Java报错:org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)(

Mac系统搭建java+selenium+testng环境

自动化测试框架selenium+java+TestNG——配置篇

selenium第一课(selenium+java+testNG+maven)

Selenium-TestNG-Maven - 获取“java.lang.NoClassDefFoundError:org/openqa/selenium/firefox/FirefoxDriver”