Ejemplo n.º 1
0
  public boolean runPrerequestScript(PostmanRequest request, PostmanRunResult runResult) {
    if (request.preRequestScript == null || request.preRequestScript.isEmpty()) {
      return true;
    }
    Context cx = Context.enter();
    String testName = "---------------------> POSTMAN test: ";
    boolean isSuccessful = false;
    try {
      Scriptable scope = cx.initStandardObjects();
      PostmanJsVariables jsVar = new PostmanJsVariables(cx, scope, this.var.getEnv());
      // jsVar.prepare(httpResponse);
      jsVar.prepare(null);

      // Evaluate the test script
      cx.evaluateString(scope, request.preRequestScript, testName, 1, null);
      // The results are in the jsVar.tests ???? variable

      // Extract any generated environment variables during the js run.
      jsVar.extractEnvironmentVariables();
      isSuccessful = true;
    } finally {
      Context.exit();
    }
    return isSuccessful;
  }
Ejemplo n.º 2
0
  /**
   * @param request
   * @param httpResponse
   * @return true if all tests pass, false otherwise
   */
  public boolean evaluateTests(
      PostmanRequest request, ResponseEntity<String> httpResponse, PostmanRunResult runResult) {
    if (request.tests == null || request.tests.isEmpty()) {
      return true;
    }
    Context cx = Context.enter();
    String testName = "---------------------> POSTMAN test";
    boolean isSuccessful = false;
    try {
      Scriptable scope = cx.initStandardObjects();
      PostmanJsVariables jsVar = new PostmanJsVariables(cx, scope, this.var.getEnv());
      jsVar.prepare(httpResponse);

      // Evaluate the test script
      cx.evaluateString(scope, request.tests, testName, 1, null);
      // The results are in the jsVar.tests variable

      // Extract any generated environment variables during the js run.
      jsVar.extractEnvironmentVariables();
      isSuccessful = true;
      boolean hasFailure = false;
      for (Map.Entry e : jsVar.tests.entrySet()) {
        runResult.totalTest++;

        String strVal = e.getValue().toString();
        if ("false".equalsIgnoreCase(strVal)) {
          hasFailure = true;
          runResult.failedTest++;
          runResult.failedTestName.add(request.name + "." + e.getKey().toString());
          isSuccessful = false;
        }

        System.out.println(testName + ": " + e.getKey() + " - " + e.getValue());
      }
      if (hasFailure) {
        System.out.println("=====THERE ARE TEST FAILURES=====");
        System.out.println("========TEST========");
        System.out.println(request.tests);
        System.out.println("========TEST========");
        System.out.println("========RESPONSE========");
        System.out.println(httpResponse.getStatusCode());
        System.out.println(httpResponse.getBody());
        System.out.println("========RESPONSE========");
        System.out.println("=====THERE ARE TEST FAILURES=====");
      }
    } catch (Throwable t) {
      isSuccessful = false;
      System.out.println("=====FAILED TO EVALUATE TEST AGAINST SERVER RESPONSE======");
      System.out.println("========TEST========");
      System.out.println(request.tests);
      System.out.println("========TEST========");
      System.out.println("========RESPONSE========");
      System.out.println(httpResponse.getStatusCode());
      System.out.println(httpResponse.getBody());
      System.out.println("========RESPONSE========");
      System.out.println("=====FAILED TO EVALUATE TEST AGAINST SERVER RESPONSE======");
    } finally {
      Context.exit();
    }
    return isSuccessful;
  }