Skip to content

Commit

Permalink
Optimized DevTools (#7231)
Browse files Browse the repository at this point in the history
Fixed bugs and added test cases for DevTools Log and Console
Improved javadocs
Made use of `fromJson` in all models.
  • Loading branch information
adiohana authored and shs96c committed May 29, 2019
1 parent d123157 commit f70bd37
Show file tree
Hide file tree
Showing 29 changed files with 278 additions and 204 deletions.
10 changes: 10 additions & 0 deletions common/src/web/devToolsConsoleTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
</head>

<body>

<p>Console Test</p>

</body>
</html>
32 changes: 24 additions & 8 deletions java/client/src/org/openqa/selenium/devtools/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,47 @@
import java.util.StringJoiner;

public class Console {
private Console() {
// Models a CDP domain
}

private final static String DOMAIN_NAME = "Console";

public static Command<Void> enable() {
return new Command<>("Console.enable", ImmutableMap.of());
return new Command<>(DOMAIN_NAME + ".enable", ImmutableMap.of());
}

public static Command<Void> disable() {
return new Command<>(DOMAIN_NAME + ".disable", ImmutableMap.of());
}

public static Event<ConsoleMessage> messageAdded() {
return new Event<ConsoleMessage>(
"Console.messageAdded",
map("message", ConsoleMessage.class));
return new Event<>(
DOMAIN_NAME + ".messageAdded",
map("message", ConsoleMessage.class));
}

public static class ConsoleMessage {

private final String source;
private final String level;
private final String text;

public ConsoleMessage(String source, String level, String text) {
ConsoleMessage(String source, String level, String text) {
this.source = Objects.requireNonNull(source);
this.level = Objects.requireNonNull(level);
this.text = Objects.requireNonNull(text);
}

public String getSource() {
return source;
}

public String getLevel() {
return level;
}

public String getText() {
return text;
}

private static ConsoleMessage fromJson(JsonInput input) {
String source = null;
String level = null;
Expand Down
29 changes: 17 additions & 12 deletions java/client/src/org/openqa/selenium/devtools/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.devtools.network.model.MonotonicTime;
import org.openqa.selenium.json.JsonInput;

import java.util.Map;
Expand All @@ -33,21 +34,23 @@

public class Log {

private Log() {
// Models a CDP domain
}
private final static String DOMAIN_NAME = "Log";

public static Command<Void> clear() {
return new Command<>("Log.clear", ImmutableMap.of());
return new Command<>(DOMAIN_NAME + ".clear", ImmutableMap.of());
}

public static Command<Void> enable() {
return new Command<>("Log.enable", ImmutableMap.of());
return new Command<>(DOMAIN_NAME + ".enable", ImmutableMap.of());
}

public static Command<Void> disable() {
return new Command<>(DOMAIN_NAME + ".disable", ImmutableMap.of());
}

public static Event<LogEntry> entryAdded() {
return new Event<>(
"Log.entryAdded",
DOMAIN_NAME + ".entryAdded",
map("entry", LogEntry.class));
}

Expand All @@ -56,9 +59,9 @@ public static class LogEntry {
private final String source;
private final String level;
private final String text;
private final Runtime.Timestamp timestamp;
private final MonotonicTime timestamp;

public LogEntry(String source, String level, String text, Runtime.Timestamp timestamp) {
public LogEntry(String source, String level, String text, MonotonicTime timestamp) {
this.source = Objects.requireNonNull(source);
this.level = Objects.requireNonNull(level);
this.text = Objects.requireNonNull(text);
Expand All @@ -77,7 +80,7 @@ public String getText() {
return text;
}

public Runtime.Timestamp getTimestamp() {
public MonotonicTime getTimestamp() {
return timestamp;
}

Expand All @@ -101,7 +104,9 @@ public org.openqa.selenium.logging.LogEntry asSeleniumLogEntry() {
break;
}

return new org.openqa.selenium.logging.LogEntry(level, getTimestamp().toMillis(), getText());
return new org.openqa.selenium.logging.LogEntry(level,
timestamp.getTimeStamp().toEpochMilli(),
getText());
}

private Map<String, Object> toJson() {
Expand All @@ -116,7 +121,7 @@ private static LogEntry fromJson(JsonInput input) {
String source = null;
String level = null;
String text = null;
Runtime.Timestamp timestamp = null;
MonotonicTime timestamp = null;

input.beginObject();
while (input.hasNext()) {
Expand All @@ -134,7 +139,7 @@ private static LogEntry fromJson(JsonInput input) {
break;

case "timestamp":
timestamp = input.read(Runtime.Timestamp.class);
timestamp = MonotonicTime.parse(input.nextNumber());
break;

default:
Expand Down
84 changes: 42 additions & 42 deletions java/client/src/org/openqa/selenium/devtools/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ public static Command<Void> clearBrowserCookies() {
* If a network fetch occurs as a result which encounters a redirect an additional Network.requestIntercepted event will be sent with the same InterceptionId.
* (EXPERIMENTAL)
*
* @param interceptionId - Identifier for the intercepted request
* @param errorReason (Optional) - If set this causes the request to fail with the given reason.
* @param interceptionId Identifier for the intercepted request
* @param errorReason If set this causes the request to fail with the given reason.
* Passing Aborted for requests marked with isNavigationRequest also cancels the navigation. Must not be set in response to an authChallenge
* @param rawResponse (Optional) - If set the requests completes using with the provided base64 encoded raw response, including HTTP status line and headers etc...
* @param rawResponse If set the requests completes using with the provided base64 encoded raw response, including HTTP status line and headers etc...
* Must not be set in response to an authChallenge
* @param url (Optional) - If set the request url will be modified in a way that's not observable by page. Must not be set in response to an authChallenge
* @param method (Optional) - If set this allows the request method to be overridden. Must not be set in response to an authChallenge
* @param postData (Optional) - If set this allows postData to be set. Must not be set in response to an authChallenge
* @param headers (Optional) - If set this allows the request headers to be changed. Must not be set in response to an authChallenge
* @param authChallengeResponse (Optional) - Response to a requestIntercepted with an authChallenge. Must not be set otherwise
* @param url If set the request url will be modified in a way that's not observable by page. Must not be set in response to an authChallenge
* @param method If set this allows the request method to be overridden. Must not be set in response to an authChallenge
* @param postData If set this allows postData to be set. Must not be set in response to an authChallenge
* @param headers If set this allows the request headers to be changed. Must not be set in response to an authChallenge
* @param authChallengeResponse Response to a requestIntercepted with an authChallenge. Must not be set otherwise
* @return DevTools Command
*/
@Beta
Expand Down Expand Up @@ -117,10 +117,10 @@ public static Command<Void> continueInterceptedRequest(InterceptionId intercepti
/**
* Deletes browser cookies with matching name and url or domain/path pair
*
* @param name - Name of the cookies to remove
* @param url (Optional) - If specified, deletes all the cookies with the given name where domain and path match provided URL
* @param domain (Optional) - If specified, deletes only cookies with the exact domain.
* @param path (Optional) - If specified, deletes only cookies with the exact path
* @param name Name of the cookies to remove
* @param url If specified, deletes all the cookies with the given name where domain and path match provided URL
* @param domain If specified, deletes only cookies with the exact domain.
* @param path If specified, deletes only cookies with the exact path
* @return DevTools Command
*/
public static Command<Void> deleteCookies(String name, Optional<String> url,
Expand Down Expand Up @@ -151,11 +151,11 @@ public static Command<Void> disable() {
/**
* Activates emulation of network conditions.
*
* @param offline - True to emulate internet disconnection.
* @param latency - Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput - Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput - Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param connectionType (Optional) - The underlying connection technology that the browser is supposedly using.
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param connectionType The underlying connection technology that the browser is supposedly using.
* @return DevTools Command
*/
public static Command<Void> emulateNetworkConditions(boolean offline, double latency,
Expand All @@ -180,9 +180,9 @@ public static Command<Void> emulateNetworkConditions(boolean offline, double lat
/**
* Enables network tracking, network events will now be delivered to the client.
*
* @param maxTotalBufferSize (Optional) - Buffer size in bytes to use when preserving network payloads (XHRs, etc).
* @param maxResourceBufferSize (Optional) - Per-resource buffer size in bytes to use when preserving network payloads (XHRs, etc).
* @param maxPostDataSize (Optional) - Longest post body size (in bytes) that would be included in requestWillBeSent notification
* @param maxTotalBufferSize Buffer size in bytes to use when preserving network payloads (XHRs, etc).
* @param maxResourceBufferSize Per-resource buffer size in bytes to use when preserving network payloads (XHRs, etc).
* @param maxPostDataSize Longest post body size (in bytes) that would be included in requestWillBeSent notification
* @return DevTools Command
*/
public static Command<Void> enable(Optional<Integer> maxTotalBufferSize,
Expand Down Expand Up @@ -226,7 +226,7 @@ public static Command<List<String>> getCertificate(String origin) {
/**
* Returns all browser cookies for the current URL. Depending on the backend support, will return detailed cookie information in the cookies field
*
* @param urls (Optional) - The list of URLs for which applicable cookies will be fetched
* @param urls The list of URLs for which applicable cookies will be fetched
* @return Array of cookies
*/
public static Command<Cookies> getCookies(Optional<List<String>> urls) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public static Command<ResponseBody> getResponseBody(RequestId requestId) {
/**
* Returns post data sent with the request. Returns an error when no data was sent with the request.
*
* @param requestId - Identifier of the network request to get content for.
* @param requestId Identifier of the network request to get content for.
* @return DevTools Command with Request body string, omitting files from multipart requests
*/
public static Command<String> getRequestPostData(RequestId requestId) {
Expand All @@ -269,7 +269,7 @@ public static Command<String> getRequestPostData(RequestId requestId) {
/**
* Returns content served for the given currently intercepted request (EXPERIMENTAL)
*
* @param interceptionId - Identifier for the intercepted request to get body for
* @param interceptionId Identifier for the intercepted request to get body for
* @return ResponseBody object
*/
@Beta
Expand All @@ -285,7 +285,7 @@ public static Command<ResponseBody> getResponseBodyForInterception(
* Returns a handle to the stream representing the response body. Note that after this command, the intercepted request can't be continued as is -- you either need to cancel it or to provide the response body.
* The stream only supports sequential read, IO.read will fail if the position is specified (EXPERIMENTAL)
*
* @param interceptionId - Identifier for the intercepted request to get body for
* @param interceptionId Identifier for the intercepted request to get body for
* @return HTTP response body Stream as a String
*/
@Beta
Expand All @@ -298,7 +298,7 @@ public static Command<String> takeResponseBodyForInterceptionAsStream(
}

/**
* @param requestId - Identifier of XHR to replay
* @param requestId Identifier of XHR to replay
* @return - DevTools Command
*/
public static Command<Void> replayXHR(RequestId requestId) {
Expand All @@ -311,10 +311,10 @@ public static Command<Void> replayXHR(RequestId requestId) {
/**
* Searches for given string in response content (EXPERIMENTAL)
*
* @param requestId - Identifier of the network response to search
* @param query - String to search for.
* @param caseSensitive - If true, search is case sensitive
* @param isRegex - If true, treats string parameter as regex
* @param requestId Identifier of the network response to search
* @param query String to search for.
* @param caseSensitive If true, search is case sensitive
* @param isRegex If true, treats string parameter as regex
* @return List of SearchMatch
*/
@Beta
Expand All @@ -340,7 +340,7 @@ public static Command<List<SearchMatch>> searchInResponseBody(RequestId requestI
/**
* Blocks URLs from loading (EXPERIMENTAL)
*
* @param urls - URL patterns to block. Wildcards ('*') are allowed.
* @param urls URL patterns to block. Wildcards ('*') are allowed.
* @return DevTools Command
*/
@Beta
Expand All @@ -352,7 +352,7 @@ public static Command<Void> setBlockedURLs(List<String> urls) {
/**
* Toggles ignoring of service worker for each request. (EXPERIMENTAL)
*
* @param bypass - Bypass service worker and load from network
* @param bypass Bypass service worker and load from network
* @return - DevTools Command
*/
@Beta
Expand All @@ -364,7 +364,7 @@ public static Command<Void> setBypassServiceWorker(boolean bypass) {
/**
* Toggles ignoring cache for each request. If true, cache will not be used.
*
* @param cacheDisabled - Cache disabled state.
* @param cacheDisabled Cache disabled state.
* @return DevTools Command
*/
public static Command<Void> setCacheDisabled(boolean cacheDisabled) {
Expand Down Expand Up @@ -405,9 +405,9 @@ private static Command<Boolean> setCookie(Cookie cookie, Optional<String> url) {
/**
* Sets a cookie with the given cookie data; may overwrite equivalent cookies if they exist
*
* @param cookie - Cookie object where Name and Value are mandatory
* @param url - The request-URI to associate with the setting of the cookie. This value can affect the default domain and path values of the created cookie
* @return - Boolean
* @param cookie Cookie object where Name and Value are mandatory
* @param url The request-URI to associate with the setting of the cookie. This value can affect the default domain and path values of the created cookie
* @return Boolean
*/
public static Command<Boolean> setCookie(org.openqa.selenium.Cookie cookie,
Optional<String> url) {
Expand All @@ -417,8 +417,8 @@ public static Command<Boolean> setCookie(org.openqa.selenium.Cookie cookie,
/**
* (EXPERIMENTAL)
*
* @param maxTotalSize - Maximum total buffer size
* @param maxResourceSize - Maximum per-resource size
* @param maxTotalSize Maximum total buffer size
* @param maxResourceSize Maximum per-resource size
* @return DevTools Command
*/
@Beta
Expand All @@ -430,7 +430,7 @@ public static Command<Void> setDataSizeLimitsForTest(int maxTotalSize, int maxRe
/**
* Specifies whether to always send extra HTTP headers with the requests from this page.
*
* @param headers - Map with extra HTTP headers.
* @param headers Map with extra HTTP headers.
* @return DevTools Command
*/
public static Command<Void> setExtraHTTPHeaders(Map<String, String> headers) {
Expand All @@ -441,7 +441,7 @@ public static Command<Void> setExtraHTTPHeaders(Map<String, String> headers) {
/**
* Sets the requests to intercept that match the provided patterns and optionally resource types (EXPERIMENTAL)
*
* @param patterns - Requests matching any of these patterns will be forwarded and wait for the corresponding continueInterceptedRequest call.
* @param patterns Requests matching any of these patterns will be forwarded and wait for the corresponding continueInterceptedRequest call.
* @return DevTools Command
*/
@Beta
Expand All @@ -454,9 +454,9 @@ public static Command<Void> setRequestInterception(List<RequestPattern> patterns
/**
* Allows overriding user agent with the given string
*
* @param userAgent - User agent to use
* @param acceptLanguage - Browser langugage to emulate
* @param platform - The platform navigator.platform should return
* @param userAgent User agent to use
* @param acceptLanguage Browser langugage to emulate
* @param platform The platform navigator.platform should return
* @return DevTools Command
*/
public static Command<Void> setUserAgentOverride(String userAgent,
Expand Down
Loading

0 comments on commit f70bd37

Please sign in to comment.