8000 CDP PoC by uarlouski · Pull Request #5612 · vividus-framework/vividus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

CDP PoC #5612

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

CDP PoC #5612

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;

import org.vividus.selenium.screenshot.strategies.AdjustingScrollableElementAwareViewportPastingDecorator;
import org.vividus.selenium.screenshot.strategies.CdpShootingStrategy;
import org.vividus.ui.web.action.WebJavascriptActions;
import org.vividus.ui.web.screenshot.WebCutOptions;
import org.vividus.ui.web.screenshot.WebScreenshotParameters;
Expand Down Expand Up @@ -107,6 +108,13 @@

private AShot createAShot(String strategyName, WebScreenshotParameters screenshotParameters)
{
if ("CDP".equals(strategyName))
{
return new AShot().shootingStrategy(
decorateWithCroppingParams(new CdpShootingStrategy(), screenshotParameters)

Check warning on line 114 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/WebAshotFactory.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/WebAshotFactory.java#L113-L114

Added lines #L113 - L114 were not covered by tests
);
}

ShootingStrategy baseShootingStrategy = getBaseShootingStrategy();
ShootingStrategy shootingStrategy;
@SuppressWarnings("checkstyle:Indentation")
Expand All @@ -128,14 +136,19 @@
String.format("Unknown shooting strategy with the name: %s", strategyName));
};
shootingStrategy = decorateWithScrollbarHiding(shootingStrategy, screenshotParameters);
shootingStrategy = screenshotParameters == null
? shootingStrategy
: decorateWithCropping(shootingStrategy, screenshotParameters);
shootingStrategy = decorateWithCroppingParams(shootingStrategy, screenshotParameters);

return new AShot().shootingStrategy(shootingStrategy)
.coordsProvider(new ScrollBarHidingCoordsProviderDecorator(coordsProvider, scrollbarHandler));
}

private ShootingStrategy decorateWithCroppingParams(ShootingStrategy shootingStrategy,
WebScreenshotParameters screenshotParameters)
{
return screenshotParameters == null ? shootingStrategy
: decorateWithCropping(shootingStrategy, screenshotParameters);
}

private ShootingStrategy getBaseShootingStrategy()
{
return ShootingStrategies.scaling((float) javascriptActions.getDevicePixelRatio());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.selenium.screenshot.strategies;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chromium.HasCdp;
import org.vividus.selenium.WebDriverUtils;

import pazone.ashot.ShootingStrategy;
import pazone.ashot.coordinates.Coords;
import pazone.ashot.util.ImageTool;

public class CdpShootingStrategy implements ShootingStrategy

Check warning on line 35 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L35

Added line #L35 was not covered by tests
{
@Override
public BufferedImage getScreenshot(WebDriver driver)
{
return getScreenshot(driver, Set.of());

Check warning on line 40 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L40

Added line #L40 was not covered by tests
}

@Override
public BufferedImage getScreenshot(WebDriver driver, Set<Coords> coords)
{
Map<String, Object> args = new HashMap<>();
args.put("captureBeyondViewport", true);
System.out.println(coords);

Check warning on line 48 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L46-L48

Added lines #L46 - L48 were not covered by tests

if (!coords.isEmpty())
{
Coords elementCoords = coords.iterator().next();
args.put("clip", Map.of(
"x", elementCoords.x,
"y", elementCoords.y,
"width", elementCoords.width,
"height", elementCoords.height,
"scale", 1)

Check warning on line 58 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L52-L58

Added lines #L52 - L58 were not covered by tests
);
}

HasCdp cdp = WebDriverUtils.unwrap(driver, HasCdp.class);
Map<String, Object> results = cdp.executeCdpCommand("Page.captureScreenshot", args);
String base64 = (String) results.get("data");
byte[] bytes = OutputType.BYTES.convertFromBase64Png(base64);

Check warning on line 65 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L62-L65

Added lines #L62 - L65 were not covered by tests

try
{
return ImageTool.toBufferedImage(bytes);

Check warning on line 69 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L69

Added line #L69 was not covered by tests
}
catch (IOException thrown)

Check warning on line 71 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L71

Added line #L71 was not covered by tests
{
throw new UncheckedIOException(thrown);

Check warning on line 73 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L73

Added line #L73 was not covered by tests
}
}

@Override
public Set<Coords> prepareCoords(Set<Coords> coordsSet)
{
return coordsSet;

Check warning on line 80 in vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-web-app/src/main/java/org/vividus/selenium/screenshot/strategies/CdpShootingStrategy.java#L80

Added line #L80 was not covered by tests
}
}
Loading
0