How to put time stamp in screenshots

Hi,
When i take screenshot and place in a path , i need to get the time stamp also with it. can anyone help?

1 Like

I wish I knew…I am now trying to find how to put timestamp to Katalon so it is similar issue…

Marek,

what kind of timestamp? Just String variable? It is as easy as long ts = System.currentTimeMillis() / 1000L

Thanks for a tip and quick reaction, that is ± what I needed to create unique names…I’ve tried to find it in Katalon docs and I failed :slight_smile: So next time I just go learn and find some groovy functions :slight_smile:

Hi,

all screenshots are marked with timestamp on the name. The numeric value that names the png is in fact the timestamp in miliseconds.

If you use the function System.currentTimeMillis() it will give you a timestamp in that format.

If you need to convert back, you can use something like this:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY) + “:” + calendar.get(Calendar.MINUTE));

Can anyone help me out in displaying timestamp in the screenshot (not as filename)!!

Thanks in advance.
Sridhar

Please try the following code as a test case. Copy this into your project and run it.

import java.awt.Color
import java.awt.Font
import java.awt.FontMetrics
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

import javax.imageio.ImageIO

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

WebUI.openBrowser('')
WebUI.setViewPortSize(800, 600)
WebUI.navigateToUrl('http://forum.katalon.com/t/how-to-put-time-stamp-in-screenshots/8831/3')
WebUI.waitForPageLoad(10)
WebUI.delay(1)

// get the current timestamp
LocalDateTime now = LocalDateTime.now()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd_HH-mm-ss')

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path tmpDir = projectDir.resolve('tmp')
Files.createDirectories(tmpDir)
Path screenshot = tmpDir.resolve("${now.format(formatter)}.png")

// take screenshot
WebUI.takeScreenshot(screenshot.toString(), FailureHandling.STOP_ON_FAILURE)

WebUI.closeBrowser()

// method to put text onto the original image
BufferedImage modifyImage(BufferedImage original, String text) {
	int w = original.getWidth()
	int h = original.getHeight()
	BufferedImage newImage = new BufferedImage(w, h, original.getType())
	Graphics2D g2d = newImage.createGraphics()
	g2d.drawImage(original, 0, 0, w, h, null)
	g2d.setPaint(Color.RED)
	g2d.setFont(new Font("Serif", Font.BOLD, 20))
	String s = text
	FontMetrics fm = g2d.getFontMetrics()
	int x = newImage.getWidth() - fm.stringWidth(s) - 5
	int y = fm.getHeight()
	g2d.drawString(s, x, y)
	g2d.dispose()
	return newImage
}

// read the screenshot PNG file
BufferedImage bi = ImageIO.read(screenshot.toFile())

// put timestamp on the image; new image will be returned
BufferedImage bi2 = modifyImage(bi, now.format(formatter))

// write the modified image into another file
Path modified = tmpDir.resolve("${now.format(formatter)}.modified.png")
ImageIO.write(bi2, "png", modified.toFile())

This will create 2 PNG files under the <project dir>/tmp directory.

A file with name of yyyy-MM-dd_HH-mm-ss.png is the original screenshot.

A file with name of yyyy-MM-dd_HH-mm-ss.modified.png is an image copied from the original with a timestamp on the top-right corner in red color. The following image shows an example.