[Java] Junit: How to avoid false positive using App.focus

Asked by Vince

I'm using Sikuli in Eclipse with Java. I've been able to get the App.focus function to work great but every once in a while it doesn't work. I created a function to check that the focus worked but I'm getting false positives. The function reports back that the window was correctly focused but nothing happens on the screen.

Any suggestions on how to avoid the false positive where the window is not focused but reports back that it was?

My code:

  public static boolean ensureFocus(String windowName) throws Exception {
    int numberOfTries = 1;
    boolean pass = false;

    do {
      App focusResult = App.focus(windowName);
      if (focusResult != null) {
        pass = true;
      }
      if (pass) {
        System.out.println("Check number " + numberOfTries + " passed focusing on " + windowName);
        return true;
      }
      else {
        System.out.println("Check number " + numberOfTries + " failed trying to focus on "
          + windowName);
      }
      numberOfTries++;
    }
    while (numberOfTries < 4);

    return false;
  }

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

No chance: App.focus() does not check, what happens with the window.

You have to somehow check yourself, wether the wanted window is visible/frontmost.

Revision history for this message
Vince (melekal) said :
#2

But isn't that the whole point of having a function to focus on a window?

Why even have that if it doesn't bring it to the forefront of other windows?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Yes of course, it tries to do that, but it does not check wether this really happens.

So, why does it not work in the cases you have?

Revision history for this message
Vince (melekal) said :
#4

It does work about 75% of the time.

However, sometimes it will respond as successful but the window is not brought forward. I suspect it may be due to a timing issue where the window isn't fully loaded which is why I was trying to have it check multiple times. With the false positive issue that doesn't work. The best I can do is try to give it a long wait each time and hope that makes a difference.

There have been times though that the window has been up a long time and still the same thing occurs. I have no idea why it happens, I just want to figure out a way to have it check a couple times to get past it.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Sorry, I fully understand your situation. But there is currently no easy solution for that.

You might use App.window() to get the windows region and then wait for visual key elements (window buttons, app icon, frame elements, other fixed window content) to get visible at it's place inside the window.

Can you help with this problem?

Provide an answer of your own, or ask Vince for more information if necessary.

To post a message you must log in.