CompareTo search coordinates broken?

Help
Faizal
2010-02-01
2013-02-18
  • Faizal

    Faizal - 2010-02-01

    Hello, I have not been able to find sufficient documentation on obtaining the search X and Y coordinates of a compareTo while using Java.  When converting a script to Java it does not convert this syntax properly (the resulting code doesn't compile).

    When using build 20090807 I was able to do a context.getVariable(_SEARCH_X) and that seemed to work.  The following also returned the correct Point: context.getVariable(context.CONTEXT_IMAGE_SEARCH_POINT_LIST).

    However, using 20100120 the same calls always return NULL.  Please help!

     
  • Robert Pes

    Robert Pes - 2010-02-01

    Both ways you mention are correct. The image search algorithm saves the coordinates both to the variable table and to a list of Point instances in the context. The context also provides a convenience method getSearchHits() accessing this list.

    I created a short example and retested the functionality. It works as expected. Test method:

    public void test() {
          try {
             int result = compareTo(new File { new File("cmptest2.png") }, "search");
             System.out.println("Result: "+result+"\nCoordinates: "+getContext().getSearchHits()
             +"\n = ");
          } catch (IOException ex) {
             ex.printStackTrace();
          }
       }

    Console output:

    Result: 0
    Coordinates: [java.awt.Point]
    =

    The list and variables are null only when the search fails which is indicated by return value of the compareTo() method. Are you testing it?

     
  • Faizal

    Faizal - 2010-02-01

    Thanks for the prompt reply, Robert.  Below is my code.

            File templateDir = new File("images/MacOS");
            getContext.setTemplateDir(templateDir);
            File compareFile = new File("images" + "/" + "MacOS" + "/" + "SpotlightSearch.png");
            System.out.println(compareFile.getAbsolutePath());
            int result = 0;
            if(compareFile.exists() && compareFile.isFile()) {
                result = compareTo(new File[] { compareFile }, SEARCH_IMAGE_COMPARISON);
            } else {
                System.err.println("screenshot file missing!");
                System.exit(-1);
            }
            if ( context.getExitCode() > 0 || result > 0) {
                System.out.println("Comparison Result: " + getContext().getComparisonResult() + " result: " + result);
                screenshot(new File("failure.png"), "Failed to find SpotlightSearch.png");
                exit(1);
            }
            
            System.out.println("SEARCH_X: " + getContext().getVariable("_SEARCH_X") + " SEARCH_Y: " + getContext().getVariable("_SEARCH_Y"));
    

    Here is the output from the older version..

    T-Plan Robot v2.0.Beta (Build No. 2.0.Beta-20090806.1) - automated testing tool based on remote desktop technologies.
    Copyright (C) 2009 T-Plan Ltd, http://www.t-plan.co.uk/. All rights reserved.
    Startup timeout set to 15 seconds, starting to count down...
    Execution is starting.
    C:\Documents and Settings\faizal\images\MacOS\SpotlightSearch.png
    SEARCH_X: 1239 SEARCH_Y: 1
    Execution finished, exit code is PASS (0)
    

    Here is the output of the running the exact same class with the latest robot.jar..

    T-Plan Robot v2.0.2 (Build No. 2.0.2-20100120.1) - automated testing tool based on remote desktop technologies.
    Copyright (C) 2009 T-Plan Ltd, http://www.t-plan.co.uk/. All rights reserved.
    Startup timeout set to 15 seconds, starting to count down...
    Execution is starting.
    C:\Documents and Settings\faizal\images\MacOS\SpotlightSearch.png
    Comparison Result: null result: 2
    SEARCH_X: null SEARCH_Y: null
    Execution finished, exit code is FAIL (1)
    
     
  • Robert Pes

    Robert Pes - 2010-02-01

    I see multiple problems in your script. In fact I am surprised it even runs because Java should refuse to compile it.

    Line 2: getContext.setTemplateDir(templateDir) should be getContext().setTemplateDir(templateDir)

    Line 12: context.getExitCode() must be getContext().getExitCode() because variable context is private (I guess this was changed during Beta).

    And the root cause of your problem: the command returns 2 which according to the spec means "failed to open template file". Looking at the code, you are setting the template path to "images/MacOS". That is fine. Then you specify the template file as "images" + "/" + "MacOS" + "/" + "SpotlightSearch.png". Regardless of what the absolute path returned by File.getAbsolutePath() says, this is a relative file and exactly according to the spec it is resolved against the template dir. That's why I suppose the command in fact looks for "images/MacOS/images/MacOS/SpotlightSearch.png". The fact that it worked in 2.0Beta is incorrect behavior which is probably caused by bug 2886394.

    Last but not least your template path is relative. I wouldn't recommend it because the specification doesn't solve such a situation. So, it may have been resolved against the user home folder in the past but there's no conflict with the spec if it doesn't work right now. I would suggest you to set it using absolute path - in your case it may be easily done as:

    getContext().setTemplateDir(new File(System.getProperty("user.home")+File.separator+"images"+File.separator+"MacOS"));
    
     
  • Faizal

    Faizal - 2010-02-01

    Thanks!  The code syntax wasn't exact because I was trying to remove a bunch of logging before I pasted it in here.  The good news is that changing the paths fixed the matching.  However your reply gave me two questions:

    1) do I need to call getContext() every time I access it or can I just assign to a local variable "context" which I access whenever I need to call getVariable?
    2) how can I use a relative path for the template directory?  right now the "." path is resolving to my home directory, not where the class is being executed from.  I want to be able to run my automated client from various machines so I cannot hardcode the path.  Ideally I would bundle all the template images in to an executable jar file with the test class.  Is this not possible?

     
  • Robert Pes

    Robert Pes - 2010-02-01

    ad 1) Context remains the same for the whole script execution. So yes, you may use a local variable.

    ad 2) My point was that resolution of relative template path against the user home folder is not a feature of T-Plan Robot. It is actually Java behavior that a File instance initialized by relative path gets resolved this way. As long as Java keeps this it will work fine.

    If you want to bundle images with the test class I recommend you to take advantage of the Class facilities. Simply put images to the same package as your test script and load them as:

    BufferedImage img = ImageIO.read(YourScript.class.getResourceAsStream("imagefilename"));
    

    This will work from a JAR file as well. The Java test script API then allows you to call the CompareTo command with Image arguments. The only issue is that the corresponding method accepts image comparison module instances (not the method name) but you can get it simply through the ImageComparisonModuleFactory class.

     
  • Faizal

    Faizal - 2010-02-04

    Thanks again for the help.  I have two more questions..

    1) Is there a way to know the template directory is not found before calling the compareTo method and checking the result?
    2) That's great that the CompareTo command accepts Image arguments, but I don't see the same support for the WaitFor command in DefaultJavaTestScript.  Would you recommend I simulate WaitFor by calling CompareTo in a loop with a timeout?

     
  • Robert Pes

    Robert Pes - 2010-02-04

    1) Is there a way to know the template directory is not found before calling the compareTo method and checking the result?

    Not directly. You may however take advantage of the Exec command to run an OS command listing the directory and then check its return value.

    2) That's great that the CompareTo command accepts Image arguments, but I don't see the same support for the WaitFor command in DefaultJavaTestScript. Would you recommend I simulate WaitFor by calling CompareTo in a loop with a timeout?

    Calling compareTo() in a loop is possible but somewhat slower because it performs analysis of the template image on each call (while waitFor() does it just once). The waitForMatch() method should support Image array as an argument. The same applies to screenshot() which also supports image comparison.

    I filed this as a bug number 2946076. It is likely to be fixed in 2.0.3.

     
  • Faizal

    Faizal - 2010-02-04

    Great, thanks!  Looking forward to 2.0.3. ;)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks