Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,10 @@ private void sendChromeRequest(ChromeRequest request) {
}

private <T> T parseChromeResponse(ChromeResponse response, TypeReference<T> valueType) {
// Most methods return a single element of data. To eliminate the user needing to access this
// single element via a pass through method, we skip the root node and map the element's data right
// into the data structure we want (i.e. we don't parse the root node itself).
//
// e.g. { "result" : { "browserContextId" : "some_id" } }
// ^--- ignore ^--- consume directly so user can act directly on string
//
// Allows the user to do `callingMethod()` instead of `callingMethod().getBrowserContextId()`.
//
// If this fails, then the method is one of the few cases where there are multiple
// results that need to be mapped into a parent data structure.
//
// e.g. { "result" : { "protocolVersion" : "1.2.3" }, { "jsVersion" : "6.6.8" } }
// |__________________________________________________________|
// `--------- must consume all so user can select which element to work with
//
// Here the user must do `callingMethod().getProtocolVersion()` or `someMethod().getJsVersion()`.
Iterator<JsonNode> elements = response.getResult().elements();
JsonNode first = elements.next();
try {
// We do our best to predict which kind of result to consume the response as, but there's
// a small chance that a multi-result response has optional, absent members, and we try and
// fail to parse it as a single-result response, which is why we catch the inner JsonMappingException.
if (elements.hasNext()) {
return objectMapper.readValue(response.getResult().toString(), valueType);
} else {
return objectMapper.readValue(objectMapper.treeAsTokens(first), valueType);
}
} catch (JsonMappingException e) {
try {
return objectMapper.readValue(response.getResult().toString(), valueType);
} catch (IOException e1) {
throw new ChromeDevToolsException(e1);
}
} catch (IOException e2) {
throw new ChromeDevToolsException(e2);
return objectMapper.readValue(response.getResult().toString(), valueType);
} catch (IOException | NullPointerException e) {
throw new ChromeDevToolsException(e);
}
}

Expand Down Expand Up @@ -298,7 +266,7 @@ public NavigateResult navigate(String url) {
}

public String getUrl() {
return getDOM().getDocument(null, null).getDocumentURL();
return getDOM().getDocument(null, null).root.getDocumentURL();
}

public EvaluateResult evaluate(String javascript) {
Expand All @@ -323,11 +291,13 @@ public EvaluateResult evaluate(String javascript) {
}

public FrameId getFrameId() {
return getDOM().getDocument(null, null).getFrameId();
return getDOM().getDocument(null, null).root.getFrameId();
}

public List<NodeId> getNodeIds(String selector) {
return getDOM().querySelectorAll(getDOM().getDocument(1, null).getNodeId(), selector);
return getDOM()
.querySelectorAll(getDOM().getDocument(1, null).root.getNodeId(), selector)
.nodeIds;
}

public NodeId getNodeId(String selector) {
Expand All @@ -341,7 +311,8 @@ public byte[] captureScreenshot() {

public byte[] captureScreenshot(FileExtension extension) {
String data = getPage()
.captureScreenshot(extension.name().toLowerCase(), null, null, null, null);
.captureScreenshot(extension.name().toLowerCase(), null, null, null, null)
.data;
return Base64.getDecoder().decode(data);
}

Expand Down Expand Up @@ -537,31 +508,31 @@ public Object getValueFromObjectId(RemoteObjectId remoteObjectId, String propert

public RemoteObjectId getObjectId(String selector) {
DOM dom = getDOM();
NodeId root = dom.getDocument(null, null).getNodeId();
NodeId root = dom.getDocument(null, null).root.getNodeId();
if (root == null) {
return null;
}
NodeId selectedNodeId = dom.querySelector(root, selector);
NodeId selectedNodeId = dom.querySelector(root, selector).nodeId;
if (selectedNodeId == null) {
return null;
}
RemoteObject remoteObject = dom.resolveNode(selectedNodeId, null, null, null);
RemoteObject remoteObject = dom.resolveNode(selectedNodeId, null, null, null).object;
if (remoteObject == null) {
return null;
}
return remoteObject.getObjectId();
}

public String getLocation() {
return getDOM().getDocument(null, null).getDocumentURL();
return getDOM().getDocument(null, null).root.getDocumentURL();
}

public boolean click(String selector) {
NodeId selectedNodeId = getNodeId(selector);
if (selectedNodeId == null) {
return false;
}
BoxModel boxModel = getDOM().getBoxModel(selectedNodeId, null, null);
BoxModel boxModel = getDOM().getBoxModel(selectedNodeId, null, null).model;
if (boxModel == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static void main(String[] args) {
// Locate the element on the page
BoxModel boxModel = session
.getDOM()
.getBoxModel(session.getNodeId(cssSelector), null, null);
.getBoxModel(session.getNodeId(cssSelector), null, null)
.model;
int width = boxModel.getWidth();
int height = boxModel.getHeight(); // includes shadows

Expand All @@ -58,7 +59,8 @@ public static void main(String[] args) {
// Get the screenshot data as a base 64 encoded string
String base64Data = session
.getPage()
.captureScreenshot("png", null, clip, null, null);
.captureScreenshot("png", null, clip, null, null)
.data;
byte[] data = Base64.getDecoder().decode(base64Data);

// Alternatively use the client shortcut to capture either a PNG or PDF screenshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,7 @@ private Optional<TypeSpec> maybeBuildContainerForMultipleReturns(
// Sometimes chrome will return multiple objects. This creates classes that will
// encapsulate those types into a single class.
List<Property> returnValues = command.getReturns().orElse(Collections.emptyList());
if (returnValues.size() > 1) {
return Optional.of(buildContainerForMultipleReturns(command, domain, returnValues));
}
return Optional.empty();
return Optional.of(buildContainerForMultipleReturns(command, domain, returnValues));
}

private TypeSpec buildContainerForMultipleReturns(
Expand Down Expand Up @@ -777,12 +774,11 @@ private MethodSpec generateMethodSpec(
packageName,
returnTypeName.get()
);
TypeName valueType = returnValues.size() == 1
? getTypeName(returnValues.get(0), packageName)
: ClassName.get(
classPackageResolver.getPackageName(),
classPackageResolver.getClassName()
);

TypeName valueType = ClassName.get(
classPackageResolver.getPackageName(),
classPackageResolver.getClassName()
);

TypeName returnType = valueType;
if (async) {
Expand Down