├── .gitignore ├── LICENSE ├── README.md ├── chrome.png ├── firefox.png ├── index.html ├── pom.xml ├── script.js ├── src └── test │ ├── java │ └── com │ │ └── aerokube │ │ └── selenoid │ │ └── ChromeDevtoolsTest.java │ └── resources │ └── log4j.properties └── style.css /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | test*.png 4 | target 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Apache License 190 | Version 2.0, January 2004 191 | http://www.apache.org/licenses/ 192 | 193 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 194 | 195 | 1. Definitions. 196 | 197 | "License" shall mean the terms and conditions for use, reproduction, 198 | and distribution as defined by Sections 1 through 9 of this document. 199 | 200 | "Licensor" shall mean the copyright owner or entity authorized by 201 | the copyright owner that is granting the License. 202 | 203 | "Legal Entity" shall mean the union of the acting entity and all 204 | other entities that control, are controlled by, or are under common 205 | control with that entity. For the purposes of this definition, 206 | "control" means (i) the power, direct or indirect, to cause the 207 | direction or management of such entity, whether by contract or 208 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 209 | outstanding shares, or (iii) beneficial ownership of such entity. 210 | 211 | "You" (or "Your") shall mean an individual or Legal Entity 212 | exercising permissions granted by this License. 213 | 214 | "Source" form shall mean the preferred form for making modifications, 215 | including but not limited to software source code, documentation 216 | source, and configuration files. 217 | 218 | "Object" form shall mean any form resulting from mechanical 219 | transformation or translation of a Source form, including but 220 | not limited to compiled object code, generated documentation, 221 | and conversions to other media types. 222 | 223 | "Work" shall mean the work of authorship, whether in Source or 224 | Object form, made available under the License, as indicated by a 225 | copyright notice that is included in or attached to the work 226 | (an example is provided in the Appendix below). 227 | 228 | "Derivative Works" shall mean any work, whether in Source or Object 229 | form, that is based on (or derived from) the Work and for which the 230 | editorial revisions, annotations, elaborations, or other modifications 231 | represent, as a whole, an original work of authorship. For the purposes 232 | of this License, Derivative Works shall not include works that remain 233 | separable from, or merely link (or bind by name) to the interfaces of, 234 | the Work and Derivative Works thereof. 235 | 236 | "Contribution" shall mean any work of authorship, including 237 | the original version of the Work and any modifications or additions 238 | to that Work or Derivative Works thereof, that is intentionally 239 | submitted to Licensor for inclusion in the Work by the copyright owner 240 | or by an individual or Legal Entity authorized to submit on behalf of 241 | the copyright owner. For the purposes of this definition, "submitted" 242 | means any form of electronic, verbal, or written communication sent 243 | to the Licensor or its representatives, including but not limited to 244 | communication on electronic mailing lists, source code control systems, 245 | and issue tracking systems that are managed by, or on behalf of, the 246 | Licensor for the purpose of discussing and improving the Work, but 247 | excluding communication that is conspicuously marked or otherwise 248 | designated in writing by the copyright owner as "Not a Contribution." 249 | 250 | "Contributor" shall mean Licensor and any individual or Legal Entity 251 | on behalf of whom a Contribution has been received by Licensor and 252 | subsequently incorporated within the Work. 253 | 254 | 2. Grant of Copyright License. Subject to the terms and conditions of 255 | this License, each Contributor hereby grants to You a perpetual, 256 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 257 | copyright license to reproduce, prepare Derivative Works of, 258 | publicly display, publicly perform, sublicense, and distribute the 259 | Work and such Derivative Works in Source or Object form. 260 | 261 | 3. Grant of Patent License. Subject to the terms and conditions of 262 | this License, each Contributor hereby grants to You a perpetual, 263 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 264 | (except as stated in this section) patent license to make, have made, 265 | use, offer to sell, sell, import, and otherwise transfer the Work, 266 | where such license applies only to those patent claims licensable 267 | by such Contributor that are necessarily infringed by their 268 | Contribution(s) alone or by combination of their Contribution(s) 269 | with the Work to which such Contribution(s) was submitted. If You 270 | institute patent litigation against any entity (including a 271 | cross-claim or counterclaim in a lawsuit) alleging that the Work 272 | or a Contribution incorporated within the Work constitutes direct 273 | or contributory patent infringement, then any patent licenses 274 | granted to You under this License for that Work shall terminate 275 | as of the date such litigation is filed. 276 | 277 | 4. Redistribution. You may reproduce and distribute copies of the 278 | Work or Derivative Works thereof in any medium, with or without 279 | modifications, and in Source or Object form, provided that You 280 | meet the following conditions: 281 | 282 | (a) You must give any other recipients of the Work or 283 | Derivative Works a copy of this License; and 284 | 285 | (b) You must cause any modified files to carry prominent notices 286 | stating that You changed the files; and 287 | 288 | (c) You must retain, in the Source form of any Derivative Works 289 | that You distribute, all copyright, patent, trademark, and 290 | attribution notices from the Source form of the Work, 291 | excluding those notices that do not pertain to any part of 292 | the Derivative Works; and 293 | 294 | (d) If the Work includes a "NOTICE" text file as part of its 295 | distribution, then any Derivative Works that You distribute must 296 | include a readable copy of the attribution notices contained 297 | within such NOTICE file, excluding those notices that do not 298 | pertain to any part of the Derivative Works, in at least one 299 | of the following places: within a NOTICE text file distributed 300 | as part of the Derivative Works; within the Source form or 301 | documentation, if provided along with the Derivative Works; or, 302 | within a display generated by the Derivative Works, if and 303 | wherever such third-party notices normally appear. The contents 304 | of the NOTICE file are for informational purposes only and 305 | do not modify the License. You may add Your own attribution 306 | notices within Derivative Works that You distribute, alongside 307 | or as an addendum to the NOTICE text from the Work, provided 308 | that such additional attribution notices cannot be construed 309 | as modifying the License. 310 | 311 | You may add Your own copyright statement to Your modifications and 312 | may provide additional or different license terms and conditions 313 | for use, reproduction, or distribution of Your modifications, or 314 | for any such Derivative Works as a whole, provided Your use, 315 | reproduction, and distribution of the Work otherwise complies with 316 | the conditions stated in this License. 317 | 318 | 5. Submission of Contributions. Unless You explicitly state otherwise, 319 | any Contribution intentionally submitted for inclusion in the Work 320 | by You to the Licensor shall be under the terms and conditions of 321 | this License, without any additional terms or conditions. 322 | Notwithstanding the above, nothing herein shall supersede or modify 323 | the terms of any separate license agreement you may have executed 324 | with Licensor regarding such Contributions. 325 | 326 | 6. Trademarks. This License does not grant permission to use the trade 327 | names, trademarks, service marks, or product names of the Licensor, 328 | except as required for reasonable and customary use in describing the 329 | origin of the Work and reproducing the content of the NOTICE file. 330 | 331 | 7. Disclaimer of Warranty. Unless required by applicable law or 332 | agreed to in writing, Licensor provides the Work (and each 333 | Contributor provides its Contributions) on an "AS IS" BASIS, 334 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 335 | implied, including, without limitation, any warranties or conditions 336 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 337 | PARTICULAR PURPOSE. You are solely responsible for determining the 338 | appropriateness of using or redistributing the Work and assume any 339 | risks associated with Your exercise of permissions under this License. 340 | 341 | 8. Limitation of Liability. In no event and under no legal theory, 342 | whether in tort (including negligence), contract, or otherwise, 343 | unless required by applicable law (such as deliberate and grossly 344 | negligent acts) or agreed to in writing, shall any Contributor be 345 | liable to You for damages, including any direct, indirect, special, 346 | incidental, or consequential damages of any character arising as a 347 | result of this License or out of the use or inability to use the 348 | Work (including but not limited to damages for loss of goodwill, 349 | work stoppage, computer failure or malfunction, or any and all 350 | other commercial damages or losses), even if such Contributor 351 | has been advised of the possibility of such damages. 352 | 353 | 9. Accepting Warranty or Additional Liability. While redistributing 354 | the Work or Derivative Works thereof, You may choose to offer, 355 | and charge a fee for, acceptance of support, warranty, indemnity, 356 | or other liability obligations and/or rights consistent with this 357 | License. However, in accepting such obligations, You may act only 358 | on Your own behalf and on Your sole responsibility, not on behalf 359 | of any other Contributor, and only if You agree to indemnify, 360 | defend, and hold each Contributor harmless for any liability 361 | incurred by, or claims asserted against, such Contributor by reason 362 | of your accepting any such warranty or additional liability. 363 | 364 | END OF TERMS AND CONDITIONS 365 | 366 | APPENDIX: How to apply the Apache License to your work. 367 | 368 | To apply the Apache License to your work, attach the following 369 | boilerplate notice, with the fields enclosed by brackets "{}" 370 | replaced with your own identifying information. (Don't include 371 | the brackets!) The text should be enclosed in the appropriate 372 | comment syntax for the file format. We also recommend that a 373 | file or class name and description of purpose be included on the 374 | same "printed page" as the copyright notice for easier 375 | identification within third-party archives. 376 | 377 | Copyright 2020+ Aerokube Software OÜ 378 | 379 | Licensed under the Apache License, Version 2.0 (the "License"); 380 | you may not use this file except in compliance with the License. 381 | You may obtain a copy of the License at 382 | 383 | http://www.apache.org/licenses/LICENSE-2.0 384 | 385 | Unless required by applicable law or agreed to in writing, software 386 | distributed under the License is distributed on an "AS IS" BASIS, 387 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 388 | See the License for the specific language governing permissions and 389 | limitations under the License. 390 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chrome Devtools Protocol Java Example 2 | 3 | This example demonstrates how to work with [Chrome Devtools Protocol](https://chromedevtools.github.io/devtools-protocol/) with Java. 4 | 5 | ``` 6 | $ mvn clean test 7 | ``` 8 | -------------------------------------------------------------------------------- /chrome.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aerokube/chrome-developer-tools-protocol-java-example/d9854f5b3541245740e92f61fb8f98e723b5ad52/chrome.png -------------------------------------------------------------------------------- /firefox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aerokube/chrome-developer-tools-protocol-java-example/d9854f5b3541245740e92f61fb8f98e723b5ad52/firefox.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chrome Developer Tools Example Webapp 6 | 7 | 8 | 9 | 10 | 11 |

It works!

12 | Chrome 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.aerokube.selenoid 8 | cdp-java-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 17 13 | 4.1.4 14 | 5.0.0 15 | 5.8.2 16 | UTF-8 17 | 2.0.0-alpha7 18 | 19 | 20 | 21 | 22 | org.seleniumhq.selenium 23 | selenium-remote-driver 24 | ${selenium.version} 25 | test 26 | 27 | 28 | io.github.sskorol 29 | cdt-java-client 30 | ${cdt-java-client.version} 31 | test 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | ${junit-jupiter.version} 37 | test 38 | 39 | 40 | org.slf4j 41 | slf4j-reload4j 42 | ${slf4j-log4j12.version} 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-source-plugin 52 | 3.2.1 53 | 54 | 55 | 56 | jar 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-compiler-plugin 64 | 3.10.1 65 | 66 | ${java.version} 67 | ${java.version} 68 | 69 | 70 | 71 | org.apache.maven.plugins 72 | maven-surefire-plugin 73 | 3.0.0-M6 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | console.log("Welcome to Chrome Developer Tools Example Webapp!"); 2 | 3 | console.error("An error has occurred"); 4 | 5 | throw "Exception: something is broken..."; 6 | -------------------------------------------------------------------------------- /src/test/java/com/aerokube/selenoid/ChromeDevtoolsTest.java: -------------------------------------------------------------------------------- 1 | package com.aerokube.selenoid; 2 | 3 | import io.github.sskorol.cdt.protocol.commands.Runtime; 4 | import io.github.sskorol.cdt.protocol.commands.*; 5 | import io.github.sskorol.cdt.protocol.types.css.RuleUsage; 6 | import io.github.sskorol.cdt.protocol.types.css.SourceRange; 7 | import io.github.sskorol.cdt.protocol.types.dom.BoxModel; 8 | import io.github.sskorol.cdt.protocol.types.dom.RGBA; 9 | import io.github.sskorol.cdt.protocol.types.fetch.RequestPattern; 10 | import io.github.sskorol.cdt.protocol.types.network.Request; 11 | import io.github.sskorol.cdt.protocol.types.network.Response; 12 | import io.github.sskorol.cdt.protocol.types.overlay.HighlightConfig; 13 | import io.github.sskorol.cdt.protocol.types.page.Viewport; 14 | import io.github.sskorol.cdt.services.ChromeDevToolsService; 15 | import org.junit.jupiter.api.AfterEach; 16 | import org.junit.jupiter.api.BeforeEach; 17 | import org.junit.jupiter.api.Test; 18 | import org.openqa.selenium.remote.DesiredCapabilities; 19 | import org.openqa.selenium.remote.RemoteWebDriver; 20 | 21 | import java.io.FileOutputStream; 22 | import java.net.URL; 23 | import java.nio.file.Files; 24 | import java.nio.file.Paths; 25 | import java.util.Base64; 26 | import java.util.Collections; 27 | import java.util.List; 28 | import java.util.Map; 29 | import java.util.concurrent.ConcurrentHashMap; 30 | import java.util.concurrent.CountDownLatch; 31 | import java.util.stream.Collectors; 32 | 33 | import static io.github.sskorol.cdt.protocol.types.network.ErrorReason.FAILED; 34 | import static io.github.sskorol.cdt.protocol.types.page.CaptureScreenshotFormat.PNG; 35 | import static java.lang.String.format; 36 | 37 | class ChromeDevtoolsTest { 38 | 39 | private static final String URL = "https://aerokube.com/chrome-developer-tools-protocol-java-example/"; 40 | 41 | private RemoteWebDriver driver; 42 | private ChromeDevToolsService devtools; 43 | 44 | @BeforeEach 45 | void setUp() throws Exception { 46 | // Init standard Selenium session 47 | DesiredCapabilities caps = new DesiredCapabilities(); 48 | caps.setBrowserName("chrome"); 49 | driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps); 50 | 51 | // Init ChromeDevtools client 52 | devtools = ChromeDevToolsService.from(format("ws://localhost:4444/devtools/%s/page", driver.getSessionId())); 53 | } 54 | 55 | @AfterEach 56 | void tearDown() { 57 | if (devtools != null && !devtools.isClosed()) { 58 | devtools.close(); 59 | devtools = null; 60 | } 61 | if (driver != null) { 62 | driver.quit(); 63 | driver = null; 64 | } 65 | } 66 | 67 | @Test 68 | void testTakeScreenshot() throws Exception { 69 | Page page = devtools.getPage(); 70 | navigate(page); 71 | takeScreenshot(page, "testScreenshot.png"); 72 | } 73 | 74 | private static void navigate(Page page) throws InterruptedException { 75 | navigate(page, URL); 76 | } 77 | 78 | private static void navigate(Page page, String url) throws InterruptedException { 79 | page.enable(); 80 | CountDownLatch countDownLatch = new CountDownLatch(1); 81 | page.onLoadEventFired(e -> countDownLatch.countDown()); 82 | page.navigate(url); 83 | countDownLatch.await(); 84 | } 85 | 86 | private static void takeScreenshot(Page page, String fileName) throws Exception { 87 | String encodedScreenshot = page.captureScreenshot(); 88 | saveScreenshot(fileName, encodedScreenshot); 89 | } 90 | 91 | private static void takeScreenshot(Page page, String fileName, Viewport viewport) throws Exception { 92 | String encodedScreenshot = page.captureScreenshot(PNG, 100, viewport, true, true); 93 | saveScreenshot(fileName, encodedScreenshot); 94 | } 95 | 96 | private static void saveScreenshot(String fileName, String bytes) throws Exception { 97 | try (FileOutputStream fos = new FileOutputStream(fileName)) { 98 | fos.write(Base64.getDecoder().decode(bytes)); 99 | fos.flush(); 100 | } 101 | } 102 | 103 | @Test 104 | void testPrintNetworkRequests() throws Exception { 105 | Network network = devtools.getNetwork(); 106 | Map requests = new ConcurrentHashMap<>(); 107 | network.onRequestWillBeSent( 108 | e -> requests.put(e.getRequestId(), e.getRequest()) 109 | ); 110 | network.onResponseReceived(e -> { 111 | Request request = requests.get(e.getRequestId()); 112 | if (request != null) { 113 | Response response = e.getResponse(); 114 | Double duration = response.getTiming() != null ? response.getTiming().getReceiveHeadersEnd() : 0d; 115 | System.out.printf("%s %s %f ms\n", request.getMethod(), request.getUrl(), duration); 116 | } 117 | }); 118 | network.onLoadingFailed(e -> { 119 | // E.g. mixed-content and CORS requests blocked by browser 120 | Request request = requests.get(e.getRequestId()); 121 | if (request != null) { 122 | System.out.printf("%s %s %s\n", request.getMethod(), request.getUrl(), e.getBlockedReason().name()); 123 | } 124 | }); 125 | network.enable(); 126 | 127 | Page page = devtools.getPage(); 128 | navigate(page); 129 | } 130 | 131 | @Test 132 | void testAbortNetworkRequests() throws Exception { 133 | Fetch fetch = devtools.getFetch(); 134 | fetch.onRequestPaused( 135 | e -> fetch.failRequest(e.getRequestId(), FAILED) 136 | ); 137 | RequestPattern requestPattern = new RequestPattern(); 138 | requestPattern.setUrlPattern("*chrome.png"); 139 | fetch.enable(Collections.singletonList(requestPattern), true); 140 | 141 | Page page = devtools.getPage(); 142 | navigate(page); 143 | 144 | takeScreenshot(page, "testAbortNetworkRequest.png"); 145 | } 146 | 147 | @Test 148 | void testMockNetworkRequests() throws Exception { 149 | byte[] fileContents = Files.readAllBytes(Paths.get("firefox.png")); 150 | String encodedFileContents = new String(Base64.getEncoder().encode(fileContents)); 151 | Fetch fetch = devtools.getFetch(); 152 | fetch.onRequestPaused( 153 | e -> fetch.fulfillRequest( 154 | e.getRequestId(), 155 | 200, 156 | Collections.emptyList(), 157 | "application/octet-stream", 158 | encodedFileContents, 159 | "OK" 160 | ) 161 | ); 162 | RequestPattern requestPattern = new RequestPattern(); 163 | requestPattern.setUrlPattern("*chrome.png"); 164 | fetch.enable(Collections.singletonList(requestPattern), true); 165 | 166 | Page page = devtools.getPage(); 167 | navigate(page); 168 | 169 | takeScreenshot(page, "testMockNetworkRequest.png"); 170 | } 171 | 172 | @Test 173 | void testPrintConsoleMessages() throws Exception { 174 | Runtime runtime = devtools.getRuntime(); 175 | runtime.onConsoleAPICalled( 176 | e -> System.out.printf( 177 | "%s %s\n", 178 | e.getType(), 179 | e.getArgs().stream() 180 | .map(a -> String.valueOf(a.getValue())) 181 | .collect(Collectors.joining("; ")) 182 | ) 183 | ); 184 | runtime.onExceptionThrown( 185 | e -> System.out.println( 186 | e.getExceptionDetails().getException().getValue() 187 | ) 188 | ); 189 | runtime.enable(); 190 | 191 | Page page = devtools.getPage(); 192 | navigate(page); 193 | } 194 | 195 | @Test 196 | void testDOMManipulation() throws Exception { 197 | DOM dom = devtools.getDOM(); 198 | dom.enable(); 199 | 200 | Page page = devtools.getPage(); 201 | navigate(page); 202 | 203 | Integer img = dom.querySelector(dom.getDocument().getNodeId(), "img[alt=Chrome]"); 204 | dom.setOuterHTML(img, "

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed velit et sem pharetra sagittis. Sed enim erat, finibus et elementum a, rhoncus at elit.

"); 205 | takeScreenshot(page, "testUpdateDOM.png"); 206 | } 207 | 208 | @Test 209 | void testElementScreenshot() throws Exception { 210 | DOM dom = devtools.getDOM(); 211 | dom.enable(); 212 | 213 | Page page = devtools.getPage(); 214 | navigate(page); 215 | 216 | Integer img = dom.querySelector(dom.getDocument().getNodeId(), "img[alt=Chrome]"); 217 | BoxModel boxModel = dom.getBoxModel(img, 0, ""); 218 | List imgCoords = boxModel.getContent(); 219 | Viewport elementViewport = viewport(imgCoords.get(0), imgCoords.get(1), boxModel.getWidth(), boxModel.getHeight()); 220 | takeScreenshot(page, "testElementScreenshot.png", elementViewport); 221 | } 222 | 223 | private static Viewport viewport(double x, double y, double width, double height) { 224 | Viewport viewport = new Viewport(); 225 | viewport.setX(x); 226 | viewport.setY(y); 227 | viewport.setScale(1d); 228 | viewport.setWidth(width); 229 | viewport.setHeight(height); 230 | return viewport; 231 | } 232 | 233 | @Test 234 | void testCSSCoverage() throws Exception { 235 | DOM dom = devtools.getDOM(); 236 | dom.enable(); 237 | 238 | CSS css = devtools.getCSS(); 239 | css.enable(); 240 | 241 | Page page = devtools.getPage(); 242 | navigate(page); 243 | 244 | css.startRuleUsageTracking(); 245 | 246 | List ruleUsages = css.stopRuleUsageTracking(); 247 | ruleUsages.forEach(r -> { 248 | String styleSheetText = css.getStyleSheetText(r.getStyleSheetId()); 249 | String usedRule = styleSheetText.substring( 250 | r.getStartOffset().intValue(), 251 | r.getEndOffset().intValue() 252 | ); 253 | System.out.println(usedRule); 254 | }); 255 | } 256 | 257 | @Test 258 | void testAddCSSRule() throws Exception { 259 | DOM dom = devtools.getDOM(); 260 | dom.enable(); 261 | 262 | SourceRange sourceRange = new SourceRange(); 263 | sourceRange.setStartLine(0); 264 | sourceRange.setStartColumn(0); 265 | sourceRange.setEndLine(0); 266 | sourceRange.setEndColumn(0); 267 | 268 | CSS css = devtools.getCSS(); 269 | css.onStyleSheetAdded( 270 | e -> css.addRule( 271 | e.getHeader().getStyleSheetId(), 272 | "h1 {color: red;}", 273 | sourceRange 274 | ) 275 | ); 276 | css.enable(); 277 | 278 | Page page = devtools.getPage(); 279 | navigate(page); 280 | 281 | takeScreenshot(page, "testAddCSSRule.png"); 282 | } 283 | 284 | @Test 285 | void testCSSMediaPrint() throws Exception { 286 | Emulation emulation = devtools.getEmulation(); 287 | emulation.setEmulatedMedia("print", Collections.emptyList()); 288 | 289 | Page page = devtools.getPage(); 290 | navigate(page); 291 | 292 | takeScreenshot(page, "testCSSMediaPrint.png"); 293 | } 294 | 295 | @Test 296 | void testUserAgentOverride() throws Exception { 297 | Page page = devtools.getPage(); 298 | navigate(page, "https://google.com/"); 299 | takeScreenshot(page, "testOriginalGoogleCom.png"); 300 | 301 | Emulation emulation = devtools.getEmulation(); 302 | String iPhoneUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) CriOS/61.0.3163.73 Mobile/14G60 Safari/602.1"; 303 | emulation.setUserAgentOverride(iPhoneUserAgent, "zh", "iOS", null); 304 | 305 | navigate(page, "https://google.com/"); 306 | takeScreenshot(page, "testUserAgentOverride.png"); 307 | } 308 | 309 | @Test 310 | void testGeolocationOverride() throws Exception { 311 | Emulation emulation = devtools.getEmulation(); 312 | emulation.setGeolocationOverride(37.774929, -122.419416, 0d); 313 | 314 | Page page = devtools.getPage(); 315 | navigate(page, "https://browserleaks.com/geo"); 316 | takeScreenshot(page, "testGeolocationOverride.png"); 317 | } 318 | 319 | @Test 320 | void testHideScrollbars() throws Exception { 321 | Page page = devtools.getPage(); 322 | navigate(page, "https://cnet.com/news/"); 323 | takeScreenshot(page, "testPageWithScrollbars.png"); 324 | 325 | Emulation emulation = devtools.getEmulation(); 326 | emulation.setScrollbarsHidden(true); 327 | 328 | navigate(page, "https://cnet.com/news/"); 329 | takeScreenshot(page, "testPageWithoutScrollbars.png"); 330 | } 331 | 332 | @Test 333 | void testHighlightNode() throws Exception { 334 | Page page = devtools.getPage(); 335 | navigate(page); 336 | 337 | DOM dom = devtools.getDOM(); 338 | Integer h1 = dom.querySelector(dom.getDocument().getNodeId(), "h1"); 339 | 340 | Overlay overlay = devtools.getOverlay(); 341 | overlay.enable(); 342 | overlay.highlightNode(highlightConfig(), h1, null, null, null); 343 | 344 | takeScreenshot(page, "testHighlightNode.png"); 345 | } 346 | 347 | private static HighlightConfig highlightConfig() { 348 | HighlightConfig highlightConfig = new HighlightConfig(); 349 | highlightConfig.setBorderColor(rgba(255, 229, 153, 0.66)); 350 | highlightConfig.setContentColor(rgba(111, 168, 220, 0.66)); 351 | highlightConfig.setCssGridColor(rgba(75, 0, 130, 0)); 352 | highlightConfig.setEventTargetColor(rgba(255, 196, 196, 0.66)); 353 | highlightConfig.setMarginColor(rgba(246, 178, 107, 0.66)); 354 | highlightConfig.setPaddingColor(rgba(147, 196, 125, 0.55)); 355 | highlightConfig.setShapeColor(rgba(96, 82, 117, 0.8)); 356 | highlightConfig.setShapeMarginColor(rgba(96, 82, 127, 0.6)); 357 | highlightConfig.setShowExtensionLines(true); 358 | highlightConfig.setShowInfo(true); 359 | highlightConfig.setShowRulers(true); 360 | highlightConfig.setShowStyles(false); 361 | return highlightConfig; 362 | } 363 | 364 | private static RGBA rgba(int r, int g, int b, double a) { 365 | RGBA result = new RGBA(); 366 | result.setR(r); 367 | result.setG(g); 368 | result.setB(b); 369 | result.setA(a); 370 | return result; 371 | } 372 | 373 | } 374 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # suppress inspection "UnusedProperty" for whole file 2 | log4j.rootLogger=INFO, out 3 | 4 | # CONSOLE appender not used by default 5 | log4j.appender.out=org.apache.log4j.ConsoleAppender 6 | log4j.appender.out.layout=org.apache.log4j.PatternLayout 7 | log4j.appender.out.layout.ConversionPattern=%d %-5p %c{1} - %m%n 8 | log4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer 9 | 10 | log4j.logger.org.openqa.selenium.remote = ERROR 11 | log4j.logger.com.github.kklisura.cdt.services = ERROR 12 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | text-align: center; 3 | } 4 | 5 | @media print { 6 | body { 7 | text-decoration: underline; 8 | } 9 | } 10 | 11 | /* This is an unused rule */ 12 | h2 { 13 | color: green; 14 | } 15 | --------------------------------------------------------------------------------