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 {
@Keyword
	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:

CustomKeywords.'multipleScreenshot.naming.screenshotNaming'(GlobalVariable.path, GlobalVariable.name)

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

multipleScreenshot.naming.screenshotNaming:48

multipleScreenshot.naming.invokeMethod:0

Test Cases/First Test Case FAILED.
Reason:
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$1.call(Unknown Source)
	at multipleScreenshot.naming.screenshotNaming(naming.groovy:48)
	at multipleScreenshot.naming.invokeMethod(naming.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
	at First Test Case.run(First Test Case:23)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1590015190727.run(TempTestCase1590015190727.groovy:25)
Caused by: java.io.IOException: The filename, directory name, or volume label syntax is incorrect
	at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1079)
	at com.kms.katalon.core.webui.util.FileUtil.takesScreenshot(FileUtil.java:31)
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword$_takeScreenshot_closure1.doCall(TakeScreenshotKeyword.groovy:89)
	at com.kms.katalon.core.webui.keyword.builtin.TakeScreenshotKeyword$_takeScreenshot_closure1.call(TakeScreenshotKeyword.groovy)
	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?

Keyword:

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 https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
	// Choose appropriate ZoneOffset for you.
	private static ZoneOffset myZoneOffset = ZoneOffset.of("+0900")

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

	@Keyword
	public static String makeScreenshotPath(Path baseDir, String name){
		// date time of now
		LocalDateTime now = LocalDateTime.now(myZoneOffset)
		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")
Files.createDirectories(baseDir)

String screenshotPath = CustomKeywords."my.NamingUtils.makeScreenshotPath"(baseDir, "home")
WebUI.comment("screenshotPath=${screenshotPath}")

WebUI.openBrowser("http://demoaut.katalon.com")
WebUI.takeScreenshot(screenshotPath)
WebUI.closeBrowser()

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

<projectDir>/tmp/home_20200523-065803.png.

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(“dd.MM.yyyy-hh.mm.ss”)

	@Keyword
	public void screenshotNaming(String path, String name){
		LocalDateTime now = LocalDateTime.now(myZoneOffset)
		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.