rainmanwy / robotframework-SikuliLibrary

Sikuli Robot Framework Library provide keywords for Robot Framework to test UI through Sikuli.
Apache License 2.0
144 stars 61 forks source link

Error sikulilib.exceptions.ScreenOperatioException: Paste text failed #170

Closed matteoloduca closed 2 years ago

matteoloduca commented 2 years ago

Hello guys

We have tried to use the keyword "Paste Text image text" (with Robot Framework 4.1.2 and SikuliLibrary 2.0.0) the effect works but the test was signed "Failed" with the exception:

com.github.rainmanwy.robotframework.sikulilib.exceptions.ScreenOperatioException: Paste text failed

We have found an error in ScreenKeywords.java (line 441):


@RobotKeyword("Paste text. Image could be empty")
    @ArgumentNames({"image", "text"})
    public void pasteText(String image, String text) throws Exception {
        System.out.println("Paste Text:");
        System.out.println(text);
        if ( !"".equals(image) ) {
            this.click(image);
        }
        int result = region.paste(text);
        if (result != 0) {
            throw new ScreenOperationException("Paste text failed");
        }
    }

The new function in the file Region.java (Functions at line 4663 and line 4681) (from SikuliX1 repository) is changed, now return 0 for error and 1 for correct imputation:

/**
   * pastes the text at the current position of the focus/carret <br>using the clipboard and strg/ctrl/cmd-v (paste
   * keyboard shortcut)
   *
   * @param text a string, which might contain unicode characters
   * @return 0 if possible, 1 otherwise
   */
  public int paste(String text) {
    try {
      return paste(null, text);
    } catch (FindFailed ex) {
      return 0;
    }
  }

  /**
   * first does a click(target) at the given target position to gain focus/carret <br> and then pastes the text <br>
   * using the clipboard and strg/ctrl/cmd-v (paste keyboard shortcut)
   *
   * @param <PFRML> Pattern, Filename, Text, Region, Match or Location target
   * @param target  Pattern, Filename, Text, Region, Match or Location
   * @param text    a string, which might contain unicode characters
   * @return 1 if possible, 0 otherwise
   * @throws FindFailed if not found
   */
  public <PFRML> int paste(PFRML target, String text) throws FindFailed {
    if (target != null && 0 == click(target, 0)) {
      return 0;
    }
    if (text != null) {
      App.setClipboard(text);
      int mod = Key.getHotkeyModifier();
      IRobot r = getRobotForRegion();
      r.keyDown(mod);
      r.keyDown(KeyEvent.VK_V);
      r.keyUp(KeyEvent.VK_V);
      r.keyUp(mod);
      return 1;
    }
    return 0;
  }

We suggest to change the file ScreenKeywords.java with this:


@RobotKeyword("Paste text. Image could be empty")
    @ArgumentNames({"image", "text"})
    public void pasteText(String image, String text) throws Exception {

        [...]

        int result = region.paste(text);
        if (result != 1) {
            throw new ScreenOperationException("Paste text failed");
        }
    }