public static void runTestCaseExecuteElement(Element executeElement) throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

    XMLLoggerHandler.updateStatus(executeElement, "pending");

    String classCommandName = executeElement.attributeValue("test-case");

    String className = PoshiRunnerGetterUtil.getClassNameFromClassCommandName(classCommandName);

    if (className.equals("super")) {
      className = PoshiRunnerGetterUtil.getExtendedTestCaseName();

      classCommandName = classCommandName.replaceFirst("super", className);
    }

    PoshiRunnerStackTraceUtil.pushStackTrace(executeElement);

    Element rootElement = PoshiRunnerContext.getTestCaseRootElement(className);

    List<Element> rootVarElements = rootElement.elements("var");

    for (Element rootVarElement : rootVarElements) {
      runVarElement(rootVarElement, false, true);
    }

    Element commandElement = PoshiRunnerContext.getTestCaseCommandElement(classCommandName);

    runTestCaseCommandElement(commandElement);

    PoshiRunnerStackTraceUtil.popStackTrace();

    XMLLoggerHandler.updateStatus(executeElement, "pass");
  }
  public static void runForElement(Element element) throws Exception {
    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    XMLLoggerHandler.updateStatus(element, "pending");

    String list = PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("list"));

    String[] paramValues = list.split(",");

    String paramName = PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("param"));

    for (String paramValue : paramValues) {
      PoshiRunnerVariablesUtil.putIntoCommandMap(paramName, paramValue);

      parseElement(element);
    }

    XMLLoggerHandler.updateStatus(element, "pass");
  }
  public static void runTaskElement(Element element) throws Exception {
    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    XMLLoggerHandler.updateStatus(element, "pending");

    try {
      SummaryLoggerHandler.startSummary(element);

      parseElement(element);
    } catch (Exception e) {
      SummaryLoggerHandler.failSummary(element, e.getMessage());

      throw e;
    }

    SummaryLoggerHandler.passSummary(element);

    XMLLoggerHandler.updateStatus(element, "pass");
  }
  public static void runWhileElement(Element element) throws Exception {
    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    XMLLoggerHandler.updateStatus(element, "pending");

    int maxIterations = 15;

    if (element.attributeValue("max-iterations") != null) {
      maxIterations = GetterUtil.getInteger(element.attributeValue("max-iterations"));
    }

    List<Element> whileChildElements = element.elements();

    Element conditionElement = whileChildElements.get(0);
    Element thenElement = element.element("then");

    boolean conditionRun = false;

    for (int i = 0; i < maxIterations; i++) {
      if (!evaluateConditionalElement(conditionElement)) {
        break;
      }

      conditionRun = true;

      PoshiRunnerStackTraceUtil.setCurrentElement(thenElement);

      XMLLoggerHandler.updateStatus(thenElement, "pending");

      parseElement(thenElement);

      XMLLoggerHandler.updateStatus(thenElement, "pass");
    }

    if (conditionRun) {
      XMLLoggerHandler.updateStatus(element, "pass");
    } else {
      XMLLoggerHandler.updateStatus(element, "conditional-fail");
    }
  }
  public static void runReturnElement(Element returnElement) throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(returnElement);

    if (returnElement.attributeValue("value") != null) {
      String returnName = returnElement.attributeValue("name");
      String returnValue = returnElement.attributeValue("value");

      returnValue = PoshiRunnerVariablesUtil.replaceCommandVars(returnValue);

      PoshiRunnerVariablesUtil.putIntoReturnMap(returnName, returnValue);
    }

    XMLLoggerHandler.updateStatus(returnElement, "pass");
  }
  public static void runFailElement(Element element) throws Exception {
    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    CommandLoggerHandler.logMessage(element);

    String message = element.attributeValue("message");

    XMLLoggerHandler.updateStatus(element, "fail");

    if (Validator.isNotNull(message)) {
      throw new Exception(PoshiRunnerVariablesUtil.replaceCommandVars(message));
    }

    throw new Exception();
  }
  public static void runVarElement(Element element, boolean commandVar, boolean updateLoggerStatus)
      throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    if (updateLoggerStatus) {
      XMLLoggerHandler.updateStatus(element, "pending");
    }

    String varName = element.attributeValue("name");
    String varValue = element.attributeValue("value");

    if (varValue == null) {
      if (element.attributeValue("attribute") != null) {
        LiferaySelenium liferaySelenium = SeleniumUtil.getSelenium();

        String attribute = element.attributeValue("attribute");

        String locator = element.attributeValue("locator");

        if (locator.contains("#")) {
          locator = PoshiRunnerContext.getPathLocator(locator);
        }

        varValue = liferaySelenium.getAttribute(locator + "@" + attribute);
      } else if ((element.attributeValue("group") != null)
          && (element.attributeValue("input") != null)
          && (element.attributeValue("pattern") != null)) {

        varValue =
            RegexUtil.replace(
                PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("input")),
                element.attributeValue("pattern"),
                element.attributeValue("group"));
      } else if (element.attributeValue("locator") != null) {
        String locator = element.attributeValue("locator");

        if (locator.contains("#")) {
          locator = PoshiRunnerContext.getPathLocator(locator);
        }

        LiferaySelenium liferaySelenium = SeleniumUtil.getSelenium();

        locator = PoshiRunnerVariablesUtil.replaceCommandVars(locator);

        try {
          if (locator.contains("/input")) {
            varValue = liferaySelenium.getElementValue(locator);
          } else {
            varValue = liferaySelenium.getElementText(locator);
          }
        } catch (Exception e) {
          XMLLoggerHandler.updateStatus(element, "fail");

          throw e;
        }
      } else if (element.attributeValue("method") != null) {
        String classCommandName =
            PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("method"));

        if (classCommandName.startsWith("TestPropsUtil")) {
          classCommandName = classCommandName.replace("TestPropsUtil", "PropsUtil");
        }

        try {
          varValue = PoshiRunnerGetterUtil.getVarMethodValue(classCommandName);
        } catch (Exception e) {
          XMLLoggerHandler.updateStatus(element, "fail");

          Throwable throwable = e.getCause();

          throw new Exception(throwable.getMessage(), e);
        }
      } else if (element.attributeValue("property-value") != null) {
        varValue = PropsUtil.get(element.attributeValue("property-value"));

        if (varValue == null) {
          varValue = "";
        }
      } else {
        varValue = element.getText();
      }
    } else {
      Matcher matcher = _variableMethodPattern.matcher(varValue);

      if (matcher.find()) {
        String method = matcher.group(2);
        String variable = matcher.group(1);

        if (method.equals("length()")) {
          if (PoshiRunnerVariablesUtil.containsKeyInCommandMap(variable)) {

            variable = PoshiRunnerVariablesUtil.getValueFromCommandMap(variable);
          } else {
            throw new Exception("No such variable " + variable);
          }

          varValue = String.valueOf(variable.length());
        } else {
          throw new Exception("No such method " + method);
        }
      }
    }

    String replacedVarValue = PoshiRunnerVariablesUtil.replaceCommandVars(varValue);

    Matcher matcher = _variablePattern.matcher(replacedVarValue);

    if (matcher.matches() && replacedVarValue.equals(varValue)) {
      if (updateLoggerStatus) {
        XMLLoggerHandler.updateStatus(element, "pass");
      }

      return;
    }

    String staticValue = element.attributeValue("static");

    if (commandVar) {
      PoshiRunnerVariablesUtil.putIntoCommandMap(varName, replacedVarValue);
    } else if ((staticValue != null) && staticValue.equals("true")) {
      if (!PoshiRunnerVariablesUtil.containsKeyInStaticMap(varName)) {
        PoshiRunnerVariablesUtil.putIntoStaticMap(varName, replacedVarValue);
      }
    } else {
      PoshiRunnerVariablesUtil.putIntoExecuteMap(varName, replacedVarValue);
    }

    String currentFilePath = PoshiRunnerStackTraceUtil.getCurrentFilePath();

    if (commandVar && currentFilePath.contains(".testcase")) {
      if (PoshiRunnerVariablesUtil.containsKeyInStaticMap(varName)) {
        PoshiRunnerVariablesUtil.putIntoStaticMap(varName, replacedVarValue);
      }
    }

    if (updateLoggerStatus) {
      XMLLoggerHandler.updateStatus(element, "pass");
    }
  }
  public static void runMacroExecuteElement(Element executeElement, String macroType)
      throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

    XMLLoggerHandler.updateStatus(executeElement, "pending");

    String classCommandName = executeElement.attributeValue(macroType);

    String className = PoshiRunnerGetterUtil.getClassNameFromClassCommandName(classCommandName);

    PoshiRunnerStackTraceUtil.pushStackTrace(executeElement);

    Element rootElement = PoshiRunnerContext.getMacroRootElement(className);

    List<Element> rootVarElements = rootElement.elements("var");

    for (Element rootVarElement : rootVarElements) {
      runVarElement(rootVarElement, false, true);
    }

    PoshiRunnerStackTraceUtil.popStackTrace();

    List<Element> executeVarElements = executeElement.elements("var");

    for (Element executeVarElement : executeVarElements) {
      runVarElement(executeVarElement, false, false);
    }

    PoshiRunnerStackTraceUtil.pushStackTrace(executeElement);

    SummaryLoggerHandler.startSummary(executeElement);

    Element commandElement = PoshiRunnerContext.getMacroCommandElement(classCommandName);

    try {
      Map<String, String> macroReturns = runMacroCommandElement(classCommandName, commandElement);

      List<Element> returnElements = executeElement.elements("return");

      for (Element returnElement : returnElements) {
        String returnFrom = returnElement.attributeValue("from");

        String returnValue = macroReturns.get(returnFrom);

        if (returnValue != null) {
          String returnName = returnElement.attributeValue("name");

          PoshiRunnerVariablesUtil.putIntoCommandMap(returnName, returnValue);
        }
      }
    } catch (Exception e) {
      SummaryLoggerHandler.failSummary(executeElement, e.getMessage());

      throw e;
    }

    SummaryLoggerHandler.passSummary(executeElement);

    PoshiRunnerStackTraceUtil.popStackTrace();

    XMLLoggerHandler.updateStatus(executeElement, "pass");
  }
  public static boolean evaluateConditionalElement(Element element) throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    XMLLoggerHandler.updateStatus(element, "pending");

    boolean conditionalValue = false;

    String elementName = element.getName();

    if (elementName.equals("and")) {
      List<Element> andElements = element.elements();

      conditionalValue = true;

      for (Element andElement : andElements) {
        if (conditionalValue) {
          conditionalValue = evaluateConditionalElement(andElement);
        }

        if (!conditionalValue) {
          break;
        }
      }
    } else if (elementName.equals("condition")) {
      if (element.attributeValue("function") != null) {
        runFunctionExecuteElement(element);

        conditionalValue = (boolean) _returnObject;
      } else if (element.attributeValue("selenium") != null) {
        runSeleniumElement(element);

        conditionalValue = (boolean) _returnObject;
      }
    } else if (elementName.equals("contains")) {
      String string = PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("string"));
      String substring =
          PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("substring"));

      if (string.contains(substring)) {
        conditionalValue = true;
      }
    } else if (elementName.equals("equals")) {
      String arg1 = PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("arg1"));
      String arg2 = PoshiRunnerVariablesUtil.replaceCommandVars(element.attributeValue("arg2"));

      if (arg1.equals(arg2)) {
        conditionalValue = true;
      }
    } else if (elementName.equals("isset")) {
      if (PoshiRunnerVariablesUtil.containsKeyInCommandMap(element.attributeValue("var"))) {

        conditionalValue = true;
      }
    } else if (elementName.equals("or")) {
      List<Element> orElements = element.elements();

      for (Element orElement : orElements) {
        if (!conditionalValue) {
          conditionalValue = evaluateConditionalElement(orElement);
        }

        if (conditionalValue) {
          break;
        }
      }
    } else if (elementName.equals("not")) {
      List<Element> notElements = element.elements();

      Element notElement = notElements.get(0);

      conditionalValue = !evaluateConditionalElement(notElement);
    }

    if (conditionalValue) {
      XMLLoggerHandler.updateStatus(element, "pass");
    } else {
      XMLLoggerHandler.updateStatus(element, "conditional-fail");
    }

    return conditionalValue;
  }
  public static void runIfElement(Element element) throws Exception {
    PoshiRunnerStackTraceUtil.setCurrentElement(element);

    XMLLoggerHandler.updateStatus(element, "pending");

    List<Element> ifChildElements = element.elements();

    Element ifConditionElement = ifChildElements.get(0);

    boolean condition = evaluateConditionalElement(ifConditionElement);
    boolean conditionRun = false;

    if (condition) {
      conditionRun = true;

      Element ifThenElement = element.element("then");

      PoshiRunnerStackTraceUtil.setCurrentElement(ifThenElement);

      XMLLoggerHandler.updateStatus(ifThenElement, "pending");

      parseElement(ifThenElement);

      XMLLoggerHandler.updateStatus(ifThenElement, "pass");
    } else if (element.element("elseif") != null) {
      List<Element> elseIfElements = element.elements("elseif");

      for (Element elseIfElement : elseIfElements) {
        PoshiRunnerStackTraceUtil.setCurrentElement(elseIfElement);

        XMLLoggerHandler.updateStatus(elseIfElement, "pending");

        List<Element> elseIfChildElements = elseIfElement.elements();

        Element elseIfConditionElement = elseIfChildElements.get(0);

        condition = evaluateConditionalElement(elseIfConditionElement);

        if (condition) {
          conditionRun = true;

          Element elseIfThenElement = elseIfElement.element("then");

          PoshiRunnerStackTraceUtil.setCurrentElement(elseIfThenElement);

          XMLLoggerHandler.updateStatus(elseIfThenElement, "pending");

          parseElement(elseIfThenElement);

          XMLLoggerHandler.updateStatus(elseIfThenElement, "pass");
          XMLLoggerHandler.updateStatus(elseIfElement, "pass");

          break;
        } else {
          XMLLoggerHandler.updateStatus(elseIfElement, "conditional-fail");
        }
      }
    }

    if ((element.element("else") != null) && !conditionRun) {
      conditionRun = true;

      Element elseElement = element.element("else");

      PoshiRunnerStackTraceUtil.setCurrentElement(elseElement);

      XMLLoggerHandler.updateStatus(elseElement, "pending");

      parseElement(elseElement);

      XMLLoggerHandler.updateStatus(elseElement, "pass");
    }

    if (conditionRun) {
      XMLLoggerHandler.updateStatus(element, "pass");
    } else {
      XMLLoggerHandler.updateStatus(element, "conditional-fail");
    }
  }