Search in sources :

Example 1 with SessionId

use of org.openqa.selenium.remote.SessionId in project cerberus-source by cerberustesting.

the class SeleniumServerService method getIPOfNode.

private static void getIPOfNode(TestCaseExecution tCExecution) {
    try {
        Session session = tCExecution.getSession();
        HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver) session.getDriver()).getCommandExecutor();
        SessionId sessionId = ((RemoteWebDriver) session.getDriver()).getSessionId();
        String hostName = ce.getAddressOfRemoteServer().getHost();
        int port = ce.getAddressOfRemoteServer().getPort();
        HttpHost host = new HttpHost(hostName, port);
        HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
        URL sessionURL = new URL(SeleniumServerService.getBaseUrl(session.getHost(), session.getPort()) + "/grid/api/testsession?session=" + sessionId);
        BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
        HttpResponse response = client.execute(host, r);
        if (!response.getStatusLine().toString().contains("403") && !response.getEntity().getContentType().getValue().contains("text/html")) {
            InputStream contents = response.getEntity().getContent();
            StringWriter writer = new StringWriter();
            IOUtils.copy(contents, writer, "UTF8");
            JSONObject object = new JSONObject(writer.toString());
            URL myURL = new URL(object.getString("proxyId"));
            if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
                tCExecution.setIp(myURL.getHost());
                tCExecution.setPort(String.valueOf(myURL.getPort()));
            }
        }
    } catch (IOException ex) {
        LOG.error(ex.toString());
    } catch (JSONException ex) {
        LOG.error(ex.toString());
    }
}
Also used : RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) Point(org.openqa.selenium.Point) URL(java.net.URL) HttpCommandExecutor(org.openqa.selenium.remote.HttpCommandExecutor) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) SessionId(org.openqa.selenium.remote.SessionId) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) Session(org.cerberus.engine.entity.Session) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 HttpHost (org.apache.http.HttpHost)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)1 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)1 Session (org.cerberus.engine.entity.Session)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Point (org.openqa.selenium.Point)1 HttpCommandExecutor (org.openqa.selenium.remote.HttpCommandExecutor)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1 SessionId (org.openqa.selenium.remote.SessionId)1