Skip to content

Commit

Permalink
Propagate altered DC to Node
Browse files Browse the repository at this point in the history
Currently the grid logic is such that if a particular
test’s capabilities are altered via an overloaded
version of

* DefaultRemoteProxy#getNewSession
* DefaultRemoteProxy#beforeSession

the expectation is that the altered capabilities
are what gets forwarded to the actual node prior to
an actual session creation on the node. But
this doesn’t happen because the altered desired
capabilities are not being considered.
Fixed this.

Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
  • Loading branch information
krmahadevan authored and shs96c committed Jul 7, 2017
1 parent 392311e commit 86419e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public RequestType getRequestType() {

@Override
public ServletInputStream getInputStream() throws IOException {
setBody(getBody());
return new ServletInputStreamImpl(new ByteArrayInputStream(body));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

package org.openqa.grid.web.servlet.handler;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.Registry;
import org.openqa.selenium.remote.BeanToJsonConverter;
import org.openqa.selenium.remote.JsonToBeanConverter;

import java.util.Map;
Expand All @@ -32,6 +35,9 @@

public class WebDriverRequest extends SeleniumBasedRequest {

private static final String CAPABILITIES = "capabilities";
private static final String DESIRED_CAPABILITIES = "desiredCapabilities";

public WebDriverRequest(HttpServletRequest httpServletRequest, Registry registry) {
super(httpServletRequest, registry);
}
Expand Down Expand Up @@ -65,17 +71,41 @@ public Map<String, Object> extractDesiredCapability() {
JsonObject map = new JsonParser().parse(json).getAsJsonObject();
// Current W3C has required / desired capabilities wrapped in a 'capabilities' object.
// This will need to be updated if/when https://github.com/w3c/webdriver/pull/327 gets merged
if (map.has("capabilities")) {
JsonObject outerCapabilities = map.getAsJsonObject("capabilities");
if (outerCapabilities.has("desiredCapabilities")) {
JsonObject desiredCapabilities = outerCapabilities.getAsJsonObject("desiredCapabilities");
if (map.has(CAPABILITIES)) {
JsonObject outerCapabilities = map.getAsJsonObject(CAPABILITIES);
if (outerCapabilities.has(DESIRED_CAPABILITIES)) {
JsonObject desiredCapabilities = outerCapabilities.getAsJsonObject(DESIRED_CAPABILITIES);
return new JsonToBeanConverter().convert(Map.class, desiredCapabilities);
}
}
JsonObject dc = map.get("desiredCapabilities").getAsJsonObject();
JsonObject dc = map.get(DESIRED_CAPABILITIES).getAsJsonObject();
return new JsonToBeanConverter().convert(Map.class, dc);

} catch (Exception e) {
throw new GridException("Cannot extract a capabilities from the request: " + json, e);
}
}

@Override
public String getBody() {
String json = super.getBody();
try {
Map<String, Object> capsMap = getDesiredCapabilities();
if (capsMap == null) {
return json;
}
JsonObject map = new JsonParser().parse(json).getAsJsonObject();
JsonElement dc = new BeanToJsonConverter().convertObject(capsMap);
if (map.has(CAPABILITIES)) {
map.getAsJsonObject(CAPABILITIES)
.add(DESIRED_CAPABILITIES, new BeanToJsonConverter().convertObject(dc));
} else {
map.add(DESIRED_CAPABILITIES, dc);
}
return new Gson().toJson(map);
} catch (Exception e) {
return json;
}
}

}

0 comments on commit 86419e9

Please sign in to comment.