Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created August 9, 2012 11:56
Show Gist options
  • Save krmahadevan/3303591 to your computer and use it in GitHub Desktop.
Save krmahadevan/3303591 to your computer and use it in GitHub Desktop.
This is a custom proxy for the Grid, which will ensure that all the unattended alerts are cleared before the browser is recycled
package com.test.proxy;
import java.net.URL;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.message.BasicHttpRequest;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;
public class MyProxy extends DefaultRemoteProxy {
private final int MAX_NETWORK_LATENCY = 1000;
public MyProxy(RegistrationRequest arg0, Registry arg1) {
super(arg0, arg1);
}
public void beforeRelease(TestSession session) {
System.out.println(">>>>>>> Session is about to be released");
boolean moreAlertsPresent = true;
do {
moreAlertsPresent = dismissAlerts(session);
System.out.println(">>>>>>> Processing all stale alerts");
} while (moreAlertsPresent);
super.beforeRelease(session);
}
public boolean dismissAlerts(TestSession session) {
TestSlot slot = session.getSlot();
ExternalSessionKey externalKey = session.getExternalKey();
URL remoteURL = slot.getRemoteURL();
HttpRequest request;
switch (slot.getProtocol()) {
//code borrowed from DefaultRemoteProxy.java. Not sure how the URL would be for Selenium protocol.
//Can conk when used with Selenium protocol.
case Selenium:
request = new BasicHttpRequest("POST", remoteURL.toExternalForm() + "/?cmd=testComplete&sessionId="
+ externalKey.getKey());
break;
case WebDriver:
String uri = remoteURL.toString() + "/session/" + externalKey + "/dismiss_alert";
request = new BasicHttpRequest("POST", uri);
break;
default:
throw new GridException("Error, protocol not implemented.");
}
HttpHost host = new HttpHost(remoteURL.getHost(), remoteURL.getPort());
boolean ok;
try {
HttpClient client = getClient(slot);
HttpResponse response = client.execute(host, request);
int code = response.getStatusLine().getStatusCode();
String statusMsg = response.getStatusLine().getReasonPhrase();
ok = (code == 204);
System.out.println("Return code for alert processing = " + code + " reason = " + statusMsg);
} catch (Throwable e) {
ok = false;
System.out.println("Error releasing. Server corrupted ?");
}
return ok;
}
private HttpClient getClient(TestSlot slot) {
Registry reg = slot.getProxy().getRegistry();
int browserTimeout = reg.getConfiguration().getBrowserTimeout();
if (browserTimeout > 0){
final int selenium_server_cleanup_cycle = browserTimeout / 10;
browserTimeout += (selenium_server_cleanup_cycle + MAX_NETWORK_LATENCY);
browserTimeout *=2; // Lets not let this happen too often
}
return slot.getProxy().getHttpClientFactory().getGridHttpClient(browserTimeout);
}
}
@morganwu277
Copy link

it needs a org.openqa.grid.internal.Registry which is for 2.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment