Skip to main content

Run Data-Enabled TestNG Scenarios

Below

Run article explains about Data Enabling usingData-Enabled TestNG DataScenarios

Provider.

Use ItTestNG coversdata belowproviders mentionedfor topics:maintained Java/TestNG assets that still run through AIQ. For current Scenario Editor tests, prefer DPLs or approved AIQ test data sources when they can meet the same need.

  • Keep
  • Howthe data provider deterministic and easy to Createreset.
  • Use aapproved TestNGnon-production Classtest indata.
  • Separate Eclipse?
    environment *configuration Howfrom usetest TestNGdata.
  • Log classenough jar filecontext to rundiagnose Functional scenario in Appvance?
Run Load test using TestNG class(with DataProvider) in Appvance.
 How to Create a TestNG Class in Eclipse?
 Pre-requisite: *Download jxl-2.6.jar file (http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm)
 *To install TestNG in Eclipse: http://toolsqa.com/selenium-webdriver/install-testng/ Create a new package in Eclipse Right Click package Name Select Build PathConfigure Build path -Select Libraries tab Click Add External Jars -Browse and add downloaded jxl-2.6.jar file. Create a TestNG class with TestNG annotations (DataProvider to readwhich data fromrow Xlsfailed file)without exposing secrets.

[@DataProvider:

Validation Marksworkflow

a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method.The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.]

Sample code to read data from Xls file:

packageTestNG;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.Random;
importjxl.Sheet;
importjxl.Workbook;
importjxl.read.biff.BiffException;
public class RandamData {
public static void main(String args[])
{
getExcelData("F:Spree.xls","Sheet1");
}
public String[][] getExcelData(String fileName, String sheetName) {
String[][] arrayExcelData = null;
try {
FileInputStreamfs = newFileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet(sheetName);
inttotalNoOfCols = sh.getColumns();
inttotalNoOfRows = sh.getRows();
arrayExcelData = new String[totalNoOfRows][totalNoOfCols];
for (inti= 0 ; i<totalNoOfRows; i++) {
for (intj=0; j<totalNoOfCols; j++) {
arrayExcelData[i][j] = sh.getCell(j,i).getContents();
} }
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
} catch (BiffExceptione) {
e.printStackTrace();
} return arrayExcelData;
}


To use in TestNG:

@DataProvider(name="Login")
public Object[][] loginData() {
Object[][] arrayObject=getExcelData("F:Sample.xls","Sheetname");
return arrayObject;
}

Note: For example refer attached Java file(ExcelData.java) with TestNGclass and annotations that uses Xls data to login .

-Now create a Jar file for above TestNG class file to run script in Appvance.


How useTestNG class jar file to run Functional scenario in Appvance:

-Open a Appvance and click Scenario builder from start screen.
-Select a Functional Test Test Type in scenario builder.
-Create a Scenario for TestNG by selecting TestNG test type under Test case panel.
-Browse and add Jar file that contains TestNG code
-Enter

  1. Run the TestNG classsuite namelocally asor "packagename.classname".
    -Navigatein toCI resourceswith panel,a clicksmall Adddata resources.
    -Selectset.
  2. Confirm resourcedependency Type as Jarpackaging and addJava jxl-2.6.jarversion file.
    -Savecompatibility.
  3. Execute thefrom ScenarioAIQ with a controlled data sample.
  4. Review output for data, classpath, and clickenvironment Playfailures button.before scaling.

Note: Refer attached ExcelData.jar and Exceldata1.scenario file

Output:



 To run a Load test using TestNG : 

 -Use a random number in a TestNG script to read a data from Excel.
 Sample code:

@DataProvider(name="Login")
	public Object[][] loginData() {
		Object[][] arrayObject=null;
		for(int i=0;i<1;){
			for(int j=0;j<1;j++){
		arrayObject = getExcelData("F:\\Spree.xls","sheetname");
		}
		break;
		}
		return arrayObject;
	}
public static String[][] getExcelData(String fileName, String sheetName) {
	String[][] arrayExcelData = null;
	try {
		FileInputStream fs = new FileInputStream(fileName);
		Workbook wb = Workbook.getWorkbook(fs);
		Sheet sh = wb.getSheet(sheetName);
		int totalNoOfRows = sh.getRows();
		int totalNoOfCols = sh.getColumns();
		//System.out.println("No. of rows: " + totalNoOfRows);
		//System.out.println("No. of columns: " + totalNoOfCols);
		//To generate Random Number 
		arrayExcelData = new String[totalNoOfRows][totalNoOfCols];
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(totalNoOfRows);
		System.out.println("Generated : " + randomInt);
		arrayExcelData = new String[1][totalNoOfCols];
		for(int i=0;i<totalNoOfRows;){
			for(int j=0;j<totalNoOfCols;j++){
		    arrayExcelData[i][j]=sh.getCell(j,randomInt ).getContents();
	        System.out.println("data From excel : " + arrayExcelData[i][j]);
			}
			break;
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
		e.printStackTrace();
	} catch (BiffException e) {
		e.printStackTrace();
	}
	return arrayExcelData;
}
}

Note: For example refer attached Random.jar file and DataEnabled_Load1.scenario file.



 For Example, refer the attached TestNG.Zip file that contains:
 1) Java file(TestNG class)
 2) Jar files(Used to create scenario in Appvance).
 3) Scenario files.

TestNG.zip


Last updated: JuneSeptember 26,8, 20232026

If you have any feedback on this topic, please contact us at documentation@appvance.ai