Katalon: Custom keyword working when executing on Chrome but not on Firefox

I have this custom keyword that waits until a file is downloaded and then deletes the file, the thing is that in chrome works okay but when I execute in firefox I get an error.

 @Keyword
public verifyDownloadedFile2(String downloadPath, String fileName, timeOut) {
int counter = 0
boolean fileExist = new File(downloadPath + fileName).exists()
        
while(!fileExist == true) {
    if(counter != timeOut) {
        WebUI.delay(1)
        fileExist = new File(downloadPath + fileName).exists()
        counter++
    }
    else if(counter == timeOut){
        throw new Exception("File was not downloaded within" + timeOut + " seconds.")
    }   
}

File dir = new File(downloadPath);
File[] dirContents = dir.listFiles();

if (dirContents.length > 0) {
    for (int i = 0; i < dirContents.length; i++) {
        dirContents[i].delete();
    }
}

assert fileExist == true
}

Error:

=============== ROOT CAUSE ===================== Caused by: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: windows.interactions.deleteFile2() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.Integer) values: [F:\Downloads\Katalon_Studio_Windows_64-8.1.0\Data\Temp, …]

I made a copy of your keyword class as this:

package mauri

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


public class Util {

	@Keyword
	public verifyDownloadedFile2(String downloadPath, String fileName, timeOut) {
		File file = new File(downloadPath + fileName)
		println "file=" + file.toString()
		
		int counter = 0
		boolean fileExist = file.exists()
		while(!fileExist == true) {
			if(counter != timeOut) {
				WebUI.delay(1)
				fileExist = file.exists()
				counter++
			}
			else if(counter == timeOut){
				throw new Exception("File was not downloaded within" + timeOut + " seconds.")
			}
		}

		File dir = new File(downloadPath);
		File[] dirContents = dir.listFiles();

		if (dirContents.length > 0) {
			for (int i = 0; i < dirContents.length; i++) {
				dirContents[i].delete();
			}
		}

		assert fileExist == true
	}
}

I changed just 2 lines to make a variable of File file.

I made a Test Case to execute the keyword:

CustomKeywords."mauri.Util.verifyDownloadedFile2"("tmp", "a.txt", 10)

When I execute this Test Case, it failed with the following message.

2022-02-09 14:59:38.031 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-02-09 14:59:38.035 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/mauri
file=tmpa.txt
2022-02-09 14:59:48.818 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ java.lang.Exception: File was not downloaded within10 seconds.
2022-02-09 14:59:48.823 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/mauri FAILED.
Reason:
com.kms.katalon.core.exception.StepErrorException: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.Exception: File was not downloaded within10 seconds.
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.throwError(CustomKeywordDelegatingMetaClass.java:96)
	

Please find the following output:

file=tmpa.txt

This is too bad. I have a file “tmp/a.txt” on my Mac (or “tmp\a.txt” on Windows). There is no file named tmpa.txt. So, it is rational that the test fails.

Your code has a mistake at

... new File(downloadPath + fileName)

This code would never work properly.

Do you want some solution? Then, read this article

In there you will find a sample code:

Path path2 = Paths.get("baeldung").resolve("tutorial.txt");

This is what you need.

You shouldn’t use java.io.File class any longer. Prefer java.nio.file.Path.

I will let you two discuss the File variable, but could it be that the error is passed to the “missingMethod…” because it’s wanting a data type to your “timeOut” variable, if it uses that?

public verifyDownloadedFile2(String downloadPath, String fileName, int timeOut) {

Hi thanks for noticing this!

I fixed it and now it doesn’t generate an error, but when running on firefox some files are deleted after verifying them and some are not, not sure what is happening.

Hey Thanks for the help but I wasn’t able to use “Paths”, sorry I am new to programming and I am not sure why it is not working. Thanks anyways!

you need to write appropriate import statements for Paths.

See https://www.baeldung.com/java-nio-2-path for examples.