Unable to write data into an excel file

Hi,
I am able to read data from excel file but unable to write data. My scenario is:
step 1: Hit URL in google search box
Step 2: Hit search button
Then, if I get some result like ‘1 result’ or ‘20 results’…in this case, I want to get the text (that is the URL itself for which I am receiving any result count) into an excel file (want to write into excel file).
But on executing the test case, I am not getting any result in excel file.(Even when I have certain URLs which are showing me result).
Here is my code for keyword:

package myPack

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword

import com.kms.katalon.core.checkpoint.Checkpoint

import com.kms.katalon.core.checkpoint.CheckpointFactory

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords

import com.kms.katalon.core.model.FailureHandling

import com.kms.katalon.core.testcase.TestCase

import com.kms.katalon.core.testcase.TestCaseFactory

import com.kms.katalon.core.testdata.TestData

import com.kms.katalon.core.testdata.TestDataFactory

import com.kms.katalon.core.testobject.ObjectRepository

import com.kms.katalon.core.testobject.TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable

import MobileBuiltInKeywords as Mobile

import WSBuiltInKeywords as WS

import WebUiBuiltInKeywords as WebUI

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteExcel {

@Keyword

public void demoKey(String name) throws IOException{

FileInputStream fis = new FileInputStream(“G:\\Epikso.xlsx”);

XSSFWorkbook workbook = new XSSFWorkbook(fis);

XSSFSheet sheet = workbook.getSheet("Sheet1");

int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

Row row = sheet.createRow(rowCount+1);

Cell cell = row.createCell(0);

cell.setCellType(cell.CELL\_TYPE\_STRING);

cell.setCellValue(name);

FileOutputStream fos = new FileOutputStream("G:\\\Epikso.xlsx");

workbook.write(fos);

fos.close();

}

}

And here is my code for reading and writing the data.(Reading from different excel file and writing into different excel file):

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.stringtemplate.v4.compiler.STParser.element_return as element_return

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory

import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory

import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

import org.openqa.selenium.Keys as Keys

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://www.google.com/’)

WebUI.maximizeWindow()

WebUI.delay(4)

WebUI.setText(findTestObject(‘Google_Objects/Page_Google/input_q’), BacklinkName)

WebUI.click(findTestObject(‘Page_Google/input_btnK’))

WebUI.delay(6)

if (WebUI.verifyElementPresent(findTestObject(‘googleResult_ObjectNew/Page_httpswww.plurk.compmyjwpf - Go/div_1 result (0.24 seconds)’),

3)) {

//KeywordLogger log = new KeywordLogger()

//log.logInfo("This backlink is indexed. ")

WebUI.delay(2)



String result = WebUI.getText(findTestObject('GoogleSearch/Page\_test - Google Search/input\_q'))



WebUI.delay(2)



CustomKeywords.'myPack.WriteExcel.demoKey' (result)

}

Is the file “G:\Epikso.xlsx” present before you run your test case?

If the file isn’t present, then the following line in the keyword will raise a FileNotFoundException and fail.

    FileInputStream fis = new FileInputStream("G:\\Epikso.xlsx");

Yes, the file is present before I execute the test case.

Even the point is that there is no failure in code. My script is passed but the excel file is not written.

What do you mean by saying “the excel file is not written” ?

Before running your test script how the sheet looks like? and after running it how the sheet looks like? Is the timestamp of the file updated or stays unchanged? How is the size of the file?

Have you tried debug-print to see if the code is running as you expect? Certainly not. Then you need to start with tracing how the code is actually working by println statements.

You had better close the workbook object to make sure the data is flushed.

    FileOutputStream fos = new FileOutputStream("G:\\Epikso.xlsx");
    workbook.write(fos);    workbook.close()
    fos.close();