Dear all,
I am writing my new custom keyword to save all values that displayed on table to my Excel file (code below) but my problem is: sometimes it thrown an errors: _java.io.FileNotFoundException (The process cannot access the file because it is being used by another process) _and as my research the "Synchronized " keyword added to method (means my custom keyword) will resolve this problem, but it not ! so my question is: Does custom keyword support Synchronized methods?
My custom keyword method:
public synchronized void saveExcelString2(String path,ArrayList<String> result,int row,int cell,String sheet){
FileInputStream ExcelFile = new FileInputStream(path);
//Workbook ExcelWBook = WorkbookFactory.create(new File(path));
//ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWBook = WorkbookFactory.create(ExcelFile)
ExcelWSheet = ExcelWBook.getSheet(sheet);
for(int num=0;num<result.size();num++)
{
xRow = ExcelWSheet.getRow(row)
if(xRow==null)
{
ExcelWSheet.createRow(row)
}
xRow = ExcelWSheet.getRow(row)
try {
xCell = xRow.getCell(cell, xRow.RETURN_BLANK_AS_NULL);
} catch (Exception e) {
xCell = xRow.createCell(cell);
xCell.setCellValue(result.get(num));
}
if (xCell == null) {
xCell = xRow.createCell(cell);
xCell.setCellValue(result.get(num));
} else {
xCell.setCellValue(result.get(num));
}
row++
}
ExcelFile.close();
FileOutputStream fileOut = new FileOutputStream(path);
ExcelWBook.write(fileOut);
//fileOut.flush();
if(fileOut!=null )
{
fileOut.close()}
}
P/S: After that I did not use custom keyword anymore, I write a method by groovy language directly in script with @Synchronized annotation and it seems like my problem is solved but calling a method like that in script takes a lot of time when I run my test suite !
Thanks and regards,
Tan Nguyen
my research the "Synchronized " keyword added to method (means my custom keyword) will resolve this problem,
I do not think “Synchronized” keyword would solve your problem. “Synchronized” keyword is about Java Threading; nothing to do with locks over file.
This might help:
1 Like
kazurayam said:
my research the "Synchronized " keyword added to method (means my custom keyword) will resolve this problem,
I do not think “Synchronized” keyword would solve your problem. “Synchronized” keyword is about Java Threading; nothing to do with locks over file.
This might help:
https://stackoverflow.com/questions/46010060/javaexception-the-process-can-not-access-the-file-because-this-file-is-used-b
Hi kazurayam,
Thanks for your reply, I have read article that you provided but you can refer to this URL : https://stackoverflow.com/questions/29231263/java-io-filenotfoundexception-the-process-cannot-access-the-file-because-it-is
and I think that this problem is because I called this method/custom keyword a lot of times to write my values to Excel file so when one method open/close excel file is not finish and another call method/keyword again so it will throw an errors that why I put “Synchronized” keyword here.
I understad your reason why you put “Synchronized” here. May be I am wrong, but I am not sure if Katalon Studio runs multi-threaded internally. If it doesn’t, “Synchorinized” keyword may not be effective.
I think that this problem is because I called this method/custom keyword a lot of times to write my values to Excel file so when one method open/close excel file
If I were given with a similar problem (and actually I have one in my mind), I would try following approach.
- Make a GlobalVariable ''ExcelWBook" with initial value of null
- In the TestListener @BeforeTestSuite, open a Excel file to create instance of POI Workbook. save it into the GlobalVariable.ExcelWBook
- In the Test Cases or TestListener, do something refering to the GlobalVariable.ExcelWBook and update it
- In the TestListener @AfterTestSuite, serialize ExcelWBook into the file and close resources.
The idea is simple: open/close excel file only once.
As far as I know, a GlobalVariable is an instance of java.lang.Object in the global scope during a Test Suite run. You can use it as a long-living container for whatever you need. For example, you can store an instance of org.apache.poi.ss.usermodel.Workbook into a GlobalVariable.
1 Like
kazurayam said:
I think that this problem is because I called this method/custom keyword a lot of times to write my values to Excel file so when one method open/close excel file
If I were given with a similar problem (and actually I have one in my mind), I would try following approach.
- Make a GlobalVariable ''ExcelWBook" with initial value of null
- In the TestListener @BeforeTestSuite, open a Excel file to create instance of POI Workbook. save it into the GlobalVariable.ExcelWBook
- In the Test Cases or TestListener, do something refering to the GlobalVariable.ExcelWBook and update it
- In the TestListener @AfterTestSuite, serialize ExcelWBook into the file and close resources.
The idea is simple: open/close excel file only once.
As far as I know, a GlobalVariable is an instance of java.lang.Object in the global scope during a Test Suite run. You can use it as a long-living container for whatever you need. For example, you can store an instance of org.apache.poi.ss.usermodel.Workbook into a GlobalVariable.
I think its a good idea to try ! if you refer to my 1st post I have done some familiar idea( just open workbook once and loop for all values and save to excel file then close workbook/output stream) but anyway your idea is very good and I will have a try on it. Thanks for your advice !