Finding Images in Sikuli X Java: A Guide

Finding Images in Sikuli X Java: A Guide

10 November 2024 Stephan Petzl Leave a comment Tech-Help

When working with Sikuli X Java, you may encounter challenges in identifying images on the screen. This guide explores practical solutions to address these issues, particularly when running Appium on an iOS simulator.

Understanding the Problem

You might face issues where the image you are trying to locate cannot be found, resulting in errors like:

      FindFailed: cannot find /path/to/image.jpg in S(0)[0,0 1440x900]
    

This error indicates that the image specified does not match what is on the screen.

Potential Solutions

1. Verify Image Formats

Ensure that the image file format you are using matches the expected format in your code. For instance, if your code is set to look for a .PNG file, but you are providing a .JPG file, it will not be found. Correct the file extension in your path to match the actual file type.

2. Image Verification Method

To effectively verify images, consider using a method that captures and compares images. Here is a practical example:

      @Test
      public void verifyImages() {
          // Take screenshot
          File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

          // Capture the specific image element
          List imageList = driver.findElementsByXPath("//*[@class='android.widget.ImageView' and @index='0']");
          WebElement image = imageList.get(1);
          Point point = image.getLocation();
          int width = image.getSize().getWidth();
          int height = image.getSize().getHeight();

          BufferedImage img = ImageIO.read(screen);
          BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width, height);
          ImageIO.write(dest, "png", screen);
          File file = new File("Menu.png");
          FileUtils.copyFile(screen, file);

          // Verify images
          verifyImage("Menu.png", "Menu.png");
      }

      public void verifyImage(String image1, String image2) throws IOException {
          BufferedImage bufileInput = ImageIO.read(new File(image1));
          BufferedImage bufileOutPut = ImageIO.read(new File(image2));
          boolean matchFlag = true;

          if (bufileInput.getData().getDataBuffer().getSize() == bufileOutPut.getData().getDataBuffer().getSize()) {
              for (int j = 0; j < bufileInput.getData().getDataBuffer().getSize(); j++) {
                  if (bufileInput.getData().getDataBuffer().getElem(j) != bufileOutPut.getData().getDataBuffer().getElem(j)) {
                      matchFlag = false;
                      break;
                  }
              }
          } else {
              matchFlag = false;
          }
          Assert.assertTrue(matchFlag, "Images are not the same");
      }
    

This method involves taking a screenshot, capturing the specific image element, and comparing it with the expected image to verify accuracy.

Conclusion

By ensuring correct file formats and utilizing effective image verification methods, you can overcome the challenges associated with finding images using Sikuli X Java.

Enhance Your Testing with Repeato

For those seeking a more efficient and reliable testing solution, consider using Repeato. As a no-code test automation tool, Repeato leverages computer vision and AI to create and run tests swiftly, providing a stable alternative to traditional methods like Appium. This approach not only reduces complexity but also accelerates the testing process, ensuring high-quality app performance.

Like this article? there’s more where that came from!