Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 58f5455
Showing
6 changed files
with
14,740 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
**/bin/** | ||
**/binaries/** | ||
**/.settings | ||
**/.classpath | ||
**/.project | ||
**/.gitignore | ||
**/work/** | ||
**/target/** | ||
**/*.log | ||
**/src/**/*.jar | ||
**/build/** | ||
**/repo/** | ||
**/test-output/** | ||
/pom.xml.releaseBackup | ||
/release.properties | ||
/settings.xml | ||
*.unc-backup~ | ||
*.uncrustify | ||
*~ | ||
temp.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014,2017 Serguei Kouzmine | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
### Info | ||
|
||
The project practices Java Selenium 4.0 beta release [ChromiumDriver](https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/chromium/ChromiumDriver.java) for executing the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/) (cdp) commands (a entirely different set of API communicated to the Chrome browser family via `POST` requests to `/session/$sessionId/goog/cdp/execute` with API-specific payload) feature. | ||
|
||
### Examples | ||
|
||
#### Override User Agent | ||
|
||
One can call cdp protocol to invoke [setUserAgentOverride](https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setUserAgentOverride) method and dynmically modify the `user-agent` header during the test: | ||
|
||
```java | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chromium.ChromiumDriver; | ||
|
||
ChromiumDriver driver = new ChromeDriver(); | ||
driver.get("https://www.whoishostingthis.com/tools/user-agent/"); | ||
By locator = By.cssSelector(".user-agent"); | ||
WebElement element = driver.findElement(locato); | ||
assertThat(element.getAttribute("innerText"), containsString("Mozilla")); | ||
Map<String, Object> params = new HashMap<String, Object>(); | ||
params.put("userAgent", "python 2.7"); | ||
params.put("platform", "Windows"); | ||
driver.executeCdpCommand("Network.setUserAgentOverride", params); | ||
driver.navigate().refresh(); | ||
sleep(100); | ||
|
||
element = driver.findElement(locator); | ||
assertThat(element.isDisplayed(), is(true)); | ||
assertThat(element.getAttribute("innerText"), is("python 2.7")); | ||
|
||
``` | ||
demonstrates that the user-agent is indeed changing | ||
#### Cookies | ||
The example shows alternative API to collect the cookies available to page Javascript | ||
```java | ||
Map<String, Object> result = driver.executeCdpCommand("Page.getCookies", new HashMap<String, Object>()); | ||
ArrayList<Map<String, Object>> cookies = (ArrayList<Map<String, Object>>) result.get("cookies"); | ||
cookies.stream().limit(100).map(o -> o.keySet()).forEach(System.err::println); | ||
``` | ||
#### Capture Screenshot | ||
```java | ||
String result = driver.executeCdpCommand("Page.captureScreenshot", new HashMap<>()); | ||
String data = (String) result.get("data"); | ||
byte[] image = new (Base64()).decode(data); | ||
assertThat(ImageIO.read(new ByteArrayInputStream(image)).getWidth(), greaterThan(0)); | ||
(new FileOutputStream("temp.png")).write(image); | ||
``` | ||
|
||
|
||
Note: some CDP API notably `Page.printToPDF` are not curently implemented: | ||
```sh | ||
unhandled inspector error: {"code":-32000,"message":"PrintToPDF is not implemented"}(..) | ||
``` | ||
|
||
### Selenum release dependency | ||
|
||
It appears that the critical dependency jar of this project, [selenium-chromium-driver](https://jcenter.bintray.com/org/seleniumhq/selenium/selenium-chromium-driver/) only available for Selenum release 4.x. | ||
|
||
### See Also | ||
|
||
* [chrome devtools](https://github.com/ChromeDevTools/awesome-chrome-devtools) | ||
project | ||
* [puppeteer online](https://try-puppeteer.appspot.com/) | ||
* [GoogleChrome/puppeteer](https://github.com/GoogleChrome/puppeteer) | ||
* [ChromeDevTools/debugger-protocol-viewer](https://github.com/ChromeDevTools/debugger-protocol-viewer) | ||
* [standalond java cdp clienti](https://github.com/webfolderio/cdp4j) (commecial) | ||
* [cdp4j/javadoc](https://webfolder.io/cdp4j/javadoc/index.html) | ||
|
||
### License | ||
This project is licensed under the terms of the MIT license. | ||
|
||
### Author | ||
[Serguei Kouzmine](kouzmine_serguei@yahoo.com) |
Oops, something went wrong.