I'm trying to compare the data on my csv file to web table. `
WebElement mytable1 = driver.findElement(By.cssSelector("#dStocks1"));
// To locate rows of table.
List<WebElement> rows_table1 = mytable1.findElements(By.tagName("tr"));
// To calculate no of rows In table.
int rows_count1 = rows_table1.size();
String path = (filePath);
BufferedReader br = null;
String line;
String splitBy = ",(?=([^\"]|\"[^\"]*\")*$)";
br = new BufferedReader(new FileReader(path));
System.out.println("\n CSV data: ");
br.readLine();
while ((line = br.readLine()) != null) {
for (int col = 1; col < line.length();) {
for (int row = 1; row < rows_count1;) {
System.out.println(" ");
String[] cells = line.split(splitBy);
List<WebElement> rows_table2 = mytable1.findElements(By.tagName("tr"));
List<WebElement> Columns_row = rows_table2.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " row " are " columns_count);
for (int column = 1; column < columns_count; column ) {
String celtext = Columns_row.get(column).getText();
String col1 = cells[col].substring(1, cells[col].length() - 1);
assertEquals(celtext, col1);
System.out.print(" CELL: " column " " col1 "\n");
br.readLine();
col ;
}
row ;
System.out.println(" ");
}
}
}
`
This is the result `
Number of cells In Row 1 are 9
CELL: 1 Requested
CELL: 2 PO0815454781
CELL: 3 Main Office Stockroom
CELL: 4 09/13/2022
CELL: 5 DR1212475112
CELL: 6 09/20/2022
CELL: 7 TGP
CELL: 8 3,000,000.00
Number of cells In Row 2 are 9
PASSED: export
FAILED: exportRequested
Index 9 out of bounds for length 9
`
The last index of my csv file is '8' but after printing the line.length
it display '115' and that is the problem. My index is out of bounds. Can someone help me with this. Thank you in advance
CodePudding user response:
Try fixing the csv reading part first. You can use something like this:
public class CSVReader {
private static final String COMMA_DELIMITER = ",";
private static List<String> getRecordFromLine(String line) {
List<String> values = new ArrayList<>();
try (Scanner rowScanner = new Scanner(line)) {
rowScanner.useDelimiter(COMMA_DELIMITER);
while (rowScanner.hasNext()) {
values.add(rowScanner.next());
}
}
return values;
}
public static void main(String[] args) throws FileNotFoundException {
List<List<String>> records = new ArrayList<>();
try (Scanner scanner = new Scanner(new File("src/test/resources/test.csv"));) {
while (scanner.hasNextLine()) {
records.add(getRecordFromLine(scanner.nextLine()));
}
}
records.forEach(System.out::println);
}
}
CSV content:
data1
data4
data7
CodePudding user response:
I do it with FileReader
and CsvReader
. You can try something like this:
public String readCsv(String path) {
try {
FileReader filereader = new FileReader(path);
CSVReader csvReader = new CSVReader(filereader);
String[] nextRecord;
while ((nextRecord = csvReader.readNext()) != null) {
for (String cell : nextRecord) {
return cell;
}
}
} catch (Exception e) {
e.printStackTrace();
}
throw new IllegalArgumentException("Something went wrong. Path to file is wrong or cell not found.");
}