Java Code Examples for org.sikuli.basics.Settings#NewAPI

The following examples show how to use org.sikuli.basics.Settings#NewAPI . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Region.java    From SikuliX1 with MIT License 6 votes vote down vote up
/**
 * waits until target vanishes or timeout (in seconds) is passed
 *
 * @param <PSI>   Pattern, String or Image
 * @param target  Pattern, String or Image
 * @param timeout time in seconds
 * @return true if target vanishes, false otherwise and if imagefile is missing.
 */
public <PSI> boolean waitVanish(PSI target, double timeout) {
  if (Settings.NewAPI) { //TODO waitVanish()
    return super.waitVanish(target, timeout);
  }
  RepeatableVanish rv = new RepeatableVanish(target);
  Image img = rv._image;
  String targetStr = img.getName();
  Boolean response = true;
  if (!img.isValid()) {
    response = handleImageMissing(img, false);//waitVanish
  }
  if (null != response && response) {
    log(logLevel, "waiting for " + targetStr + " to vanish within %.1f secs", timeout);
    if (rv.repeat(timeout)) {
      log(logLevel, "%s vanished", targetStr);
      return true;
    }
    log(logLevel, "%s did not vanish before timeout", targetStr);
    return false;
  }
  return false;
}
 
Example 2
Source File: ElementRegionOldTest.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Test
public void test200_RegionFind() {
  testIntro(testBase);
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Settings.NewAPI = false;
  Region reg = getDefaultRegion();
  Match match = null;
  try {
    match = reg.find(testName);
  } catch (FindFailed findFailed) {
  }
  if (null != match) {
    if (showImage) {
      match.highlight(2);
    }
  }
  testOutro("%s in %s is %s", testName, reg, match);
  Assert.assertTrue("Not found!", checkMatch(match, 0.95));
}
 
Example 3
Source File: ElementRegionOldTest.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Test
public void test210_RegionFindTrans() {
  testIntro(testBase);
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Settings.NewAPI = false;
  Region reg = getDefaultRegion();
  Match match = null;
  try {
    match = reg.find(testNameTrans);
  } catch (FindFailed findFailed) {
  }
  if (null != match) {
    if (showImage) {
      match.highlight(2);
    }
  }
  testOutro("%s in %s is %s", testName, reg, match);
  Assert.assertTrue("Not found!", checkMatch(match, 0.95));
}
 
Example 4
Source File: ElementRegionOldTest.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Test
public void test211_RegionFindMask() {
  testIntro(testBase);
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Settings.NewAPI = false;
  Region reg = getDefaultRegion();
  Match match = null;
  try {
    Image imageMasked = new Image(testName).mask(testNameMask);
    match = reg.find(imageMasked);
  } catch (FindFailed findFailed) {
  }
  if (null != match) {
    if (showImage) {
      match.highlight(2);
    }
  }
  testOutro("%s in %s is %s", testName, reg, match);
  Assert.assertTrue("Not found!", checkMatch(match, 0.95));
}
 
Example 5
Source File: ElementRegionOldTest.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Test
public void test220_RegionFindAll() {
  testIntro(testBase);
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Settings.NewAPI = false;
  Region reg = getDefaultRegion();
  Iterator<Match> matches = null;
  try {
    matches = reg.findAll(testName);
  } catch (FindFailed findFailed) {
  }
  Match match = null;
  int matchCount = 0;
  while (matches.hasNext()) {
    matchCount++;
    match = matches.next();
    if (showImage) {
      match.highlight();
    }
  }
  if (showImage) {
    Highlight.closeAll(3);
  }
  testOutro("%s in %s is %s (%d)", testName, reg, match, matchCount);
  Assert.assertNotNull("Not Found!", match);
}
 
Example 6
Source File: Region.java    From SikuliX1 with MIT License 5 votes vote down vote up
/**
 * Check if target exists with a specified timeout.
 * <br>
 * timout = 0: returns immediately after first search,
 * does not raise FindFailed
 *
 * @param <PSI>   Pattern, String or Image
 * @param target  The target to search for
 * @param timeout Timeout in seconds
 * @return the match (null if not found or image file missing)
 */
public <PSI> Match exists(PSI target, double timeout) {
  if (Settings.NewAPI) { //TODO exists()
    return super.exists(target, timeout);
  }
  lastMatch = null;
  RepeatableFind rf = new RepeatableFind(target, null);
  Image img = rf._image;
  Boolean response = true;
  if (!img.isText() && !img.isValid()) {
    response = handleImageMissing(img, false);//exists
    if (response == null) {
      if (Settings.SwitchToText) {
        log(logLevel, "Exists: image missing: switching to text search (deprecated - use text methods)");
        response = true;
        img.asText(true);
        rf.setTarget("\t" + target + "\t");
      } else {
        throw new RuntimeException(String.format("SikuliX: exists: ImageMissing: %s", target));
      }
    }
  }
  String targetStr = img.getName();
  log(logLevel, "exists: waiting %.1f secs for %s to appear in %s", timeout, targetStr, this.toStringShort());
  if (rf.repeat(timeout)) {
    lastMatch = rf.getMatch();
    //lastMatch.setImage(img);
    if (isOtherScreen()) {
      lastMatch.setOtherScreenOf(this);
    } else if (img != null) {
      img.setLastSeen(lastMatch.getRect(), lastMatch.score());
    }
    log(logLevel, "exists: %s has appeared (%s)", targetStr, lastMatch);
    return lastMatch;
  }
  log(logLevel, "exists: %s did not appear [%d msec]", targetStr, new Date().getTime() - lastFindTime);
  return null;
}
 
Example 7
Source File: Region.java    From SikuliX1 with MIT License 5 votes vote down vote up
/**
   * finds all occurences of the given Pattern, String or Image in the region and returns an Iterator of Matches.
   *
   * @param <PSI>  Pattern, String or Image
   * @param target A search criteria
   * @return All elements matching
   * @throws FindFailed if the Find operation failed
   */
  public <PSI> Matches findAll(PSI target) throws FindFailed {
//  public <PSI> Iterator<Match> findAll(PSI target) throws FindFailed {
    if (Settings.NewAPI) { //TODO findAll()
      return super.findAll(target);
    }
    lastMatches = null;
    RepeatableFindAll rf = new RepeatableFindAll(target, null);
    Image img = rf._image;
    String targetStr = img.getName();
    Boolean response = true;
    if (!img.isValid()) {
      response = handleImageMissing(img, false);//findAll
      if (response == null) {
        throw new RuntimeException(String.format("SikuliX: findAll: ImageMissing: %s", target));
      }
    }
    while (null != response && response) {
      log(logLevel, "findAll: waiting %.1f secs for (multiple) %s to appear in %s",
              getAutoWaitTimeout(), targetStr, this.toStringShort());
      if (getAutoWaitTimeout() > 0) {
        rf.repeat(getAutoWaitTimeout());
        lastMatches = rf.getMatches();
      } else {
        lastMatches = doFindAll(target, null);
      }
      if (lastMatches != null) {
        log(logLevel, "findAll: %s has appeared", targetStr);
        break;
      } else {
        log(logLevel, "findAll: %s did not appear", targetStr);
        response = handleFindFailed(target, img);
      }
    }
    if (null == response) {
      throw new FindFailed(FindFailed.createErrorMessage(this, img));
    }
    return lastMatches;
  }
 
Example 8
Source File: Region.java    From SikuliX1 with MIT License 5 votes vote down vote up
public List<Match> findAny(Object... args) {
  if (Settings.NewAPI) { //TODO findAny(Object...)
    return super.findAny(args);
  }
  if (args.length == 0) {
    return new ArrayList<Match>();
  }
  List<Object> pList = new ArrayList<>();
  pList.addAll(Arrays.asList(args));
  return findAnyList(pList);
}
 
Example 9
Source File: Region.java    From SikuliX1 with MIT License 5 votes vote down vote up
public List<Match> findAnyList(List<Object> pList) {
  if (Settings.NewAPI) { //TODO findAny(List)
    return super.findAnyList(pList);
  }
  Debug.log(logLevel, "findAny: enter");
  if (pList == null || pList.size() == 0) {
    return new ArrayList<Match>();
  }
  List<Match> mList = findAnyCollect(pList);
  return mList;
}
 
Example 10
Source File: SXTest.java    From SikuliX1 with MIT License 5 votes vote down vote up
public void setUpBase() {
  Settings.NewAPI = true;
  Settings.AlwaysResize = 1;
  Settings.CheckLastSeen = false;
  if (null == bundlePath) {
    setUpTime = new Date().getTime();
    File bundleFile = new File(runTime.fWorkDir, "src/main/resources/images");
    ImagePath.setBundleFolder(bundleFile);
    bundlePath = ImagePath.getBundlePath();
    setUpTime = new Date().getTime() - setUpTime;
  }
}
 
Example 11
Source File: Region.java    From SikuliX1 with MIT License 4 votes vote down vote up
/**
 * Waits for the Pattern, String or Image to appear or timeout (in second) is passed
 *
 * @param <PSI>   Pattern, String or Image
 * @param target  The target to search for
 * @param timeout Timeout in seconds
 * @return The found Match
 * @throws FindFailed if the Find operation finally failed
 */
public <PSI> Match wait(PSI target, double timeout) throws FindFailed {
  if (Settings.NewAPI) { // TODO wait()
    return super.wait(target, timeout);
  }
  lastMatch = null;
  String shouldAbort = "";
  RepeatableFind rf = new RepeatableFind(target, null);
  Image img = rf._image;
  String targetStr = img.getName();
  Boolean response = true;
  if (!img.isText() && !img.isValid()) {
    response = handleImageMissing(img, false); //wait
    if (response == null) {
      if (Settings.SwitchToText) {
        log(logLevel, "wait: image missing: switching to text search (deprecated - use text methods)");
        response = true;
        img.asText(true);
        rf.setTarget("\t" + target + "\t");
      } else {
        throw new RuntimeException(String.format("SikuliX: wait: ImageMissing: %s", target));
      }
    }
  }
  while (null != response && response) {
    log(logLevel, "wait: waiting %.1f secs for %s to appear in %s", timeout, targetStr, this.toStringShort());
    if (rf.repeat(timeout)) {
      lastMatch = rf.getMatch();
      //lastMatch.setImage(img);
      if (isOtherScreen()) {
        lastMatch.setOtherScreenOf(this);
      } else if (img != null) {
        img.setLastSeen(lastMatch.getRect(), lastMatch.score());
      }
      log(logLevel, "wait: %s appeared (%s)", img.getName(), lastMatch);
      return lastMatch;
    } else {
      response = handleFindFailed(target, img);
      if (null == response) {
        shouldAbort = FindFailed.createErrorMessage(this, img);
        break;
      } else if (response) {
        if (img.isRecaptured()) {
          rf = new RepeatableFind(target, img);
        }
        continue;
      }
      break;
    }
  }
  log(logLevel, "wait: %s did not appear [%d msec]", targetStr, new Date().getTime() - lastFindTime);
  if (!shouldAbort.isEmpty()) {
    throw new FindFailed(shouldAbort);
  }
  return lastMatch;
}
 
Example 12
Source File: Region.java    From SikuliX1 with MIT License 4 votes vote down vote up
/**
 * finds the given Pattern, String or Image in the region and returns the best match.
 *
 * @param <PSI>  Pattern, String or Image
 * @param target what (PSI) to find
 * @return If found, the element. null otherwise
 * @throws FindFailed if the Find operation failed
 */
public <PSI> Match find(PSI target) throws FindFailed {
  if (Settings.NewAPI) { //TODO find()
    return super.find(target);
  }
  Image img = Element.getImage(target);
  lastMatch = null;
  Boolean response = true;
  if (!img.isText() && !img.isValid()) {
    response = handleImageMissing(img, false); //find()
    if (response == null) {
      if (Settings.SwitchToText) {
        log(logLevel, "find: image missing: switching to text search (deprecated - use text methods)");
        response = true;
        img.asText(true);
        target = (PSI) ("\t" + target + "\t");
      } else {
        throw new RuntimeException(String.format("SikuliX: find: ImageMissing: %s", target));
      }
    }
  }
  String targetStr = img.getName();
  while (null != response && response) {
    log(logLevel, "find: waiting 0 secs for %s to appear in %s", targetStr, this.toStringShort());
    lastMatch = doFind(target, img, null);
    if (lastMatch != null) {
      if (isOtherScreen()) {
        lastMatch.setOtherScreenOf(this);
      } else if (img != null) {
        img.setLastSeen(lastMatch.getRect(), lastMatch.score());
      }
      log(logLevel, "find: %s appeared (%s)", targetStr, lastMatch);
      break;
    }
    log(logLevel, "find: %s did not appear [%d msec]", targetStr, new Date().getTime() - lastFindTime);
    if (null == lastMatch) {
      response = handleFindFailed(target, img);
    }
  }
  if (null == response) {
    throw new FindFailed(FindFailed.createErrorMessage(this, img));
  }
  return lastMatch;
}