Cannot take screenshot with custom keyword

I am trying to take a screenshot at different times of the automated test case.
In order to be able to save them where I want and have every screenshot with a unique name, I created a custom keyword as follows:

package multipleScreenshot
public class naming {
	public void screenshotNaming(String path, String name){
		String completePath = path + name + (int)(Math.random() * 10000)+ ".jpg"
		println WebUI.takeScreenshot(completePath);

I have added the global variables name and path to the default profile:

I am calling the function in the test case as follows:


Unfortunately I keep getting the same error no matter what I have tried. I would really appreciate some help.

05-21-2020 12:53:13 AM Test Cases/First Test Case

Elapsed time: 16.338s



Test Cases/First Test Case FAILED.
com.kms.katalon.core.exception.StepFailedException: Cannot take screenshot
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword.takeScreenshot(TakeScreenshotKeyword.groovy:88)
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword.execute(TakeScreenshotKeyword.groovy:71)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.takeScreenshot(WebUiBuiltInKeywords.groovy:2907)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$takeScreenshot$ Source)
	at multipleScreenshot.naming.screenshotNaming(naming.groovy:48)
	at multipleScreenshot.naming.invokeMethod(naming.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(
	at First Test Test Case:23)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(
	at com.kms.katalon.core.main.TestCaseExecutor.execute(
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$ Source)
Caused by: The filename, directory name, or volume label syntax is incorrect
	at com.kms.katalon.core.webui.util.FileUtil.takesScreenshot(
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword$_takeScreenshot_closure1.doCall(TakeScreenshotKeyword.groovy:89)
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword$
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
	... 20 more

file path can not contain \r\n characters.

Thank you very much, I’ve noticed that too after a while. Somehow it was automatically added.
Any idea how to use a time stamp instead of the random number to name the screenshot?


package my

import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

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

public class NamingUtils {

	// My Time Zone
	// See
	// Choose appropriate ZoneOffset for you.
	private static ZoneOffset myZoneOffset = ZoneOffset.of("+0900")

	// My Date Time Formatter
	private static DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("yyyyMMdd-hhmmss")

	public static String makeScreenshotPath(Path baseDir, String name){
		// date time of now
		LocalDateTime now =
		String dateTime = dtFormatter.format(now)
		Path p = baseDir.resolve(name + "_" + dateTime + ".png")
		return p.toString()

Sample Test Case which uses my.NamingUtils.makeScreenshotPath Keyword:

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

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

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path baseDir = projectDir.resolve("tmp")

String screenshotPath = CustomKeywords."my.NamingUtils.makeScreenshotPath"(baseDir, "home")


When I ran the testcase, it created a PNG file of


My “Materials” library provides more sophisticated solution for locating screenshot files.

Though Materials library could be too much for simple test cases.

Thank you very much. I am pretty new to Katalon Studio and Groovy so I still have to learn a lot. But I am determined to understand better.
I have tried to use your method but I keep getting this error

No signature of method: my.NamingUtils.makeScreenshotPath() is applicable for argument types: (java.lang.String) values: [C:\Users\Oana\Katalon Studio\Bachelor Thesis\tmp\home_20200523-074820.png]
Possible solutions: makeScreenshotPath(java.nio.file.Path, java.lang.String)

You are not careful enough about the type of argument passed to the keyword: not java.lang.String, but java.nio.file.Path. Read the sample code thoroughly.

Possibly you should set back yourself and learn Java 8 programming with some good books before spending hours on my sample code. You should learn java.nio.files.* and java.time.* packages. Path API was introduced at Java 7 in 2011. java.time package was introduced at Java 8 in 2014. Older books that lack java.nio.files.* and java.time.* coverage would not help.

I was just curious to see your result.
I added the dateTime to the path I was using and I was able to save with time stamp.
Thank you again for your advice :slight_smile:
public class naming {
private static ZoneOffset myZoneOffset = ZoneOffset.of("+0200")
private static DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern(“”)

	public void screenshotNaming(String path, String name){
		LocalDateTime now =
		String dateTime = dtFormatter.format(now)
		String completePath = path + name + dateTime + ".jpg"

		println WebUI.takeScreenshot(completePath);

Does the screenshotName() method work ok or not? Are you asking me anything more?

String completePath = path + name + dateTime + ".jpg"

println WebUI.takeScreenshot(completePath);

I am curious. WebUI.takeScreenthos() keyword create a PNG file, not a JPEG file. Why do you name the file as .jpg instead of .png?

Everything works, thank you :slight_smile:

Good point, I did’t actually consider that.