chapter 9: Run multiple test class or suite in selenium

Lets see first how in real project we setup the testcase and then testng.xml file to run test classes.

Now we will see how to run multiple test suite using TestNG.

Step 1) 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 mercurytours.java and clothigsite.java) and copy the below code in respective classes
  4. Create a new file in your project and name it as testing.xml  Testng.xml contains all configuration (classnames, testnames and suitnames.

mercurytours.java:

package suite1;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class mercurytours {
WebDriver driver = new ChromeDriver();
String username = "Testology"; // Change to your username and passwrod
String password = "Testology12";

// This method is to navigate mercurytours URL
@BeforeClass
public void init() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to("http://newtours.demoaut.com/");
	}

// To log in mercury tours
@Test
public void login() {
driver.findElement(By.linkText("SIGN-ON")).click();
driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password); driver.findElement(By.cssSelector("input[name='login'][value='Login']")).click();
		}
// Select one way from flight details
@Test
public void SelectProduct() {
		driver.findElement(By.cssSelector("input[type='radio'][value='oneway']")).click();
		}
  @Test
public void logout() {
driver.findElement(By.linkText("SIGN-OFF")).click();
}

@AfterClass
public void quit() {
driver.close();
	}
}

clothingsite.java:

package suite2;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class clothingsite {
WebDriver driver = new ChromeDriver();
String username = "pjadhav7777@gmail.com"; // Change to your username and passwrod 
String password = "Test12";
String pinCode = "90002";
// This method is to navigate URL
@BeforeClass
public void init() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to("http://automationpractice.com/index.php");
	}
// To log in 
@Test
public void login() {
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("email")).sendKeys(username);
driver.findElement(By.id("passwd")).sendKeys(password);
driver.findElement(By.id("SubmitLogin")).click();
driver.switchTo().defaultContent();
	}
// Search For product
@Test
public void searchAndSelectProduct() {
driver.findElement(By.cssSelector("input[id='search_query_top'][placeholder='Search']")).sendKeys("printed dress");
driver.findElement(By.cssSelector("input[class='search_query form-control ac_input']")).click();
// select the first item in the search results
String css = ".search_query_top: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();
	}
}

Final project structure looks like below,