How do I verify the presence of an image?

Hi There,

In the end anyone has a solution to this error?
I’m able to click on the webelement but not to verify the image linked. Also someone know what the verifyImage will verify? Only the corresponding name or the colors and size?

[INFO] - Clicking on object: ‘Object Repository/xxxxxxxx/main-footer-client-logo’

09-21-2018 10:56:18 AM - [PASSED] - Object: ‘Object Repository/xxxxxxxx/main-footer-client-logo’ is clicked on

09-21-2018 10:56:18 AM - [END] - End action : click

09-21-2018 10:56:18 AM - [START] - Start action : verifyImagePresent

09-21-2018 10:56:18 AM - [INFO] - Finding Test Object with id ‘Object Repository/xxxxxxxx/main-footer-client-logo’

09-21-2018 10:56:18 AM - [INFO] - Checking object

09-21-2018 10:56:18 AM - [INFO] - Waiting for image: ‘C:\Users\david\Desktop\Katalon\cropped-favicon-32x32.png’ present

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See SLF4J Error Codes for further details.

09-21-2018 10:56:20 AM - [FAILED] - Image: ‘C:\Users\david\Desktop\Katalon\cropped-favicon-32x32.png’ is NOT present

09-21-2018 10:56:20 AM - [FAILED] - Unable to verify image present (Root cause: Image: ‘C:\Users\david\Desktop\Katalon\cropped-favicon-32x32.png’ is NOT present)

09-21-2018 10:56:20 AM - [END] - End action : verifyImagePresent

Thanks for the help

verifyImage.PNG

I was able to get this working when I supplied the image. However, it is still very unstable. It appears to pass when I have the browser on top, but even then my mouse may need to be over the top of it and I’m still getting failures. I can’t consistently get it to pass even on the same page with no change and using the same session. Therefore, this function is still useless, because we can’t trust it to run on its own and not fail randomly.

We had been downloading images and checking their MD5 checksums to verify that they are as expected, but that is dependent on knowing where the file will download to, so will only run on Chrome and on certain machines. We were hoping this function would work, but for the time being we may investigate doing our own image validation rather than using Katalon’s built-in functionality.

Capture.PNG

I think I found the issue in Katalon. They somehow removed the function to get the Y location of a TestObject. I assume it used to exist, because they have a function WebUI.getElementLeftPosition(), which is necessary for a more efficient image search. However, I implemented my own function to handle this whole workflow. There are external libraries that will give you more options, such as partial matches, but I just want an exact match to a subimage. Providing a TestObject as expected by Katalon (see previous comment) is supported and completed 6 seconds faster on my machine (1.5 seconds instead of 7.5). This function assume that the image to verify will be in the viewport, but I may add a step to scroll to the object if we end up needing it for stability.

@Keyword

def verifyImagePresent(TestObject imageObject) {

String imagePath = imageObject.getImagePath()

WebElement imageElement = getWebElementFromTestObject(imageObject)

Point location = imageElement.getLocation()

int offsetX = location.getX()

int offsetY = location.getY()

return verifyImagePresent(imagePath, offsetX, offsetY)

}

@Keyword

def verifyImagePresent(String imagePath) {

return verifyImagePresent(imagePath, 0, 0)

}

@Keyword

def verifyImagePresent(String imagePath, int offsetX, int offsetY) {

boolean matches = false

String screenshotPath = FileUtils.getAbsoluteFilePath(‘screenshot.jpg’)

String expectedPath = FileUtils.getAbsoluteFilePath(imagePath)

WebUI.takeScreenshot(screenshotPath)

BufferedImage img, subImage

try{

img = ImageIO.read(new File(screenshotPath))

} catch (IOException ioe) {

System.out.println(“Unable to open file: $screenshotPath”)

}

int imgWidth = img.getWidth()

int imgHeight = img.getHeight()

try{

subImage = ImageIO.read(new File(expectedPath))

} catch (IOException ioe) {

System.out.println(“Unable to open file: $expectedPath”)

}

int subWidth = subImage.getWidth()

int subHeight = subImage.getHeight()

for (int x=offsetX; x<=(imgWidth-subWidth); x++) {

for (int y=offsetY; y<=(imgHeight-subHeight); y++){

BufferedImage cmpImage = img.getSubimage(x, y, subWidth, subHeight)

if (bufferedImagesEqual(subImage, cmpImage)) {

matches = true

break

}

}

}

FileUtils.deleteFile(screenshotPath)

assert matches : “The images do not match”

return matches

}

private boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {

if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {

for (int x = 0; x < img1.getWidth(); x++) {

for (int y = 0; y < img1.getHeight(); y++) {

if (img1.getRGB(x, y) != img2.getRGB(x, y))

return false

}

}

} else {

return false

}

return true

}

@Keyword

public WebElement getWebElementFromTestObject(TestObject testObject) {

String xpath = getXPath(testObject);

return new WebDriverWait(DriverFactory.getWebDriver(), GlobalVariable.short_loading)

.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)))

}

What is this globalvariable ‘short_loading’? I am getting error at this statement.

It would be helpful if you could include the corresponding import statements as well.

Thanks

Hi, short_loading is one of our global variables. Just replace it with your own timeout variable/value.

Hi everyone, all the information gave in this thread are really interesting. I have the same issue as you did and I want to use your method @Caleb_Wood but, as I am very new to Katalon, I am not sure on how to proceed…
Keywords>New>Keyword? Then which name should I pick for the class? Is a package needed?
In advance, Merci for your help
Regards
AC

This work if your browser is opened but wont work if you running via Scheduled ConsoleMode Batch file. I can use verify the element present or validate the ‘ALT’ attribute of the image but that is altogether different comparison.

Any solution for verifyImage in consolemode via scheduled batch file?

i want to user this one ,but failed ,even i use ctrl+shift+o
String xpath = getXPath(testObject);
which package i can import