[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 useTestngto execute parallel test. TestNG gives an option to execute multiple test in parallel in a single configuration file (XML).

原文地址:https://www.cnblogs.com/alicegu2009/p/9098749.html

时间: 2024-10-11 07:50:39

[Selenium+Java] TestNG: Execute multiple Test Suites的相关文章

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

最近来总结下自动化测试 selenium的一些常用框架测试搭配,由简入繁,最简单的就是selenium+java+TestNG了,因为我用的是java,就只是总结下java了. TestNG在线安装: 打开Eclipse   Help ->Install New Software ,   然后Add   "http://beust.com/eclipse" 选择TestNG,finish下一步完成安装. 验证是否安装成功 File->new->other 导入sele

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

selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.支持自动录制动作和自动生成,Net.Java.Python等不同语言的测试脚本.Selenium 测试脚本可以在 Windows.Linux 和 Macintosh等多种平台上运行. 2.TestNG:TestNG是一个测试框架,其灵感来自JU

Selenium+Java+TestNG环境配置

1. JDK 2.eclipse+TestNG >TestNG安装.   Name:testng  Location:http://beust.com/eclipse.如图: 3.selenium webdriver Selenium官网下载selenium webdriver jar 包  http://docs.seleniumhq.org/download/ >下载完成后解压. >在eclipse中创建一个Java Project. >复制刚才解压出来的文件 >粘贴到刚

selenium+java+testNG+maven环境搭建

一.简单介绍 1.selenium: Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.支持自动录制动作和自动生成,Net.Java.Python等不同语言的测试脚本.Selenium 测试脚本可以在 Windows.Linux 和 Macintosh等多种平台上运行. 2.TestNG: TestNG是一个测试框架,其灵感来自JUnit和NUnit的,但引入

selenium + java + testNG 自动化环境搭建

kSelenium终极自动化测试环境搭建(一)Selenium+Eclipse+Junit+TestNG 第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 一路猛击'下一步',OK.安装完成后配置环境变量: JAVA_HOME = E:\Java\Java\jdk1.7.0_15 PATH = %JAVA_HOME%\bin CLAS

selenium+java+testng+ant环境搭建

一.安装 1. 到 http://ant.apache.org/bindownload.cgi 下载 ant发布版本 2. 将下载后的 zip 文件解压缩到任意目录,比如 D:\ant 3. 在环境变量中增加 ANT_HOME=D:\ant( 替换成你解压缩的目录 ) 4. 在环境变量 path 中增加 ;D:\ant\bin; 5. 打开 cmd ,输入 ant ,如果提示一下信息证明成功了 Buildfile: build.xml does not exist! Build failed 或

零成本实现接口自动化测试 – Java+TestNG 测试Restful service

本文是转载Wade Xu的文章http://www.cnblogs.com/wade-xu/p/4229805.html 接口自动化测试 – Java+TestNG 测试 Restful Web Service 关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, 数据分离,Java+Maven+TestNG 本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性

selenium+java:获取列表中的值

selenium+java:获取列表中的值 (2011-08-23 17:14:48) 标签: 杂谈 分类: selenium 初步研究利用java+testNg框架下写selenium测试用例,今天学会了几个API:(1)获取页面上列表中的值,并打印输出:System.out.println(selenium.getTable("xpath=/html/body/div[3]/form/table.1.1")); //输出列表中第1行第1列的值(2)判断页面上是否有某个文本:(只能判

Selenium Web 自动化 - Selenium(Java)环境搭建

Selenium Web 自动化 - Selenium(Java)环境搭建 2016-07-29 第1章 Selenium环境搭建 1.1 下载JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.2 安装和配置JDK 安装目录尽量不要有空格  D:\Java\jdk1.8.0_91; D:\Java\jre8 设置环境变量: “我的电脑”->右键->“