├── .gitattributes
├── .gitignore
├── GIPHY CAPTURE.zip
├── README.md
├── data
├── cat.png
└── durian.png
├── driver
├── chromedriver
└── chromedriver.exe
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── sahajamit
│ │ ├── Exceptions
│ │ └── MessageTimeOutException.java
│ │ ├── messaging
│ │ ├── CDPClient.java
│ │ ├── Message.java
│ │ ├── MessageBuilder.java
│ │ └── ServiceWorker.java
│ │ └── utils
│ │ ├── SSLUtil.java
│ │ ├── UINotificationService.java
│ │ ├── UIUtils.java
│ │ └── Utils.java
└── resources
│ └── log4j.properties
└── test
├── java
└── com
│ └── sahajamit
│ ├── ClipboardTests.java
│ ├── DemoTests.java
│ └── NotificationTests.java
└── resources
└── log4j.properties
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.gz filter=lfs diff=lfs merge=lfs -text
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | *.iml
3 | target
--------------------------------------------------------------------------------
/GIPHY CAPTURE.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahajamit/chrome-devtools-webdriver-integration/231ba34e54b09cd1439c88f2d897f476faf71ed4/GIPHY CAPTURE.zip
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This is the demo code for the following blogs:
2 |
3 | https://medium.com/@sahajamit/selenium-chrome-dev-tools-makes-a-perfect-browser-automation-recipe-c35c7f6a2360
4 |
5 | https://medium.com/@sahajamit/automated-testing-of-web-push-notifications-ffc8a2d85e3c
6 |
7 | https://medium.com/@sahajamit/can-selenium-chrome-dev-tools-recipe-works-inside-a-docker-container-afff92e9cce5
8 |
9 | https://medium.com/@sahajamit/getting-clipboard-content-from-remote-selenium-chrome-nodes-67a4c4d862bd
--------------------------------------------------------------------------------
/data/cat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahajamit/chrome-devtools-webdriver-integration/231ba34e54b09cd1439c88f2d897f476faf71ed4/data/cat.png
--------------------------------------------------------------------------------
/data/durian.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahajamit/chrome-devtools-webdriver-integration/231ba34e54b09cd1439c88f2d897f476faf71ed4/data/durian.png
--------------------------------------------------------------------------------
/driver/chromedriver:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahajamit/chrome-devtools-webdriver-integration/231ba34e54b09cd1439c88f2d897f476faf71ed4/driver/chromedriver
--------------------------------------------------------------------------------
/driver/chromedriver.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahajamit/chrome-devtools-webdriver-integration/231ba34e54b09cd1439c88f2d897f476faf71ed4/driver/chromedriver.exe
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.sahajamit
8 | webdriver-chrome-dev-tools
9 | 1.0-SNAPSHOT
10 |
11 | src/main/java
12 | src/test/java
13 |
14 |
15 | resources
16 |
17 |
18 |
19 |
20 | org.apache.maven.plugins
21 | maven-compiler-plugin
22 |
23 | 8
24 | 8
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | org.seleniumhq.selenium
34 | selenium-java
35 | 3.7.1
36 |
37 |
38 | commons-io
39 | commons-io
40 | 2.6
41 |
42 |
43 | com.jayway.jsonpath
44 | json-path
45 | 2.4.0
46 |
47 |
48 | com.jayway.jsonpath
49 | json-path-assert
50 | 2.4.0
51 |
52 |
53 | org.json
54 | json
55 | 20160810
56 |
57 |
58 | com.neovisionaries
59 | nv-websocket-client
60 | 2.8
61 |
62 |
63 | commons-io
64 | commons-io
65 | 2.6
66 |
67 |
68 | com.google.code.gson
69 | gson
70 | 2.8.5
71 |
72 |
73 | junit
74 | junit
75 | 4.12
76 | test
77 |
78 |
79 | org.slf4j
80 | slf4j-api
81 | 1.7.5
82 |
83 |
84 | org.slf4j
85 | slf4j-log4j12
86 | 1.7.5
87 |
88 |
89 | com.fasterxml.jackson.core
90 | jackson-core
91 | 2.9.8
92 |
93 |
94 | com.fasterxml.jackson.core
95 | jackson-databind
96 | 2.9.8
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/Exceptions/MessageTimeOutException.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.Exceptions;
2 |
3 | public class MessageTimeOutException extends Exception{
4 | public MessageTimeOutException() {
5 | super();
6 | }
7 | public MessageTimeOutException(String message) {
8 | super(message);
9 | }
10 | public MessageTimeOutException(String message, Throwable cause) {
11 | super(message,cause);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/messaging/CDPClient.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.messaging;
2 |
3 | import com.jayway.jsonpath.DocumentContext;
4 | import com.jayway.jsonpath.JsonPath;
5 | import com.neovisionaries.ws.client.WebSocket;
6 | import com.neovisionaries.ws.client.WebSocketAdapter;
7 | import com.neovisionaries.ws.client.WebSocketException;
8 | import com.neovisionaries.ws.client.WebSocketFactory;
9 | import com.sahajamit.Exceptions.MessageTimeOutException;
10 | import com.sahajamit.utils.SSLUtil;
11 | import com.sahajamit.utils.Utils;
12 | import org.json.JSONArray;
13 | import org.json.JSONException;
14 | import org.json.JSONObject;
15 |
16 | import java.io.IOException;
17 | import java.util.Base64;
18 | import java.util.Objects;
19 | import java.util.concurrent.*;
20 |
21 | public class CDPClient {
22 | private String wsUrl;
23 | private WebSocket ws = null;
24 | private WebSocketFactory factory;
25 | private BlockingQueue blockingQueue = new LinkedBlockingDeque(100000);
26 | public CDPClient(String wsURL){
27 | factory = new WebSocketFactory();
28 | SSLUtil.turnOffSslChecking(factory);
29 | factory.setVerifyHostname(false);
30 | this.wsUrl = wsURL;
31 | }
32 |
33 | private void connect() throws IOException, WebSocketException {
34 | if(Objects.isNull(ws)){
35 | System.out.println("Making the new WS connection to: " + wsUrl);
36 | ws = factory
37 | .createSocket(wsUrl)
38 | .addListener(new WebSocketAdapter() {
39 | @Override
40 | public void onTextMessage(WebSocket ws, String message) {
41 | // Received a response. Print the received message.
42 | System.out.println("Received this ws message: "+message);
43 | blockingQueue.add(message);
44 | }
45 | })
46 | .connect();
47 | }
48 | }
49 |
50 | public void sendMessage(String message) throws IOException, WebSocketException {
51 | if(Objects.isNull(ws))
52 | this.connect();
53 | System.out.println("Sending this ws message: " + message);
54 | ws.sendText(message);
55 | }
56 |
57 | public String getResponseMessage(String jsonPath, String expectedValue) throws InterruptedException {
58 | while(true){
59 | String message = blockingQueue.poll(5, TimeUnit.SECONDS);
60 | if(Objects.isNull(message))
61 | return null;
62 | DocumentContext parse = JsonPath.parse(message);
63 | String value = parse.read(jsonPath.trim()).toString();
64 | if(value.equalsIgnoreCase(expectedValue))
65 | return message;
66 | }
67 | }
68 |
69 | public String getResponseMessage(String methodName) throws InterruptedException {
70 | return getResponseMessage(methodName,5);
71 | }
72 |
73 | public String getResponseMessage(String methodName, int timeoutInSecs) throws InterruptedException {
74 | try{
75 | while(true){
76 | String message = blockingQueue.poll(timeoutInSecs, TimeUnit.SECONDS);
77 | if(Objects.isNull(message))
78 | throw new RuntimeException(String.format("No message received with this method name : '%s'",methodName));
79 | JSONObject jsonObject = new JSONObject(message);
80 | try{
81 | String method = jsonObject.getString("method");
82 | if(method.equalsIgnoreCase(methodName)){
83 | return message;
84 | }
85 | }catch (JSONException e){
86 | //do nothing
87 | }
88 | }
89 | }catch (Exception e1){
90 | throw e1;
91 | }
92 | }
93 |
94 | public String getResponseBodyMessage(int id) throws InterruptedException {
95 | try{
96 | while(true){
97 | String message = blockingQueue.poll(5, TimeUnit.SECONDS);
98 | if(Objects.isNull(message))
99 | throw new RuntimeException(String.format("No message received with this id : '%s'",id));
100 | JSONObject jsonObject = new JSONObject(message);
101 | try{
102 | int methodId = jsonObject.getInt("id");
103 | if(id == methodId){
104 | return jsonObject.getJSONObject("result").getString("body");
105 | }
106 | }catch (JSONException e){
107 | //do nothing
108 | }
109 | }
110 | }catch (Exception e1){
111 | throw e1;
112 | }
113 | }
114 |
115 | public String getResponseDataMessage(int id) throws Exception {
116 | return getResponseMessage(id,"data");
117 | }
118 |
119 | public String getResponseMessage(int id, String dataType) throws InterruptedException, MessageTimeOutException {
120 | while(true){
121 | String message = blockingQueue.poll(10, TimeUnit.SECONDS);
122 | if(Objects.isNull(message))
123 | throw new MessageTimeOutException(String.format("No message received with this id : '%s'",id));
124 | JSONObject jsonObject = new JSONObject(message);
125 | try{
126 | int methodId = jsonObject.getInt("id");
127 | if(id == methodId){
128 | return jsonObject.getJSONObject("result").getString(dataType);
129 | }
130 | }catch (JSONException e){
131 | //do nothing
132 | }
133 | }
134 | }
135 |
136 | public void mockResponse(String mockMessage){
137 | new Thread(() -> {
138 | try{
139 | String message = this.getResponseMessage("Network.requestIntercepted",5);
140 | JSONObject jsonObject = new JSONObject(message);
141 | String interceptionId = jsonObject.getJSONObject("params").getString("interceptionId");
142 | int id = Utils.getInstance().getDynamicID();
143 | this.sendMessage(MessageBuilder.buildGetContinueInterceptedRequestMessage(id,interceptionId,mockMessage));
144 | return;
145 | }catch (Exception e){
146 | //do nothing
147 | }
148 | }).start();
149 | }
150 |
151 | public void mockFunResponse(String encodedMessage){
152 | new Thread(() -> {
153 | try{
154 | while(true){
155 | String message = this.getResponseMessage("Network.requestIntercepted",10);
156 | JSONObject jsonObject = new JSONObject(message);
157 | String interceptionId = jsonObject.getJSONObject("params").getString("interceptionId");
158 | // int id1 = Utils.getInstance().getDynamicID();
159 | // this.sendMessage(MessageBuilder.buildGetResponseBodyForInterceptionMessage(id1,interceptionId));
160 | // String interceptedResponse = this.getResponseBodyMessage(id1);
161 | int id = Utils.getInstance().getDynamicID();
162 | this.sendMessage(MessageBuilder.buildGetContinueInterceptedRequestEncodedMessage(id,interceptionId,encodedMessage));
163 | }
164 | }catch (Exception e){
165 | //do nothing
166 | }
167 | }).start();
168 | }
169 |
170 | public ServiceWorker getServiceWorker(String workerURL, int timeoutInSecs, String expectedStatus) throws InterruptedException {
171 | while(true){
172 | String message = getResponseMessage("ServiceWorker.workerVersionUpdated",timeoutInSecs);
173 | if(Objects.isNull(message))
174 | return null;
175 | JSONObject jsonObject = new JSONObject(message);
176 | JSONArray jsonArray = jsonObject.getJSONObject("params").getJSONArray("versions");
177 | try{
178 | String scriptURL = jsonArray.getJSONObject(0).getString("scriptURL");
179 | String status = jsonArray.getJSONObject(0).getString("status");
180 | if(scriptURL.contains(workerURL) && status.equalsIgnoreCase(expectedStatus)){
181 | String targetId = jsonArray.getJSONObject(0).getString("targetId");
182 | String versionId = jsonArray.getJSONObject(0).getString("versionId");
183 | String registrationId = jsonArray.getJSONObject(0).getString("registrationId");
184 | String runningStatus = jsonArray.getJSONObject(0).getString("registrationId");
185 | ServiceWorker serviceWorker = new ServiceWorker(versionId,registrationId,targetId);
186 | serviceWorker.setRunningStatus(runningStatus);
187 | serviceWorker.setStatus(status);
188 | return serviceWorker;
189 | }
190 | }catch (Exception e){
191 | //do nothing
192 | }
193 | }
194 | }
195 |
196 | public void disconnect(){
197 | ws.disconnect();
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/messaging/Message.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.messaging;
2 |
3 | import com.google.gson.Gson;
4 | import org.json.JSONObject;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 | import java.util.Objects;
9 |
10 | public class Message {
11 | private int id;
12 | private String method;
13 | private Map params;
14 |
15 | public Message(int id, String method) {
16 | this.id = id;
17 | this.method = method;
18 | }
19 |
20 | public void addParam(String key, Object value){
21 | if(Objects.isNull(params))
22 | params = new HashMap<>();
23 | params.put(key,value);
24 | }
25 |
26 | public String toJson(){
27 | Gson gson = new Gson();
28 | return gson.toJson(this);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/messaging/MessageBuilder.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.messaging;
2 |
3 | import com.sahajamit.utils.Utils;
4 | import org.apache.commons.codec.binary.Base64;
5 | import org.json.JSONObject;
6 |
7 | public class MessageBuilder {
8 | public static String buildGeoLocationMessage(int id, double latitude, double longitude){
9 | Message msg = new Message(id,"Emulation.setGeolocationOverride");
10 | msg.addParam("latitude",latitude);
11 | msg.addParam("longitude",longitude);
12 | msg.addParam("accuracy",100);
13 | String message = msg.toJson();
14 | // String message = String.format("{\"id\":%s,\"method\":\"Emulation.setGeolocationOverride\",\"params\":{\"latitude\":%s,\"longitude\":%s,\"accuracy\":100}}",id,latitude,longitude);
15 | return message;
16 | }
17 |
18 | public static String buildGetResponseBodyMessage(int id, String requestID){
19 | String message = String.format("{\"id\":%s,\"method\":\"Network.getResponseBody\",\"params\":{\"requestId\":\"%s\"}}",id,requestID);
20 | return message;
21 | }
22 |
23 | public static String buildNetWorkEnableMessage(int id){
24 | String message = String.format("{\"id\":%s,\"method\":\"Network.enable\",\"params\":{\"maxTotalBufferSize\":10000000,\"maxResourceBufferSize\":5000000}}",id);
25 | return message;
26 | }
27 |
28 | public static String buildRequestInterceptorPatternMessage(int id, String pattern, String resourceType){
29 | String message = String.format("{\"id\":%s,\"method\":\"Network.setRequestInterception\",\"params\":{\"patterns\":[{\"urlPattern\":\"%s\",\"resourceType\":\"%s\",\"interceptionStage\":\"HeadersReceived\"}]}}",id,pattern,resourceType);
30 | return message;
31 | }
32 |
33 | public static String buildGetResponseBodyForInterceptionMessage(int id, String interceptionId){
34 | String message = String.format("{\"id\":%s,\"method\":\"Network.getResponseBodyForInterception\",\"params\":{\"interceptionId\":\"%s\"}}",id,interceptionId);
35 | return message;
36 | }
37 |
38 | public static String buildGetContinueInterceptedRequestMessage(int id, String interceptionId, String response){
39 | String encodedResponse = new String(Base64.encodeBase64(response.getBytes()));
40 | String message = String.format("{\"id\":%s,\"method\":\"Network.continueInterceptedRequest\",\"params\":{\"interceptionId\":\"%s\",\"rawResponse\":\"%s\"}}",id,interceptionId,encodedResponse);
41 | return message;
42 | }
43 |
44 | public static String buildGetContinueInterceptedRequestEncodedMessage(int id, String interceptionId, String encodedResponse){
45 | String message = String.format("{\"id\":%s,\"method\":\"Network.continueInterceptedRequest\",\"params\":{\"interceptionId\":\"%s\",\"rawResponse\":\"%s\"}}",id,interceptionId,encodedResponse);
46 | return message;
47 | }
48 |
49 | public static String buildServiceWorkerEnableMessage(int id){
50 | String message = String.format("{\"id\":%s,\"method\":\"ServiceWorker.enable\"}",id);
51 | return message;
52 | }
53 |
54 | public static String buildServiceWorkerInspectMessage(int id, String versionId){
55 | String message = String.format("{\"id\":%s,\"method\":\"ServiceWorker.inspectWorker\",\"params\":{\"versionId\":\"%s\"}}",id,"versionId");
56 | return message;
57 | }
58 |
59 | public static String buildEnableLogMessage(int id){
60 | String message = String.format("{\"id\":%d,\"method\":\"Log.enable\"}",id);
61 | return message;
62 | }
63 |
64 | public static String buildEnableRuntimeMessage(int id){
65 | String message = String.format("{\"id\":%d,\"method\":\"Runtime.enable\"}",id);
66 | return message;
67 | }
68 |
69 | public static String buildSendPushNotificationMessage(int id, String origin, String registrationId, String data){
70 | String message = String.format("{\"id\":%s,\"method\":\"ServiceWorker.deliverPushMessage\",\"params\":{\"origin\":\"%s\",\"registrationId\":\"%s\",\"data\":\"%s\"}}",id,origin,registrationId,data);
71 | return message;
72 | }
73 |
74 | public static String buildObserveBackgroundServiceMessage(int id){
75 | String message = String.format("{\"id\":%s,\"method\":\"BackgroundService.startObserving\",\"params\":{\"service\":\"%s\"}}",id,"pushMessaging");
76 | return message;
77 | }
78 |
79 | public static String buildGetBrowserContextMessage(int id){
80 | String message = String.format("{\"id\":%d,\"method\":\"Target.getBrowserContexts\"}",id);
81 | return message;
82 | }
83 |
84 | public static String buildClearBrowserCacheMessage(int id){
85 | String message = String.format("{\"id\":%d,\"method\":\"Network.clearBrowserCache\"}",id);
86 | return message;
87 | }
88 |
89 | public static String buildClearBrowserCookiesMessage(int id){
90 | String message = String.format("{\"id\":%d,\"method\":\"Network.clearBrowserCookies\"}",id);
91 | return message;
92 | }
93 |
94 | public static String buildClearDataForOriginMessage(int id, String url){
95 | String message = String.format("{\"id\":%s,\"method\":\"Storage.clearDataForOrigin\",\"params\":{\"origin\":\"%s\",\"storageTypes\":\"all\"}}",id,url);
96 | return message;
97 | }
98 |
99 | public static String buildTakeElementScreenShotMessage(int id, long x, long y, long height, long width, int scale){
100 | String message = String.format("{\"id\":%s,\"method\":\"Page.captureScreenshot\",\"params\":{\"clip\":{\"x\":%s,\"y\":%s,\"width\":%s,\"height\":%s,\"scale\":%s}}}",id,x,y,width,height,scale);
101 | return message;
102 | }
103 | public static String buildTakePageScreenShotMessage(int id){
104 | Message msg = new Message(id,"Page.captureScreenshot");
105 | String message = msg.toJson();
106 | // String message = String.format("{\"id\":%s,\"method\":\"Page.captureScreenshot\"}",id);
107 | return message;
108 | }
109 |
110 |
111 |
112 | private String buildRequestInterceptorEnabledMessage(){
113 | String message = String.format("{\"id\":4,\"method\":\"Network.setRequestInterception\",\"params\":{\"enabled\":true}}");
114 | System.out.println(message);
115 | return message;
116 | }
117 |
118 |
119 | private String buildBasicHttpAuthenticationMessage(String username,String password){
120 | byte[] encodedBytes = Base64.encodeBase64(String.format("%s:%s",username,password).getBytes());
121 | String base64EncodedCredentials = new String(encodedBytes);
122 | String message = String.format("{\"id\":2,\"method\":\"Network.setExtraHTTPHeaders\",\"params\":{\"headers\":{\"Authorization\":\"Basic %s\"}}}",base64EncodedCredentials);
123 | System.out.println(message);
124 | return message;
125 | }
126 |
127 |
128 |
129 | private String buildSendObservingPushMessage(){
130 | int id = Utils.getInstance().getDynamicID();
131 | String message = String.format("{\"id\":%d,\"method\":\"BackgroundService.clearEvents\",\"params\":{\"service\":\"backgroundFetch\"}}",id);
132 | System.out.println(message);
133 | return message;
134 | }
135 |
136 |
137 |
138 | private String buildAttachToTargetMessage(String targetId){
139 | int id = Utils.getInstance().getDynamicID();
140 | String message = String.format("{\"id\":%d,\"method\":\"Target.attachToTarget\",\"params\":{\"targetId\":\"%s\"}}",id,targetId);
141 | System.out.println(message);
142 | return message;
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/messaging/ServiceWorker.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.messaging;
2 |
3 | public class ServiceWorker {
4 | private String versionId;
5 | private String registrationId;
6 | private String targetId;
7 | private String status;
8 | private String runningStatus;
9 |
10 | public ServiceWorker(String versionId, String registrationId, String targetId) {
11 | this.versionId = versionId;
12 | this.registrationId = registrationId;
13 | this.targetId = targetId;
14 | }
15 |
16 | public String getVersionId() {
17 | return versionId;
18 | }
19 |
20 | public String getRegistrationId() {
21 | return registrationId;
22 | }
23 |
24 | public String getTargetId() {
25 | return targetId;
26 | }
27 |
28 | public String getStatus() {
29 | return status;
30 | }
31 |
32 | public void setStatus(String status) {
33 | this.status = status;
34 | }
35 |
36 | public String getRunningStatus() {
37 | return runningStatus;
38 | }
39 |
40 | public void setRunningStatus(String runningStatus) {
41 | this.runningStatus = runningStatus;
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "ServiceWorker{" +
47 | "versionId='" + versionId + '\'' +
48 | ", registrationId='" + registrationId + '\'' +
49 | ", targetId='" + targetId + '\'' +
50 | ", status='" + status + '\'' +
51 | ", runningStatus='" + runningStatus + '\'' +
52 | '}';
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/utils/SSLUtil.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.utils;
2 |
3 | import com.neovisionaries.ws.client.WebSocketFactory;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import javax.net.ssl.HttpsURLConnection;
8 | import javax.net.ssl.SSLContext;
9 | import javax.net.ssl.TrustManager;
10 | import javax.net.ssl.X509TrustManager;
11 | import java.security.KeyManagementException;
12 | import java.security.NoSuchAlgorithmException;
13 | import java.security.cert.X509Certificate;
14 | import java.util.HashMap;
15 | import java.util.Map;
16 |
17 | public class SSLUtil {
18 | private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[]{
19 | new X509TrustManager() {
20 | public X509Certificate[] getAcceptedIssuers(){
21 | return null;
22 | }
23 | public void checkClientTrusted( X509Certificate[] certs, String authType ){}
24 | public void checkServerTrusted( X509Certificate[] certs, String authType ){}
25 | }
26 | };
27 |
28 | public static void turnOffSslChecking() {
29 | try{
30 | // Install the all-trusting trust manager
31 | final SSLContext sc = SSLContext.getInstance("SSL");
32 | sc.init( null, UNQUESTIONING_TRUST_MANAGER, null );
33 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
34 | }catch(Exception e){
35 | System.out.println("Error in SSL Utils");
36 | }
37 | }
38 |
39 | public static void turnOffSslChecking(WebSocketFactory factory) {
40 | try{
41 | // Install the all-trusting trust manager
42 | final SSLContext sc = SSLContext.getInstance("SSL");
43 | sc.init( null, UNQUESTIONING_TRUST_MANAGER, null );
44 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
45 | factory.setSSLContext(sc);
46 | }catch(Exception e){
47 | System.out.println("Error in SSL Utils");
48 | }
49 | }
50 |
51 | public static void turnOnSslChecking() throws KeyManagementException, NoSuchAlgorithmException {
52 | // Return it to the initial state (discovered by reflection, now hardcoded)
53 | SSLContext.getInstance("SSL").init( null, null, null );
54 | }
55 |
56 | private SSLUtil(){
57 | throw new UnsupportedOperationException( "Do not instantiate libraries.");
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/com/sahajamit/utils/UINotificationService.java:
--------------------------------------------------------------------------------
1 | package com.sahajamit.utils;
2 |
3 | import org.openqa.selenium.JavascriptExecutor;
4 | import org.openqa.selenium.WebDriver;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Map;
10 | import java.util.concurrent.atomic.AtomicBoolean;
11 | import java.util.concurrent.atomic.AtomicInteger;
12 |
13 | public class UINotificationService {
14 | private static final Logger logger = LoggerFactory.getLogger(UINotificationService.class);
15 | private WebDriver driver;
16 | private static UINotificationService ourInstance = new UINotificationService();
17 | private static final String startWebNotificationJsScript = "window.notifications = []; window.DefaultNotification = window.Notification; (function () { function notificationCallback(title, opt) { console.log(\"notification title: \", title); console.log(\"notification body: \", opt.body); console.log(\"notification tag: \", opt.tag); console.log(\"notification icon: \", opt.icon); } const handler = { construct(target, args) { notificationCallback(...args); var notification = new target(...args); window.notifications.push(notification); return notification; } }; const ProxifiedNotification = new Proxy(Notification, handler); window.Notification = ProxifiedNotification; })();";
18 | private static final String stopWebNotificationJsScript = "window.notifications = []; window.Notification = window.DefaultNotification;";
19 |
20 | private static final String startPushNotificationJsScript = "window.notificationsMap = Object.create(null); async function getServiceWorkerRegistration(){ window.myServiceWorkerRegistration = await navigator.serviceWorker.getRegistration(\"%s\"); return window.myServiceWorkerRegistration;}; async function getNotifications() { window.myNotifications = await window.myServiceWorkerRegistration.getNotifications();}; window.notificationListener = setInterval(async function() { console.log(\"checking for notifications...\"); await getServiceWorkerRegistration(); await getNotifications(); for(var key in window.myNotifications){ window.notificationsMap[window.myNotifications[key].tag] = window.myNotifications[key]; }; }, 2000);";
21 | private static final String stopPushNotificationJsScript = "clearInterval(window.notificationListener); window.notificationsMap = Object.create(null); window.notifications = [] ;";
22 | private static final String getPushNotificationsJsScript = "function getCollectedNotifications(){ window.notifications = [] ; var count = 0; for (var prop in window.notificationsMap) { count++; window.notifications.push(window.notificationsMap[prop]); } console.log(\"Total notifications count is: \" + count); return window.notifications;}; return getCollectedNotifications();";
23 |
24 | public static UINotificationService getInstance(WebDriver driver) {
25 | ourInstance.driver = driver;
26 | return ourInstance;
27 | }
28 |
29 | private UINotificationService() {
30 | }
31 |
32 | public void startWebNotificationListener(){
33 | UIUtils.getInstance().executeJavaScript(startWebNotificationJsScript);
34 | }
35 |
36 | public void startPushNotificationListener(String notificationServiceURL){
37 | UIUtils.getInstance().executeJavaScript(String.format(startPushNotificationJsScript,notificationServiceURL));
38 | }
39 |
40 | public void stopWebNotificationListener(){
41 | UIUtils.getInstance().executeJavaScript(stopWebNotificationJsScript);
42 | }
43 | public void stopPushNotificationListener(){
44 | UIUtils.getInstance().executeJavaScript(stopPushNotificationJsScript);
45 | }
46 |
47 |
48 | public boolean isNotificationPresent(Map filter, String notificationType){
49 | ArrayList