Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network methods #8

Merged
merged 2 commits into from
May 16, 2019
Merged
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
16 changes: 13 additions & 3 deletions java/client/src/org/openqa/selenium/devtools/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openqa.selenium.devtools.network.events.SignedExchangeReceived;
import org.openqa.selenium.devtools.network.events.WebSocketClosed;
import org.openqa.selenium.devtools.network.events.WebSocketCreated;
import org.openqa.selenium.devtools.network.events.WebSocketFrame;
import org.openqa.selenium.devtools.network.events.WebSocketFrameError;
import org.openqa.selenium.devtools.network.types.AuthChallengeResponse;
import org.openqa.selenium.devtools.network.types.ConnectionType;
Expand Down Expand Up @@ -504,8 +505,17 @@ public static Event<WebSocketClosed> webSocketClosed(){
return new Event<>(domainName+".webSocketClosed",map("requestId", WebSocketClosed.class));
}

//TODO: @GED add events for Network.webSocketClosed, Network.webSocketCreated, Network.webSocketFrameError
//TODO: @GED Network.webSocketFrameReceived, Network.webSocketFrameSent, Network.webSocketHandshakeResponseReceived, Network.webSocketWillSendHandshakeRequest
/**
*Fired when WebSocket message is received.
*/
public static Event<WebSocketFrame> webSocketFrameReceived(){
return new Event<>(domainName+".webSocketFrameReceived",map("requestId", WebSocketFrame.class));
}

//TODO @GED Add test Network.requestIntercepted,
/**
* Fired when WebSocket message is sent.
*/
public static Event<WebSocketFrame> webSocketFrameSent(){
return new Event<>(domainName+".webSocketFrameSent",map("requestId", WebSocketFrame.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.openqa.selenium.devtools.network.events;

import org.openqa.selenium.devtools.network.types.MonotonicTime;
import org.openqa.selenium.devtools.network.types.RequestId;
import org.openqa.selenium.json.JsonInput;

/**
* WebSocket message data. This represents an entire WebSocket message, not just a fragmented frame as the name suggests.
*/
public class WebSocketFrame {

/**
* Request identifier.
*/
private final RequestId requestId;

/**
* timestamp
*/
private final MonotonicTime timestamp;
/**
* WebSocket response data.
*/
private final org.openqa.selenium.devtools.network.types.WebSocketFrame response;

public WebSocketFrame(RequestId requestId,
MonotonicTime timestamp,
org.openqa.selenium.devtools.network.types.WebSocketFrame response) {
this.requestId = requestId;
this.timestamp = timestamp;
this.response = response;
}

public static WebSocketFrame fromJson(JsonInput input){
RequestId requestId = new RequestId(input.nextString());
MonotonicTime timestamp = null;
org.openqa.selenium.devtools.network.types.WebSocketFrame response = null;

while (input.hasNext()){
switch (input.nextName()){
case "timestamp":
timestamp = MonotonicTime.parse(input.nextNumber());
break;
case "response":
response = org.openqa.selenium.devtools.network.types.WebSocketFrame.parse(input);
break;
default:
input.skipValue();
break;
}
}
return new WebSocketFrame(requestId, timestamp, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.openqa.selenium.devtools.network.types;

import org.openqa.selenium.json.JsonInput;

/**
* @author dratler
* WebSocket message data. This represents an entire WebSocket message, not just a fragmented frame as the name suggests.
*/
public class WebSocketFrame {

/**
* WebSocket message opcode.
*/
private Number opcode;
/**
* WebSocket message mask.
*/
private boolean mask;

/**
* WebSocket message payload data. If the opcode is 1, this is a text message and payloadData is a UTF-8 string. If the opcode isn't 1, then payloadData is a base64 encoded string representing binary data.
*/
private String payloadData;

public static WebSocketFrame parse(JsonInput input){
WebSocketFrame webSocketFrame = new WebSocketFrame();
while (input.hasNext()){
switch (input.nextName()){
case "opcode":
webSocketFrame.setOpcode(input.nextNumber());
break;
case "mask" :
webSocketFrame.setMask(input.nextBoolean());
break;
case "payloadData" :
webSocketFrame.setPayloadData(input.nextString());
break;
default:
input.skipValue();
break;
}
}
return webSocketFrame;
}

public Number getOpcode() {
return opcode;
}

public void setOpcode(Number opcode) {
this.opcode = opcode;
}

public boolean isMask() {
return mask;
}

public void setMask(boolean mask) {
this.mask = mask;
}

public String getPayloadData() {
return payloadData;
}

public void setPayloadData(String payloadData) {
this.payloadData = payloadData;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.openqa.selenium.devtools.network.Network.webSocketClosed;
import static org.openqa.selenium.devtools.network.Network.webSocketCreated;
import static org.openqa.selenium.devtools.network.Network.webSocketFrameError;
import static org.openqa.selenium.devtools.network.Network.webSocketFrameReceived;
import static org.openqa.selenium.devtools.network.Network.webSocketFrameSent;

import org.junit.Assert;
import org.junit.FixMethodOrder;
Expand All @@ -28,12 +30,14 @@
import org.openqa.selenium.devtools.network.events.ResponseReceived;
import org.openqa.selenium.devtools.network.events.WebSocketClosed;
import org.openqa.selenium.devtools.network.events.WebSocketCreated;
import org.openqa.selenium.devtools.network.events.WebSocketFrame;
import org.openqa.selenium.devtools.network.events.WebSocketFrameError;
import org.openqa.selenium.devtools.network.types.BlockedReason;
import org.openqa.selenium.devtools.network.types.ConnectionType;
import org.openqa.selenium.devtools.network.types.ResourceType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -264,4 +268,34 @@ public void accept(WebSocketClosed webSocketClosed) {
chromeDriver.navigate().to(TEST_WEB_SITE_ADDRESS);
devTools.close();
}

//TODO - make this test to work
@Test
public void testWebSocketFrameReceived(){
devTools.send(enable(Optional.empty(),Optional.empty(),Optional.empty()));
chromeDriver.navigate().to(TEST_WEB_SITE_ADDRESS);
devTools.send(setBlockedURLs(Arrays.asList("*://*.css")));
devTools.addListener(webSocketFrameReceived(), new Consumer<WebSocketFrame>() {
@Override
public void accept(WebSocketFrame webSocketFrame) {
System.out.println("WebSocketRecived :" + webSocketFrame);
}
});
chromeDriver.navigate().to(TEST_WEB_SITE_ADDRESS);
}

//TODO - make this test to work
@Test
public void testWebSocketFrameSent(){
devTools.send(enable(Optional.empty(),Optional.empty(),Optional.empty()));
chromeDriver.navigate().to(TEST_WEB_SITE_ADDRESS);
devTools.send(setBlockedURLs(Arrays.asList("*://*.css")));
devTools.addListener(webSocketFrameSent(), new Consumer<WebSocketFrame>() {
@Override
public void accept(WebSocketFrame webSocketFrame) {
System.out.println("WebSocketRecived :" + webSocketFrame);
}
});
devTools.send(setBlockedURLs(Arrays.asList("*://*.css")));
}
}