Write xls multiple times using multiple Test Annotations | Selenium Forum
M
Posted on 30/10/2015
HI,

I have a xls read and write code, I am trying to write the data in to xls sheet from Test Annotation (1) , which its writes sucessfully, but if i try to write the data in to xls sheet from another Test annotation (2) , the data written by the first Test anotation is cleared and the Data from Test Annotation 2 is written in to the flle. I need help where the data should not over write if try to write multiple times from differenr annotations in the while runinng batch files in Selenium webdriver


Main Class is below
---------------------

package Framework;

import Utility.Utils;

import org.apache.bcel.classfile.Constant;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.openqa.selenium.Alert;
//import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import Utility.Constants;
import Functions.Function_Classes;


public class MainClass {

String vBrowser = "FireFox";
WebDriver driver;
String xpath;
String xlURL, xlUsername, xlPassword, xlStore, xlLoggedname, xlCustFName, xlCustLName, xlssn, xldob, xlRoutingno, xlbankname, xlaccno, xlcheckno;
String[][] xlData;
String path = "C:/1/Seleniumproject/TestData_2007.xls";
String wpath = "C:/1/Seleniumproject/TestResult_2007.xls";

@Test (priority = 1)
public Test1() throws Exception {
xlData = readXL(path,"TestData");
System.out.println("xcode_No_Cust_No_Cart");
xlData[1][1] = "Pass";
writeXL(wpath,"Login",xlData);

xlData[1][2] = "Verifying Sucessfull";
writeXL(wpath,"Login",xlData);
}

@Test (priority = 2)
public Test1() throws Exception {
xlData = readXL(path,"TestData");
System.out.println("xcode_No_Cust_No_Cart");
xlData[2][1] = "Fail";
writeXL(wpath,"Login",xlData);
xlData[2][2] = "Verifying Failed";
writeXL(wpath,"Login",xlData);
}



// Method to read and Write from xls sheet
//===================================
//Method to read XL
public static String[][] readXL(String fPath, String fSheet) throws Exception{
String[][] xData;
int xRows, xCols;

File myxl = new File(fPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(fSheet);
xRows = mySheet.getLastRowNum()+1;
xCols = mySheet.getRow(0).getLastCellNum();
System.out.println("Total Rows in Excel are " + xRows);
System.out.println("Total Cols in Excel are " + xCols);
xData = new String[xRows][xCols];
for (int i = 0; i < xRows; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols; j++) {
HSSFCell cell = row.getCell(j);
String value = "-";
if (cell!=null){
value = cellToString(cell);
}
xData[i][j] = value;
// System.out.print(value);
// System.out.print("----");
}
// System.out.println("");
}
return xData;
}

//Change cell type
public static String cellToString(HSSFCell cell) {
// This function will convert an object of type excel cell to a string value
int type = cell.getCellType();
Object result;
switch (type) {
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING: //1
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: //2
throw new RuntimeException("We can't evaluate formulas in Java");
case HSSFCell.CELL_TYPE_BLANK: //3
result = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //4
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR: //5
throw new RuntimeException ("This cell has an error");
default:
throw new RuntimeException("We don't support this cell type: " + type);
}
return result.toString();
}

// Method to write into an XL
public static void writeXL(String fPath, String fSheet, String[][] xData) throws Exception{

File outFile = new File(fPath);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet osheet = wb.createSheet(fSheet);
int xR_TS = xData.length;
System.out.println(xR_TS);
int xC_TS = xData[0].length;
System.out.println(xC_TS);
for (int myrow = 0; myrow < xR_TS; myrow++) {
HSSFRow row = osheet.createRow(myrow);
for (int mycol = 0; mycol < xC_TS; mycol++) {
HSSFCell cell = row.createCell(mycol);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}

}



QUESTION
-----------

I am running the whole class Using Test NG

The final output in the xls sheet ONLY displays
cell (2)(1) = Fail
Cell (2)(2) = Verifying Failed



the data below is cleared
cell (1)(1) = Pass
Cell (1)(2) = Verifying Sucessfull


I want both the data in cell(1)(1) , Cell (1)(2), Cell(2)(1), cell(2)(2) to be displayed not to clear the first Annotation test class when second annotation


Pls i need help ASAP

Regards,
Rakesh Gudla
1-614-787-4213

M
Replied on 30/10/2015

use [b:1p9dq0fs][color=#FF0000:1p9dq0fs]xlsx [/color:1p9dq0fs][/b:1p9dq0fs]instead of [b:1p9dq0fs]xls[/b:1p9dq0fs].


M
Replied on 03/11/2015

chnaged functio to //Method to read XL
public static String[][] readXL(String fPath, String fSheet) throws Exception{
String[][] xData;
int xRows, xCols;

File myxl = new File(fPath);
FileInputStream myStream = new FileInputStream(myxl);
XSSFWorkbook myWB = new XSSFWorkbook(myStream);
XSSFSheet mySheet = myWB.getSheet(fSheet);
xRows = mySheet.getLastRowNum()+1;
xCols = mySheet.getRow(0).getLastCellNum();
System.out.println("Total Rows in Excel are " + xRows);
System.out.println("Total Cols in Excel are " + xCols);
xData = new String[xRows][xCols];
for (int i = 0; i < xRows; i++) {
XSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols; j++) {
XSSFCell cell = row.getCell(j);
String value = "-";
if (cell!=null){
value = cellToString(cell);
}
xData[i][j] = value;
// System.out.print(value);
// System.out.print("----");
}
// System.out.println("");
}
return xData;
}

//Change cell type
public static String cellToString(XSSFCell cell2) {
// This function will convert an object of type excel cell to a string value
int type = cell2.getCellType();
Object result;
switch (type) {
case XSSFCell.CELL_TYPE_NUMERIC: //0
result = cell2.getNumericCellValue();
break;
case XSSFCell.CELL_TYPE_STRING: //1
result = cell2.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_FORMULA: //2
throw new RuntimeException("We can't evaluate formulas in Java");
case XSSFCell.CELL_TYPE_BLANK: //3
result = "";
break;
case XSSFCell.CELL_TYPE_BOOLEAN: //4
result = cell2.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_ERROR: //5
throw new RuntimeException ("This cell has an error");
default:
throw new RuntimeException("We don't support this cell type: " + type);
}
return result.toString();
}

// Method to write into an XL
public static void writeXL(String fPath, String fSheet, String[][] xData) throws Exception{

File outFile = new File(fPath);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet osheet = wb.createSheet(fSheet);
int xR_TS = xData.length;
System.out.println(xR_TS);
int xC_TS = xData[0].length;
System.out.println(xC_TS);
for (int myrow = 0; myrow < xR_TS; myrow++) {
XSSFRow row = osheet.createRow(myrow);
for (int mycol = 0; mycol < xC_TS; mycol++) {
XSSFCell cell = row.createCell(mycol);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}



imported the xlsx poi files

error : "java.lang.NullPointerException"


M
Replied on 03/11/2015

Can u pls let me know how to handle xls files rather than xlsx files ps
its really urgent


M
Replied on 03/11/2015

HI,

I used xlsx file , still its doing the same thing

erasing the old written data and rewriting again

is there any thing i need to change n the write file ?


M
Replied on 03/11/2015

A NullPointerException means that one of the variables you are passing is null, but the code tries to use it like it is not.

For example, If I do this:

Integer myInteger = null;
int n = myInteger.intValue();
The code tries to grab the intValue of myInteger, but since it is null, it does not have one: a null pointer exception happens.


M
Replied on 03/11/2015

[quote:1gv69bkq]Can u pls let me know how to handle xls files rather than xlsx files ps
its really urgent
[/quote:1gv69bkq]

i'm afraid we don't have working code for that. but you can convert files to xlsx easily. and then use xls_reader.java


M
Replied on 04/11/2015

I have converted in to xlsx file , but the problem still remails, if I try to write to xls sheet from more than one Test Annotation, its over writing ,

Could you let me know if u have some other read and write code which i can use

I dont mind using other read write code


M
Replied on 05/11/2015

[quote:2dkdlxc7]if I try to write to [color=#FF0000:2dkdlxc7]xls [/color:2dkdlxc7]sheet from more than one Test Annotation, its over writing[/quote:2dkdlxc7]

first of all are you using [color=#FF0000:2dkdlxc7]xls [/color:2dkdlxc7]or [color=#FF0000:2dkdlxc7]xlsx[/color:2dkdlxc7].

how is it overwriting? does the code over write on a few cells or does it overwrites the entire file?


M
Replied on 06/11/2015

Hi,

Its over writing entire file

For example " Read xls file has data in few cells (eg : "hello" in cell (1,1)) , so in write file each annotation while executing is writing all the data from read xls sheet + what ever output from the particular annotation ,

1st test annotation ("hai" in cell (2,1)) : it writes the data from read xls sheet + data out put from Test annotation 1 ==
output in write xls sheet will be hello (1,1) and hai (2,1)

when 2nd annotation runs
2nd test annotation ("how are u" in cell (3,2)): it writes the data from read xls sheet + data out put from Test anotation 2 (clears all the data witten by Test anotation 1 )
output will be : hello (1,1) and ("how are u" in cell (3,2)) but clears hai (2,1)

My problem is when both annotation run , the output must be
hello (1,1) and hai (2,1) and ("how are u" in cell (3,2)) SHOULD NOT clear hai (2,1)

Pls help me


M
Replied on 06/11/2015

are you using xls or xlsx?

its very important that you use xlsx. in your reply you're always implying that you're using xls and not xlsx.


M
Replied on 09/11/2015

It does not matter if i am using xls or xlsx i am facing same issue


M
Replied on 12/11/2015

upload your entire project.


M
Replied on 13/11/2015

Sir, can i call you , if possible, so that I can explain the problem. kindy let me now when can i call you
Its really urgent , as i am facing this problem since last 20 days
my no is : 614-787-4213


M
Replied on 15/11/2015

I understood your problem now upload your code so I can run it.


M
Replied on 18/11/2015

Sir, Is it safe to upload my code in this general forum , i am little skeptical abt the security, as I am concerned abt the code.

I can email u to ur personal email , if u can provide .

U can call me at 614-787-4213 or kindly let me know when can i call u

Regards,
Rakesh Gudla


M
Replied on 18/11/2015

qtpselenium.supp0rt@gmail.com


M
Replied on 21/11/2015

Thanks ,

I have sent you email with the script , function and utils

Kindly check ur email