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");
  }
Exemplo n.º 2
0
  public void testPoshiRunner() throws Exception {
    String classCommandName = System.getProperty("test.case.name");

    Element element = PoshiRunnerContext.getTestcaseCommandElement(classCommandName);

    PoshiRunnerExecutor.parseElement(element);
  }
  public static void pushFilePath(String className, String classType) {
    if (className.contains("#")) {
      className = PoshiRunnerGetterUtil.getClassNameFromClassCommandName(className);
    }

    String fileExtension = PoshiRunnerGetterUtil.getFileExtensionFromClassType(classType);

    _filePaths.push(PoshiRunnerContext.getFilePathFromFileName(className + "." + fileExtension));
  }
  @Test
  public void testValidateFunctionContext() {
    String filePath = getFilePath("ValidateFunctionContext.macro");

    Element element =
        PoshiRunnerContext.getMacroCommandElement(
            "ValidateFunctionContext#validateFunctionContextPass");

    List<Element> functionElements = element.elements("execute");

    for (Element functionElement : functionElements) {
      PoshiRunnerValidation.validateFunctionContext(functionElement, filePath);
    }

    Assert.assertEquals("ValidateFunctionContext is failing", "", getExceptionMessage());

    element =
        PoshiRunnerContext.getMacroCommandElement(
            "ValidateFunctionContext#validateFunctionContextFail1");

    functionElements = element.elements("execute");

    for (Element functionElement : functionElements) {
      PoshiRunnerValidation.validateFunctionContext(functionElement, filePath);
    }

    Assert.assertEquals(
        "validateFunctionContext is failing", "Invalid path name ClickAt", getExceptionMessage());

    element =
        PoshiRunnerContext.getMacroCommandElement(
            "ValidateFunctionContext#validateFunctionContextFail2");

    functionElements = element.elements("execute");

    for (Element functionElement : functionElements) {
      PoshiRunnerValidation.validateFunctionContext(functionElement, filePath);
    }

    Assert.assertEquals(
        "validateFunctionContext is failing",
        "Invalid path locator Click#CLICK_THERE",
        getExceptionMessage());
  }
  @Test
  public void testValidateClassCommandName() {
    String classCommandName = "ValidateClassCommandName#classCommandName";

    Element element = PoshiRunnerContext.getTestCaseCommandElement(classCommandName);

    String filePath = getFilePath("ValidateClassCommandName.testcase");

    PoshiRunnerValidation.validateClassCommandName(
        element, classCommandName, "test-case", filePath);

    Assert.assertEquals("validateClassCommandName is failing", "", getExceptionMessage());

    PoshiRunnerValidation.validateClassCommandName(
        element, "ValidateClassCommandName#fail", "test-case", filePath);

    Assert.assertEquals(
        "validateClassCommandName is failing",
        "Invalid test-case command ValidateClassCommandName#fail",
        getExceptionMessage());
  }
 @After
 @Override
 public void tearDown() throws Exception {
   PoshiRunnerContext.clear();
 }
 @Before
 @Override
 public void setUp() throws Exception {
   PoshiRunnerContext.readFiles();
 }
  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 runSeleniumElement(Element executeElement) throws Exception {

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

    List<String> arguments = new ArrayList<>();
    List<Class<?>> parameterClasses = new ArrayList<>();

    String selenium = executeElement.attributeValue("selenium");

    int parameterCount = PoshiRunnerContext.getSeleniumParameterCount(selenium);

    for (int i = 0; i < parameterCount; i++) {
      String argument = executeElement.attributeValue("argument" + (i + 1));

      if (argument == null) {
        if (i == 0) {
          if (selenium.equals("assertConfirmation")
              || selenium.equals("assertConsoleTextNotPresent")
              || selenium.equals("assertConsoleTextPresent")
              || selenium.equals("assertLocation")
              || selenium.equals("assertHTMLSourceTextNotPresent")
              || selenium.equals("assertHTMLSourceTextPresent")
              || selenium.equals("assertNotLocation")
              || selenium.equals("assertTextNotPresent")
              || selenium.equals("assertTextPresent")
              || selenium.equals("waitForConfirmation")
              || selenium.equals("waitForTextNotPresent")
              || selenium.equals("waitForTextPresent")) {

            argument = PoshiRunnerVariablesUtil.getValueFromCommandMap("value1");
          } else {
            argument = PoshiRunnerVariablesUtil.getValueFromCommandMap("locator1");
          }
        } else if (i == 1) {
          argument = PoshiRunnerVariablesUtil.getValueFromCommandMap("value1");

          if (selenium.equals("clickAt")) {
            argument = "";
          }
        } else if (i == 2) {
          if (selenium.equals("assertCssValue")) {
            argument = PoshiRunnerVariablesUtil.getValueFromCommandMap("value1");
          } else {
            argument = PoshiRunnerVariablesUtil.getValueFromCommandMap("locator2");
          }
        }
      } else {
        argument = PoshiRunnerVariablesUtil.replaceCommandVars(argument);
      }

      arguments.add(argument);

      parameterClasses.add(String.class);
    }

    CommandLoggerHandler.logSeleniumCommand(executeElement, arguments);

    LiferaySelenium liferaySelenium = SeleniumUtil.getSelenium();

    Class<?> clazz = liferaySelenium.getClass();

    try {
      Method method =
          clazz.getMethod(selenium, parameterClasses.toArray(new Class[parameterClasses.size()]));

      _returnObject =
          method.invoke(
              liferaySelenium, (Object[]) arguments.toArray(new String[arguments.size()]));
    } catch (Exception e) {
      Throwable throwable = e.getCause();

      throw new Exception(throwable.getMessage(), e);
    }
  }
  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 void runFunctionExecuteElement(Element executeElement) throws Exception {

    if (_functionExecuteElement == null) {
      _functionExecuteElement = executeElement;
    }

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

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

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

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

    String classCommandName = executeElement.attributeValue("function");

    String className = classCommandName;

    if (classCommandName.contains("#")) {
      className = PoshiRunnerGetterUtil.getClassNameFromClassCommandName(classCommandName);
    }

    Exception exception = null;

    int locatorCount = PoshiRunnerContext.getFunctionLocatorCount(className);

    for (int i = 0; i < locatorCount; i++) {
      String locator = executeElement.attributeValue("locator" + (i + 1));

      if (locator == null) {
        locator = PoshiRunnerVariablesUtil.getValueFromCommandMap("locator" + (i + 1));
      }

      if (locator != null) {
        Matcher matcher = _locatorKeyPattern.matcher(locator);

        if (matcher.find() && !locator.contains("/")) {
          String pathClassName = PoshiRunnerGetterUtil.getClassNameFromClassCommandName(locator);

          String locatorKey =
              PoshiRunnerVariablesUtil.replaceCommandVars(
                  PoshiRunnerGetterUtil.getCommandNameFromClassCommandName(locator));

          PoshiRunnerVariablesUtil.putIntoExecuteMap("locator-key" + (i + 1), locatorKey);

          try {
            locator = PoshiRunnerContext.getPathLocator(pathClassName + "#" + locatorKey);
          } catch (Exception e) {
            exception = e;
          }

          locator = PoshiRunnerVariablesUtil.replaceExecuteVars(locator);
        }

        PoshiRunnerVariablesUtil.putIntoExecuteMap("locator" + (i + 1), locator);
      }

      String value = executeElement.attributeValue("value" + (i + 1));

      if (value == null) {
        value = PoshiRunnerVariablesUtil.getValueFromCommandMap("value" + (i + 1));
      }

      if (value != null) {
        PoshiRunnerVariablesUtil.putIntoExecuteMap("value" + (i + 1), value);
      }
    }

    SummaryLoggerHandler.startSummary(executeElement);

    CommandLoggerHandler.startCommand(executeElement);

    PoshiRunnerStackTraceUtil.pushStackTrace(executeElement);

    Element commandElement = PoshiRunnerContext.getFunctionCommandElement(classCommandName);

    try {
      if (exception != null) {
        throw exception;
      }

      runFunctionCommandElement(classCommandName, commandElement);
    } catch (Throwable t) {
      String warningMessage = _getWarningFromThrowable(t);

      if (warningMessage != null) {
        _functionWarningMessage = warningMessage;
      } else {
        PoshiRunnerStackTraceUtil.popStackTrace();

        if (_functionExecuteElement == executeElement) {
          PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

          SummaryLoggerHandler.failSummary(_functionExecuteElement, t.getMessage());

          CommandLoggerHandler.failCommand(_functionExecuteElement);

          _functionExecuteElement = null;
          _functionWarningMessage = null;
        }

        throw t;
      }
    }

    PoshiRunnerStackTraceUtil.popStackTrace();

    PoshiRunnerStackTraceUtil.setCurrentElement(executeElement);

    if (_functionExecuteElement == executeElement) {
      if (_functionWarningMessage != null) {
        SummaryLoggerHandler.warnSummary(_functionExecuteElement, _functionWarningMessage);

        CommandLoggerHandler.warnCommand(_functionExecuteElement);
      } else {
        SummaryLoggerHandler.passSummary(executeElement);

        CommandLoggerHandler.passCommand(executeElement);
      }

      _functionExecuteElement = null;
      _functionWarningMessage = null;
    }
  }