How to write into a text file after the test suite executes in CircleCI?

I have one test case which has to give output as a JSON and write it into a .txt file .
It works well and creates a txt file in local and writes into it. But when I run the same test with CircleCI, I get java.io.FileNotFoundException

Here is my code snippet -

def jsonInvoice_details = new groovy.json.JsonBuilder()
jsonInvoice_details fields: org_invoice

def jsonCC_details = new groovy.json.JsonBuilder()
jsonCC_details fields: org_cc

//KeywordUtil.logInfo(jsonInvoice_details.toString())
//KeywordUtil.logInfo(jsonCC_details.toString())

try{
FileWriter fw_invoice_details=new FileWriter(“./report/BillingInfo/BillingInfo_Invoices.txt”);
fw_invoice_details.write(jsonInvoice_details.toString())
fw_invoice_details.close()
FileWriter fw_cc_details=new FileWriter(“./report/BillingInfo/BillingInfo_CC.txt”);
fw_cc_details.write(jsonCC_details.toString())
fw_cc_details.close();

}
catch(Exception e){
KeywordUtil.logInfo(e.toString())
}

How do I get the text file in the Artifacts tab of Circle CI ?

Please give your suggestions.
Thanks in advance.

Is this parent directory present when the test suite executes in CircleCI?

If not, you need to make it before calling FileWriter.write(), otherwise surely you would see a java.io.FileNotFoundException.

1 Like

Yes @kazurayam, it was not present when test suites executed. Later i changed the code as below -

        def jsonInvoice_details = new groovy.json.JsonBuilder()
        jsonInvoice_details fields: org_invoice

        def jsonCC_details = new groovy.json.JsonBuilder()
        jsonCC_details fields: org_cc

        //KeywordUtil.logInfo(jsonInvoice_details.toString())
        //KeywordUtil.logInfo(jsonCC_details.toString())
        String invoices_filename = "./report/BillingInfo_Invoices.txt"
        String cc_filename = "./report/BillingInfo_CC.txt"
        try{
        	File invoicesfile = new File(invoices_filename)
        	File ccfile = new File(cc_filename)
        	if (!invoicesfile.getParentFile().exists()){
        	invoicesfile.getParentFile().mkdirs()
        	}
        	if (!invoicesfile.exists()){
        	invoicesfile.createNewFile()
        	}
        	if (!ccfile.exists()){
        		ccfile.createNewFile()
        		}
        	
        	FileWriter fw_invoice_details=new FileWriter(invoices_filename)
        	fw_invoice_details.write(jsonInvoice_details.toString())
        	KeywordUtil.logInfo("Invoice Details file present at ---> "+invoicesfile.getAbsolutePath())
        	fw_invoice_details.close()
        	FileWriter fw_cc_details=new FileWriter(cc_filename)
        	fw_cc_details.write(jsonCC_details.toString())
        	KeywordUtil.logInfo("Credit card Details file present at ---> "+ccfile.getAbsolutePath())
        	fw_cc_details.close()
        	
           }
        catch(Exception e){
        	KeywordUtil.logInfo(e.toString())
        	}

So , now i get the below output, but i am not able to see the files created in the artifacts tab -

2020-10-20 09:50:54.288 DEBUG t invoice in Billing section of each org - 17: jsonInvoice_details = new groovy.json.JsonBuilder()
2020-10-20 09:50:54.291 DEBUG t invoice in Billing section of each org - 18: jsonInvoice_details.call([“fields”:org_invoice])
2020-10-20 09:50:54.292 DEBUG t invoice in Billing section of each org - 19: jsonCC_details = new groovy.json.JsonBuilder()
2020-10-20 09:50:54.292 DEBUG t invoice in Billing section of each org - 20: jsonCC_details.call([“fields”:org_cc])
2020-10-20 09:50:54.292 DEBUG t invoice in Billing section of each org - 21: invoices_filename = “./report/BillingInfo_Invoices.txt”
2020-10-20 09:50:54.292 DEBUG t invoice in Billing section of each org - 22: cc_filename = “./report/BillingInfo_CC.txt”
2020-10-20 09:50:54.292 DEBUG t invoice in Billing section of each org - 23: try
2020-10-20 09:50:54.293 DEBUG t invoice in Billing section of each org - 1: invoicesfile = new java.io.File(invoices_filename)
2020-10-20 09:50:54.293 DEBUG t invoice in Billing section of each org - 2: ccfile = new java.io.File(cc_filename)
2020-10-20 09:50:54.293 DEBUG t invoice in Billing section of each org - 3: if (!(getParentFile().exists()))
2020-10-20 09:50:54.294 DEBUG t invoice in Billing section of each org - 1: getParentFile().mkdirs()
2020-10-20 09:50:54.295 DEBUG t invoice in Billing section of each org - 4: if (!(invoicesfile.exists()))
2020-10-20 09:50:54.295 DEBUG t invoice in Billing section of each org - 1: invoicesfile.createNewFile()
2020-10-20 09:50:54.295 DEBUG t invoice in Billing section of each org - 5: if (!(ccfile.exists()))
2020-10-20 09:50:54.296 DEBUG t invoice in Billing section of each org - 1: ccfile.createNewFile()
2020-10-20 09:50:54.296 DEBUG t invoice in Billing section of each org - 6: fw_invoice_details = new java.io.FileWriter(invoices_filename)
2020-10-20 09:50:54.299 DEBUG t invoice in Billing section of each org - 7: fw_invoice_details.write(jsonInvoice_details.toString())
2020-10-20 09:50:54.304 DEBUG t invoice in Billing section of each org - 8: logInfo("Invoice Details file present at —> " + invoicesfile.getAbsolutePath())
2020-10-20 09:50:54.304 INFO com.kms.katalon.core.util.KeywordUtil - Invoice Details file present at —> /tmp/katalon_execute/project/./report/BillingInfo_Invoices.txt
2020-10-20 09:50:54.304 DEBUG t invoice in Billing section of each org - 9: fw_invoice_details.close()
2020-10-20 09:50:54.305 DEBUG t invoice in Billing section of each org - 10: fw_cc_details = new java.io.FileWriter(cc_filename)
2020-10-20 09:50:54.305 DEBUG t invoice in Billing section of each org - 11: fw_cc_details.write(jsonCC_details.toString())
2020-10-20 09:50:54.305 DEBUG t invoice in Billing section of each org - 12: logInfo("Credit card Details file present at —> " + ccfile.getAbsolutePath())
2020-10-20 09:50:54.305 INFO com.kms.katalon.core.util.KeywordUtil - Credit card Details file present at —> /tmp/katalon_execute/project/./report/BillingInfo_CC.txt
2020-10-20 09:50:54.306 DEBUG t invoice in Billing section of each org - 13: fw_cc_details.close()
2020-10-20 09:50:54.306 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Intentwise/Billing/Check if there is a CC and past invoice in Billing section of each org
2020-10-20 09:50:54.412 INFO com.kms.katalon.core.util.KeywordUtil - Start generating HTML report folder at: /tmp/katalon_execute/project/Reports/20201020_094923/Billing/20201020_094924…
2020-10-20 09:50:54.527 INFO com.kms.katalon.core.util.KeywordUtil - HTML report generated

I am missing out something here.
Please do let me know your observations.
Thanks in advance.

The following lines are not necessary.

if (!invoicesfile.exists()) {
    invoicesfile.createNewFile()
}
if (!ccfile.exists()) {
    ccfile.createNewFile()
}

The files will be newly created, when not existing, by the FileWriter.write() call.

Do you find the files present in the Linux command line, or Windows Explorer, or Mac Finder?

@kazurayam yes using getAbsolutePath() , i can see file path in Linux command line.

How is the file size? Is it zero? or some meaningful size?

@kazurayam No it is not zero, It has some JSON written into it.

I do not know how the Artifacts tab of Circle CI is designed, I do not see why you can not find the text files in the tab, sorry.

ok @kazurayam thanks for replying.