@Override
  public ExternalSessionKey forwardNewSessionRequest(TestSession session)
      throws NewSessionException {
    try {
      // here, don't forward the requestBody directly, but read the
      // desiredCapabilities from the session instead.
      // That allow the TestSessionListener.before modification of the
      // capability map to be propagated.
      JSONObject c = new JSONObject();
      c.put("desiredCapabilities", session.getRequestedCapabilities());
      String content = c.toString();
      session.forward(getRequest(), getResponse(), content, false);
    } catch (IOException e) {
      // log.warning("Error forwarding the request " + e.getMessage());
      throw new NewSessionException("Error forwarding the request " + e.getMessage(), e);
    } catch (JSONException e) {
      // log.warning("Error with the request " + e.getMessage());
      throw new NewSessionException("Error with the request " + e.getMessage(), e);
    }

    if (getResponse().containsHeader("Location")) {
      String location =
          ((ServletHttpResponse) getResponse()).getHttpResponse().getField("Location");
      ExternalSessionKey res = ExternalSessionKey.fromWebDriverRequest(location);
      if (res != null) {
        return res;
      } else {
        throw new NewSessionException("couldn't extract the new session from the response header.");
      }
    } else {
      // log.warning("Error, header should contain Location");
      throw new NewSessionException("Error, header should contain Location ");
    }
  }
예제 #2
0
 public void beforeRelease(TestSession session) {
   // release the resources remotely.
   if (session.getExternalKey() == null) {
     throw new IllegalStateException(
         "cannot release the resources, they haven't been reserved properly.");
   }
   boolean ok = session.sendDeleteSessionRequest();
   if (!ok) {
     log.warning("Error releasing the resources on timeout for session " + session);
   }
 }
 @Override
 public void beforeRelease(TestSession session) {
   // release the resources remotly.
   if (session.getExternalKey() == null) {
     throw new IllegalStateException("No internal key yet. Did the app start properlty?");
   }
   System.err.println("timing out " + session);
   boolean ok = session.sendDeleteSessionRequest();
   if (!ok) {
     log.warning("Error releasing the resources on timeout for session " + session);
   }
 }
 public void beforeSession(TestSession session) {
   Map<String, Object> cap = session.getRequestedCapabilities();
   if ("firefox".equals(cap.get(CapabilityType.BROWSER_NAME))) {
     if (session.getSlot().getCapabilities().get(FirefoxDriver.BINARY) != null
         && cap.get(FirefoxDriver.BINARY) == null) {
       session
           .getRequestedCapabilities()
           .put(
               FirefoxDriver.BINARY,
               session.getSlot().getCapabilities().get(FirefoxDriver.BINARY));
     }
   }
 }
  private JsonObject getResponse(HttpServletRequest request) throws IOException {
    JsonObject requestJSON = null;
    if (request.getInputStream() != null) {
      BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()));
      StringBuilder s = new StringBuilder();
      String line;
      while ((line = rd.readLine()) != null) {
        s.append(line);
      }
      rd.close();
      String json = s.toString();
      if (!"".equals(json)) {
        requestJSON = new JsonParser().parse(json).getAsJsonObject();
      }
    }

    JsonObject res = new JsonObject();
    res.addProperty("success", false);

    // the id can be specified via a param, or in the json request.
    String session;
    if (requestJSON == null) {
      session = request.getParameter("session");
    } else {
      if (!requestJSON.has("session")) {
        res.addProperty(
            "msg",
            "you need to specify at least a session or internalKey when call the test slot status service.");
        return res;
      }
      session = requestJSON.get("session").getAsString();
    }

    TestSession testSession = getRegistry().getSession(ExternalSessionKey.fromString(session));

    if (testSession == null) {
      res.addProperty(
          "msg", "Cannot find test slot running session " + session + " in the registry.");
      return res;
    }
    res.addProperty("msg", "slot found !");
    res.remove("success");
    res.addProperty("success", true);
    res.addProperty("session", testSession.getExternalKey().getKey());
    res.addProperty("internalKey", testSession.getInternalKey());
    res.addProperty("inactivityTime", testSession.getInactivityTime());
    RemoteProxy p = testSession.getSlot().getProxy();
    res.addProperty("proxyId", p.getId());
    return res;
  }
예제 #6
0
  /**
   * The client shouldn't have to care where firefox is installed as long as the correct version is
   * launched, however with webdriver the binary location is specified in the desiredCapability,
   * making it the responsibility of the person running the test.
   *
   * <p>With this implementation of beforeSession, that problem disappears . If the webdriver slot
   * is registered with a firefox using a custom binary location, the hub will handle it.
   *
   * <p>For instance if a node registers:
   * {"browserName":"firefox","version":"7.0","firefox_binary":"/home/ff7"}
   *
   * <p>and later on a client requests {"browserName":"firefox","version":"7.0"} , the hub will
   * automatically append the correct binary path to the desiredCapability before it's forwarded to
   * the server. That way the version / install location mapping is done only once at the node
   * level.
   */
  public void beforeSession(TestSession session) {
    if (session.getSlot().getProtocol() == SeleniumProtocol.WebDriver) {
      Map<String, Object> cap = session.getRequestedCapabilities();

      if (BrowserType.FIREFOX.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get(FirefoxDriver.BINARY) != null
            && cap.get(FirefoxDriver.BINARY) == null) {
          session
              .getRequestedCapabilities()
              .put(
                  FirefoxDriver.BINARY,
                  session.getSlot().getCapabilities().get(FirefoxDriver.BINARY));
        }
      }

      if (BrowserType.CHROME.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get("chrome_binary") != null) {
          JSONObject options = (JSONObject) cap.get(ChromeOptions.CAPABILITY);
          if (options == null) {
            options = new JSONObject();
          }
          try {
            options.put("binary", session.getSlot().getCapabilities().get("chrome_binary"));
          } catch (JSONException e) {
            e.printStackTrace();
          }
          cap.put(ChromeOptions.CAPABILITY, options);
        }
      }

      if (BrowserType.OPERA.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get("opera_binary") != null
            && cap.get("opera.binary") == null) {
          session
              .getRequestedCapabilities()
              .put("opera.binary", session.getSlot().getCapabilities().get("opera_binary"));
        }
      }
    }
  }
예제 #7
0
 public void beforeCommand(
     TestSession session, HttpServletRequest request, HttpServletResponse response) {
   session.put("lastCommand", request.getMethod() + " - " + request.getPathInfo() + " executed.");
 }