Preserve Order in TestNG

Test Case: 1

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase1 
{
	@Test
	public void TestCase1()
	{
		System.out.println("Executing 1st Test Case");
	}
	@Test
	public void TestCase2()
	{
		System.out.println("Executing 2nd Test Case");
	}

}

Test Case: 2

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase2 
{
	@Test
	public void TestCase3()
	{
		System.out.println("Executing 3rd Test Case");
	}
	@Test
	public void TestCase4()
	{
		System.out.println("Executing 4th Test Case");
	}

}

Test Case: 3

package testng;

import org.testng.annotations.Test;

public class PreserveOrderCase3 
{
	@Test
	public void TestCase5()
	{
		System.out.println("Executing 5th Test Case");
	}
	@Test
	public void TestCase6()
	{
		System.out.println("Executing 6th Test Case");
	}

}

testng.xml (preserve-order=true > The tests will be executed in order defined in xml)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve Order Test Suite" parallel="classes" thread-count="2">
  <test name="Preserve Order Execution" preserve-order="true" >
    <classes>
      <class name="testng.PreserveOrderCase1"/>
      <class name="testng.PreserveOrderCase2"/>
      <class name="testng.PreserveOrderCase3"/>
    </classes>
  </test>
</suite>

Output: preserve-order=true

testng.xml (preserve-order=false > Test will not be executed in order. They will get executed in an unpredictable order)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve Order Test Suite" parallel="classes" thread-count="2">
  <test name="Preserve Order Execution" preserve-order="false" >
    <classes>
      <class name="testng.PreserveOrderCase1"/>
      <class name="testng.PreserveOrderCase2"/>
      <class name="testng.PreserveOrderCase3"/>
    </classes>
  </test>
</suite>

Output: preserve-order=false

Parallel Execution of Classes in TestNG

Test Case: 1

package testng;

import org.testng.annotations.Test;

public class ParallelExecutionCase1 
{
	@Test
	public void TestCase1()
	{
		System.out.println("Executing 1st Test Case ---" + getClass().getSimpleName()+ " with Thread Id:- " + Thread.currentThread().getId());
	}
	@Test
	public void TestCase2()
	{
		System.out.println("Executing 2nd Test Case ---" + getClass().getSimpleName()+ " with Thread Id:- " + Thread.currentThread().getId());
	}

}

Test Case: 2

package testng;

import org.testng.annotations.Test;

public class ParallelExecutionCase2 
{
	@Test
	public void TestCase3()
	{
		System.out.println("Executing 3rd Test Case ---" + getClass().getSimpleName()+ " with Thread Id:- " + Thread.currentThread().getId());
	}
	@Test
	public void TestCase4()
	{
		System.out.println("Executing 4th Test Case ---" + getClass().getSimpleName()+ " with Thread Id:- " + Thread.currentThread().getId());
	}


}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Test Suite" parallel="classes" thread-count="2">
  <test name="Parallel Execution">
    <classes>
      <class name="testng.ParallelExecutionCase1"/>
      <class name="testng.ParallelExecutionCase2"/>
    </classes>
  </test>
</suite>

 

Read Data from CSV using CSVReader

package com.Testing;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import au.com.bytecode.opencsv.CSVReader;
import jxl.read.biff.BiffException;

public class ReadDataUsingCSVReader {
	
	public static WebDriver driver;
	
	
  @SuppressWarnings("resource")
@Test
  public void ReadDatafromXLSL() throws InterruptedException, BiffException, IOException {	  
			  
	  System.out.println("----------------------------ReadDatafromXLSL ----------------------------"+"\n" +"\n");	 	  
	  
	  
	// This will load csv file 
	  CSVReader reader = new CSVReader(new FileReader("D:\\QA\\demo.csv"));
	  
	// this will load content into list
	  List<String[]> li=reader.readAll();
	  System.out.println("Total rows which we have is "+li.size());
	            
	 // create Iterator reference
	  Iterator<String[]>i1= li.iterator();
	    
	 // Iterate all values 
	 while(i1.hasNext()){
	     
	 String[] str=i1.next();
	   
	 System.out.print(" Values are ");
	 
	 for(int i=0;i<str.length;i++)
	{
	 
	   System.out.print(" "+str[i]);
	 
	}
	   System.out.println("   ");
	     
	    
	}
	  
	  
	        
      	  	
      
  }
  
 
}

 

Connect mySQL database using Selenium Webdriver

package testng;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.testng.annotations.Test;

public class ConnectmySQL {
	
	@Test
	public void testDB() throws ClassNotFoundException, SQLException
	{
		Class.forName("com.mysql.jdbc.Driver");
						
		Connection cnt = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentInfo","root","Akshaya@19");
					
		Statement smt = cnt.createStatement();
		
		ResultSet rs= smt.executeQuery("select * from studentName");
		
		while(rs.next())
		{
			String firstName = rs.getString("firstName");
			
			System.out.println("Database record is: --- "+firstName);
			
			
		}
		
		
	}

}

 

mysql> select * from studentName;
+———–+———-+
| firstName | lastName |
+———–+———-+
| Ram | Rao |
| Krishna | Kamath |
+———–+———-+

Run a test cases in multiple browsers in Selenium Webdriver

package testng;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class verifyFacebookTitle {
	
	WebDriver driver;
	
	@Test
	@Parameters("browser")
	public void verifyTitle(String browserName)
	{
		if(browserName.equalsIgnoreCase("firefox"))
		{
			driver=new FirefoxDriver();			
			
		}
		else if(browserName.equalsIgnoreCase("chrome"))
		{			
			System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
			
			driver=new ChromeDriver();
						
		}
		driver.manage().window().maximize();

		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.get("http://www.facebook.com");
		
		System.out.println("Title:---" +driver.getTitle());
		
		driver.quit();
	}

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" verbose="1" >
  <test name="firefox Test Suite"   >
  <parameter name="browser" value="firefox"></parameter>
  <classes>
  <class name ="testng.verifyFacebookTitle"></class>
  </classes>    
 </test>
 <test name="chrome Test Suite"   >
  <parameter name="browser" value="chrome"></parameter>
  <classes>
  <class name ="testng.verifyFacebookTitle"></class>
  </classes>    
 </test>
</suite>

 

Handling Page Scroll Down and Scroll Up

package testng;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class pageScroll {
	
	WebDriver driver;
	
	@Test
	public void scrollDrown() throws Throwable
	{
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.facebook.com");
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,600)");
		
		driver.quit();
	}
	@Test
	public void scrollUp() throws Throwable
	{
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.facebook.com");
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,600)");
		
		Thread.sleep(5000);
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,-600)");
		
		Thread.sleep(5000);
		
		Thread.sleep(5000);
		
		driver.quit();
	}

}

 

1 2 3 4 7