3
public class im4jav extends test{

public static void main(String[] args) throws IOException {

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=fkKzVqasB8yl8wefrraoCg&gws_rd=ssl");

    File scrFile = ((TakesScreenshot)driver).
    getScreenshotAs(OutputType.FILE);

    String current = "c:/temp/image.png";
    FileUtils.copyFile(scrFile, new File(current));


    boolean compareSuccess = compareImages("c:/temp/expected.png", current, "c:/temp/difference.png");

    System.out.println("run ok ? :"+compareSuccess);

    driver.close();


}}class test{

static boolean compareImages (String exp, String cur, String diff) {
      // This instance wraps the compare command
      CompareCmd compare = new CompareCmd();

      // For metric-output
      compare.setErrorConsumer(StandardStream.STDERR);
      IMOperation cmpOp = new IMOperation();
      // Set the compare metric
      cmpOp.metric("mae");

      // Add the expected image
      cmpOp.addImage(exp);

      // Add the current image
      cmpOp.addImage(cur);

      // This stores the difference
      cmpOp.addImage(diff);

      try {
        // Do the compare
        compare.run(cmpOp);
        return true;
      }
      catch (Exception ex) {
        return false;
      }
    }

}

Hi Team,

In the above code I am trying to take a screenshot of a page and compare it with expected screenshot using im4java however even if the actual and expected images are similar it still throws false and the difference image is also not getting generated

I have added expected screenshot in temp folder.

Can someone please suggest what i am doing wrong. Thanks in advance

Suraj Prasad
  • 241
  • 3
  • 24
  • what does the exception says? use `ex.printStacktrace()` before `return false;` – Yazan Feb 04 '16 at 13:28
  • org.im4java.core.CommandException: java.io.IOException: Cannot run program "compare": CreateProcess error=2, The system cannot find the file specified – Suraj Prasad Feb 04 '16 at 13:53
  • `The system cannot find the file specified` maybe one of the images does not exist. make sure current and expected images do really exist in the given path – Yazan Feb 04 '16 at 14:09
  • I got the same error and both images exist in the path. Does anybody help? – Onur Baskirt Jul 30 '16 at 23:29

1 Answers1

0

I spent too much time to fix the problem. Solution is:

  1. You should fully install ImageMagick with selecting all checkboxes.
  2. You should set its installation path to the environmental variables system path.
  3. Optional: Restart your PC for those settings will work correctly.

With these steps I solved my problems.

Onur Baskirt
  • 336
  • 2
  • 12