public static void typeAceEditor(LiferaySelenium liferaySelenium, String locator, String value) {

    int x = 0;
    int y = value.indexOf("${line.separator}");

    String line = value;

    if (y != -1) {
      line = value.substring(x, y);
    }

    liferaySelenium.typeKeys(locator, line.trim());

    liferaySelenium.keyPress(locator, "\\RETURN");

    while (y != -1) {
      x = value.indexOf("}", x) + 1;
      y = value.indexOf("${line.separator}", x);

      if (y != -1) {
        line = value.substring(x, y);
      } else {
        line = value.substring(x, value.length());
      }

      liferaySelenium.typeKeys(locator, line.trim());

      liferaySelenium.keyPress(locator, "\\RETURN");
    }
  }
  public static void assertVisible(LiferaySelenium liferaySelenium, String locator)
      throws Exception {

    liferaySelenium.assertElementPresent(locator);

    if (liferaySelenium.isNotVisible(locator)) {
      throw new Exception("Element is not visible at \"" + locator + "\"");
    }
  }
  public static void assertNotChecked(LiferaySelenium liferaySelenium, String locator)
      throws Exception {

    liferaySelenium.assertElementPresent(locator);

    if (liferaySelenium.isChecked(locator)) {
      throw new Exception("Element is checked at \"" + locator + "\"");
    }
  }
  public static void assertNotPartialText(
      LiferaySelenium liferaySelenium, String locator, String pattern) throws Exception {

    liferaySelenium.assertElementPresent(locator);

    if (liferaySelenium.isPartialText(locator, pattern)) {
      String text = liferaySelenium.getText(locator);

      throw new Exception("\"" + text + "\" contains \"" + pattern + "\" at \"" + locator + "\"");
    }
  }
  public static void assertValue(LiferaySelenium liferaySelenium, String locator, String pattern)
      throws Exception {

    liferaySelenium.assertElementPresent(locator);

    if (liferaySelenium.isNotValue(locator, pattern)) {
      String value = liferaySelenium.getValue(locator);

      throw new Exception(
          "Pattern \"" + pattern + "\" does not match \"" + value + "\" at \"" + locator + "\"");
    }
  }
  public static void assertNotSelectedLabel(
      LiferaySelenium liferaySelenium, String selectLocator, String pattern) throws Exception {

    liferaySelenium.assertElementPresent(selectLocator);

    if (liferaySelenium.isSelectedLabel(selectLocator, pattern)) {
      String text = liferaySelenium.getSelectedLabel(selectLocator);

      throw new Exception(
          "Pattern \"" + pattern + "\" matches \"" + text + "\" at \"" + selectLocator + "\"");
    }
  }
  public static void sikuliType(LiferaySelenium liferaySelenium, String image, String value)
      throws Exception {

    sikuliClick(liferaySelenium, image);

    liferaySelenium.pause("1000");

    Keyboard keyboard = new DesktopKeyboard();

    if (value.contains("${line.separator}")) {
      String[] tokens = StringUtil.split(value, "${line.separator}");

      for (int i = 0; i < tokens.length; i++) {
        keyboard.type(tokens[i]);

        if ((i + 1) < tokens.length) {
          keyboard.type(Key.ENTER);
        }
      }

      if (value.endsWith("${line.separator}")) {
        keyboard.type(Key.ENTER);
      }
    } else {
      keyboard.type(value);
    }
  }
  public static void sendEmail(
      LiferaySelenium liferaySelenium, String to, String subject, String body) throws Exception {

    EmailCommands.sendEmail(to, subject, body);

    liferaySelenium.pause("3000");
  }
  public static boolean isHTMLSourceTextPresent(LiferaySelenium liferaySelenium, String value)
      throws Exception {

    URL url = new URL(liferaySelenium.getLocation());

    InputStream inputStream = url.openStream();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    String line = null;

    while ((line = bufferedReader.readLine()) != null) {
      Pattern pattern = Pattern.compile(value);

      Matcher matcher = pattern.matcher(line);

      if (matcher.find()) {
        return true;
      }
    }

    inputStream.close();

    bufferedReader.close();

    return false;
  }
  public static ImageTarget getImageTarget(LiferaySelenium liferaySelenium, String image)
      throws Exception {

    File file = new File(getPortalRootDirName() + liferaySelenium.getSikuliImagesDirName() + image);

    return new ImageTarget(file);
  }
  public static void assertTextPresent(LiferaySelenium liferaySelenium, String pattern)
      throws Exception {

    if (liferaySelenium.isTextNotPresent(pattern)) {
      throw new Exception("\"" + pattern + "\" is not present");
    }
  }
  public static void replyToEmail(LiferaySelenium liferaySelenium, String to, String body)
      throws Exception {

    EmailCommands.replyToEmail(to, body);

    liferaySelenium.pause("3000");
  }
  public static void assertElementPresent(LiferaySelenium liferaySelenium, String locator)
      throws Exception {

    if (liferaySelenium.isElementNotPresent(locator)) {
      throw new Exception("Element is not present at \"" + locator + "\"");
    }
  }
  public static void sikuliLeftMouseDown(LiferaySelenium liferaySelenium) throws Exception {

    liferaySelenium.pause("1000");

    Mouse mouse = new DesktopMouse();

    mouse.press();
  }
  public static void sikuliRightMouseUp(LiferaySelenium liferaySelenium) throws Exception {

    liferaySelenium.pause("1000");

    Mouse mouse = new DesktopMouse();

    mouse.rightRelease();
  }
  public static boolean isElementPresentAfterWait(LiferaySelenium liferaySelenium, String locator)
      throws Exception {

    for (int second = 0; ; second++) {
      if (second >= PropsValues.TIMEOUT_EXPLICIT_WAIT) {
        return liferaySelenium.isElementPresent(locator);
      }

      if (liferaySelenium.isElementPresent(locator)) {
        break;
      }

      Thread.sleep(1000);
    }

    return liferaySelenium.isElementPresent(locator);
  }
  public static void waitForSelectedLabel(
      LiferaySelenium liferaySelenium, String selectLocator, String pattern) throws Exception {

    for (int second = 0; ; second++) {
      if (second >= PropsValues.TIMEOUT_EXPLICIT_WAIT) {
        liferaySelenium.assertSelectedLabel(selectLocator, pattern);
      }

      try {
        if (liferaySelenium.isSelectedLabel(selectLocator, pattern)) {
          break;
        }
      } catch (Exception e) {
      }

      Thread.sleep(1000);
    }
  }
  public static void assertConfirmation(LiferaySelenium liferaySelenium, String pattern)
      throws Exception {

    String confirmation = liferaySelenium.getConfirmation();

    if (!pattern.equals(confirmation)) {
      throw new Exception("Pattern \"" + pattern + "\" does not match \"" + confirmation + "\"");
    }
  }
  public static void assertPartialConfirmation(LiferaySelenium liferaySelenium, String pattern)
      throws Exception {

    String confirmation = liferaySelenium.getConfirmation();

    if (!confirmation.contains(pattern)) {
      throw new Exception("\"" + confirmation + "\" does not contain \"" + pattern + "\"");
    }
  }
  public static void waitForVisible(LiferaySelenium liferaySelenium, String locator)
      throws Exception {

    for (int second = 0; ; second++) {
      if (second >= PropsValues.TIMEOUT_EXPLICIT_WAIT) {
        liferaySelenium.assertVisible(locator);
      }

      try {
        if (liferaySelenium.isVisible(locator)) {
          break;
        }
      } catch (Exception e) {
      }

      Thread.sleep(1000);
    }
  }
  public static void saveScreenshot(LiferaySelenium liferaySelenium) throws Exception {

    _screenshotCount++;

    captureScreen(
        liferaySelenium.getProjectDirName()
            + "portal-web/test-results/functional/screenshots/"
            + _screenshotCount
            + ".jpg");
  }
  public static void waitForValue(LiferaySelenium liferaySelenium, String locator, String value)
      throws Exception {

    value = RuntimeVariables.replace(value);

    for (int second = 0; ; second++) {
      if (second >= PropsValues.TIMEOUT_EXPLICIT_WAIT) {
        liferaySelenium.assertValue(locator, value);
      }

      try {
        if (liferaySelenium.isValue(locator, value)) {
          break;
        }
      } catch (Exception e) {
      }

      Thread.sleep(1000);
    }
  }
  public static void typeFrame(LiferaySelenium liferaySelenium, String locator, String value) {

    StringBuilder sb = new StringBuilder();

    String titleAttribute = liferaySelenium.getAttribute(locator + "@title");

    int x = titleAttribute.indexOf(",");
    int y = titleAttribute.indexOf(",", x + 1);

    if (y == -1) {
      y = titleAttribute.length();
    }

    sb.append(titleAttribute.substring(x + 1, y));

    sb.append(".setHTML(\"");
    sb.append(HtmlUtil.escapeJS(value.replace("\\", "\\\\")));
    sb.append("\")");

    liferaySelenium.runScript(sb.toString());
  }
  public static void saveScreenshotBeforeAction(
      LiferaySelenium liferaySelenium, boolean actionFailed) throws Exception {

    if (actionFailed) {
      _screenshotErrorCount++;
    }

    captureScreen(
        liferaySelenium.getProjectDirName()
            + "portal-web/test-results/functional/screenshots/"
            + "ScreenshotBeforeAction"
            + _screenshotErrorCount
            + ".jpg");
  }
  public static void sikuliUploadCommonFile(
      LiferaySelenium liferaySelenium, String image, String value) throws Exception {

    sikuliClick(liferaySelenium, image);

    Keyboard keyboard = new DesktopKeyboard();

    keyboard.keyDown(Key.CTRL);

    keyboard.type("a");

    keyboard.keyUp(Key.CTRL);

    sikuliType(
        liferaySelenium,
        image,
        getPortalRootDirName() + liferaySelenium.getDependenciesDirName() + value);

    keyboard.type(Key.ENTER);
  }
  public static void sikuliUploadTempFile(
      LiferaySelenium liferaySelenium, String image, String value) throws Exception {

    sikuliClick(liferaySelenium, image);

    Keyboard keyboard = new DesktopKeyboard();

    keyboard.keyDown(Key.CTRL);

    keyboard.type("a");

    keyboard.keyUp(Key.CTRL);

    String slash = "/";

    if (OSDetector.isWindows()) {
      slash = "\\";
    }

    sikuliType(liferaySelenium, image, liferaySelenium.getOutputDirName() + slash + value);

    keyboard.type(Key.ENTER);
  }
  public static boolean isTextNotPresent(LiferaySelenium liferaySelenium, String pattern) {

    return !liferaySelenium.isTextPresent(pattern);
  }
  public static boolean isNotVisible(LiferaySelenium liferaySelenium, String locator) {

    return !liferaySelenium.isVisible(locator);
  }
  public static boolean isNotValue(LiferaySelenium liferaySelenium, String locator, String value) {

    return !liferaySelenium.isValue(locator, value);
  }
  public static boolean isNotPartialText(
      LiferaySelenium liferaySelenium, String locator, String value) {

    return !liferaySelenium.isPartialText(locator, value);
  }