This class is NOT working post webdriver upgrade to 2.44, not sure how to fix this issue. Compilation errors on JSONObject.
It is NOT liking "JSONObject" from the below code, and i have highlighted them in red color.
Compilation error -
Multiple markers at this line
- JSONObject cannot be resolved to a type
- The method extractObject(HttpResponse) from the type GetHostName refers to the
missing type JSONObject
- JSONObject cannot be resolved to a type
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.remote.SessionId;
public class GetHostName {
public static String getHostName(
SessionId session) throws UnknownHostException
{
String hostDetail = null;
String hostName ="selgridin001";
int port = 4444;
String errorMsg = "Failed to acquire remote webdriver node and port info. Root cause: ";
try {
HttpHost host = new HttpHost(hostName, port);
DefaultHttpClient client = new DefaultHttpClient();
URL sessionURL = new URL("http://" + hostName + ":" + port + "/grid/api/testsession?session=" + session);
System.out.println("URL is : "+sessionURL);
BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
HttpResponse response = client.execute(host, r);
JSONObject object = extractObject(response);
URL myURL = new URL(object.getString("proxyId"));
if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
hostDetail = myURL.getHost();
}
} catch (Exception e) {
//logger.log(Level.SEVERE, errorMsg, e);
throw new RuntimeException(errorMsg, e);
}
return hostDetail;
}
private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuffer s = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
s.append(line);
}
rd.close();
JSONObject objToReturn = new JSONObject(s.toString());
return objToReturn;
}
}