Data Provider with Excel sheet.

DataProvider with excel sheet.

Here we are fetching data from excel sheet and with the help of @dataProvider sending it to @Test method

Java Code-


public class Testing_dataProviderExcelSheet {
	
	static String filename="F:\\eclipse\\Trip\\TestFile1.xlsx";
	static String sheet1="Sheet1";
	static int xrows;
	static int xcols;
    
	//Below @Test method will receive the data.
	@Test(dataProvider="sheetdata")
	public void fillform(String testingSkill, String developmentSkill,String databaseSkill)
	{
	     System.out.println("Testing Skill: "+ testingSkill);
	     System.out.println("Development Skill: "+ developmentSkill);
	       System.out.println("Database Skill: "+ databaseSkill);
	       
	       System.out.println("*********************");
	}
	
	// Below dataProvider method will receive the data.
	@DataProvider(name="sheetdata")
	public Object[][]recods() throws IOException
	{
		
		Object[][] data=exlread(filename,sheet1);
		return data;	
	}
	
    //Below method will read the data from excel sheet.
	public static String[][] exlread(String filepath, String sheet1) throws IOException
	{
		File fpath=new File(filepath);
		FileInputStream fis= new FileInputStream(fpath);
		XSSFWorkbook wb= new XSSFWorkbook(fis);
		XSSFSheet sheet= wb.getSheet(sheet1);
		
		xrows= sheet.getLastRowNum();
		xcols=sheet.getRow(0).getLastCellNum();
		
		String[][]xdata=new String[xrows][xcols];
		int Count=0;
		for(int i=1;i<=xrows; i++)
		{
			XSSFRow row=sheet.getRow(i);

			for(int j=0; j<xcols;j++)
			{
				XSSFCell cell= row.getCell(j);
				String value= cell.toString();
				xdata[Count][j]=value;				
			}	
			Count++;
		}	
		return xdata;
		
	}

}

Output-


[RemoteTestNG] detected TestNG version 7.4.0
Testing Skill: Selenium
Development Skill: JAVA
Database Skill: Sql
*********************
Testing Skill: ETL
Development Skill: Python
Database Skill: MongoDB
*********************
Testing Skill: API Testing
Development Skill: C++
Database Skill: MySQL
*********************
PASSED: fillform("ETL", "Python", "MongoDB")
PASSED: fillform("API Testing", "C++", "MySQL")
PASSED: fillform("Selenium", "JAVA", "Sql")

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0

Related topics-

No comments:

Post a Comment