TESTNG-EXCEL - Unable to ignore the first line in excel | Selenium Forum
M
Posted on 30/01/2017
Hi,
I am trying to get data from excel sheet but unable to ignore the first line in which it is username and password
with my code below it prints
username-----password
harsha1-----password1
harsha2-----password2
harsha3-----password3
harsha4-----password4

BUT I DONT WANT username and password obviously in my code below if I CHANGE for(int x=1;x<noofrows;x++)
then I get

null-----null
harsha1-----password1
harsha2-----password2
harsha3-----password3
harsha4-----password4


what to do ?

my code
package Rough;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class NewExcel {
public static Row r ;

@Test(dataProvider="testdata")
public void print(String username, String pass)
{
System.out.print(username+"-----"+pass);
System.out.println();
}
//@DataProvider (name="testdata")
@DataProvider(name="testdata")
public Object[][] readdata() throws Throwable
{
InputStream Ip = new FileInputStream ("C:\\Workspace3\\Parks_Victoria\\src\\Data\\TestSuiteData.xlsx");
Workbook wb = WorkbookFactory.create(Ip);
Sheet s =wb.getSheet("TestData");
int noofrow = s.getLastRowNum();
int noofrows = noofrow+1;
System.out.println(noofrows);
r = s.getRow(0);
int columncount = r.getLastCellNum();
System.out.println(columncount);
String inputdata[][] = new String [noofrows][columncount];


for(int x=0;x<noofrows;x++)
{
r =s.getRow(x);
for(int y =0; y<columncount; y++)
{
Cell c = r.getCell(y);
inputdata[x][y]= c.getStringCellValue();
//System.out.print(c.getStringCellValue());
}
// System.out.println();
}
//Cell c = r.getCell(3);
//System.out.println(c.getStringCellValue());
//return null;
return inputdata;
}
}

M
Replied on 30/01/2017

what are your goals? and what is the problem? I'm not able to understand.


M
Replied on 31/01/2017

Well
My excel sheet data is like this
username password
harsha1 password1
harsha2 password2

I want to be able to select only

harsha1 password1
harsha2 password2
GOAL
I want to ignore the first line in my excel sheet which is
username password and just print the rest of the lines
with my code its printing all the lines starting from username password

code below --------------


package Rough;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Utilities.Base_class;
public class ExcelPackage extends Base_class {
@Test(dataProvider = "harsha")
public void testa(String username, String pass )


{
System.out.println(username+pass);
}
@DataProvider(name ="harsha")
public Object[][] getdata()

{
//Xls_Reader(System.getProperty("user.dir")+""//src");
Xls_Reader xls = new Xls_Reader("C://Workspace3//Parks_Victoria//src//Data//TestSuiteData.xlsx");
int noofrow = xls.getRowCount("TestData");
int noofrows = noofrow+1;
System.out.println(noofrow);
int Columncount = xls.getColumnCount("TestData");
System.out.println(Columncount);
Object data[][] = new Object[noofrows][Columncount];
int dataRow =0;
for(int x = 0; x <noofrows;x++)
{
for (int y =0; y<Columncount;y++)
{

data[dataRow][y]= xls.getCellData("TestData", y, x);
//System.out.print(xls.getCellData("regtest", y, x));
//System.out.print("-------------------------------");
}
//System.out.println();
dataRow++;
}
return data ;

}
}


M
Replied on 31/01/2017

use this, tell me if works.

[quote:1bp9p0pr]for(int [color=#FF0000:1bp9p0pr]x = 1[/color:1bp9p0pr]; x <noofrows;x++)
{
for (int y =0; y<Columncount;y++)
{

data[dataRow][y]= xls.getCellData("TestData", y, x);
//System.out.print(xls.getCellData("regtest", y, x));
//System.out.print("-------------------------------");
}
//System.out.println();
dataRow++;
}
return data ;

}
}[/quote:1bp9p0pr]