-
Notifications
You must be signed in to change notification settings - Fork 0
[Java] FPS support for local grid using bidi #236
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
4f485b2
ea63119
ce5b3ba
1d17f3c
828ae23
438bb9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
import com.saucelabs.visual.utils.*; | ||
import dev.failsafe.Failsafe; | ||
import dev.failsafe.RetryPolicy; | ||
import java.awt.image.BufferedImage; | ||
import java.lang.reflect.Field; | ||
import java.net.URL; | ||
import java.time.Duration; | ||
|
@@ -24,6 +23,8 @@ | |
import java.util.stream.Collectors; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.openqa.selenium.*; | ||
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; | ||
import org.openqa.selenium.bidi.browsingcontext.CaptureScreenshotParameters; | ||
import org.openqa.selenium.remote.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -617,36 +618,53 @@ public <D> D execute(GraphQLOperation operation, Class<D> responseType) { | |
} | ||
|
||
private String sauceVisualCheckLocal(String snapshotName, CheckOptions options) { | ||
Capabilities caps = driver.getCapabilities(); | ||
Window window = new Window(this.driver); | ||
Rectangle viewport = window.getViewport(); | ||
|
||
byte[] screenshot; | ||
|
||
// clip image if required | ||
Map<String, Object> windowDims = | ||
(Map<String, Object>) | ||
driver.executeScript( | ||
"return { height: window.innerHeight, width: window.innerWidth, dpr: window.devicePixelRatio }"); | ||
double devicePixelRatio = formatDevicePixelRatio(windowDims.get("dpr")); | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boolean fullPage = options.getFullPageScreenshotConfig() != null; | ||
WebElement clipElement = getClipElement(options); | ||
Point scrollOffset = new Point(0, 0); | ||
Point clipOffset = new Point(0, 0); | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Rectangle clipRect = null; | ||
|
||
if (clipElement != null) { | ||
Rectangle clipRect = clipElement.getRect(); | ||
Rectangle clipElementDims = getClipElementDimensions(fullPage, clipElement); | ||
|
||
if (!fullPage) { | ||
scrollOffset = window.getViewport().getPoint(); | ||
} | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
clipRect = | ||
new Rectangle( | ||
(int) Math.round(clipElementDims.getX() * devicePixelRatio), | ||
(int) Math.round(clipElementDims.getY() * devicePixelRatio), | ||
(int) Math.round(clipElementDims.getHeight() * devicePixelRatio), | ||
(int) Math.round(clipElementDims.getWidth() * devicePixelRatio)); | ||
Comment on lines
+641
to
+646
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should belong in a helper of some sort, or at least a function in this class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what reason? It's only 4 lines, and those lines are pretty readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess... it's just that this function is growing very quickly. |
||
|
||
clipOffset = new Point(clipElementDims.getX(), clipElementDims.getY()); | ||
} | ||
|
||
// Scroll to the clipped element | ||
Rectangle newViewport = window.scrollTo(clipRect.getPoint()); | ||
screenshot = driver.getScreenshotAs(OutputType.BYTES); | ||
byte[] screenshot = getScreenshot(fullPage); | ||
|
||
// Restore the original scroll | ||
window.scrollTo(viewport.getPoint()); | ||
// Handle clipping, if rect is present | ||
if (clipRect != null) { | ||
Dimension imageDims = ImageHelpers.getImageDimensions(screenshot); | ||
// bound the clip rect to the image bounds so there is no outside of bounds exception | ||
Optional<Rectangle> offsetRect = | ||
CartesianHelpers.intersect(clipRect, new Rectangle(new Point(0, 0), imageDims)); | ||
|
||
Optional<Rectangle> cropRect = CartesianHelpers.intersect(clipRect, newViewport); | ||
if (!cropRect.isPresent()) { | ||
if (offsetRect.isPresent()) { | ||
screenshot = | ||
ImageHelpers.saveImage( | ||
ImageHelpers.cropImage(ImageHelpers.loadImage(screenshot), offsetRect.get()), | ||
"png"); | ||
} else { | ||
throw new VisualApiException("Clipping would result in an empty image"); | ||
} | ||
|
||
BufferedImage image = ImageHelpers.loadImage(screenshot); | ||
BufferedImage cropped = | ||
ImageHelpers.cropImage( | ||
image, CartesianHelpers.relativeTo(newViewport.getPoint(), cropRect.get())); | ||
screenshot = ImageHelpers.saveImage(cropped, "png"); | ||
viewport = cropRect.get(); | ||
} else { | ||
screenshot = driver.getScreenshotAs(OutputType.BYTES); | ||
} | ||
|
||
// create upload and get urls | ||
|
@@ -663,19 +681,30 @@ private String sauceVisualCheckLocal(String snapshotName, CheckOptions options) | |
Optional.ofNullable(options.getIgnoreElements()).orElse(Collections.emptyList()))); | ||
ignoreRegions.addAll(extractIgnoreSelectors(options)); | ||
|
||
// get adjusted screenshot dimensions | ||
Dimension imageDims = ImageHelpers.getImageDimensions(screenshot); | ||
Rectangle imageDimsDpr = | ||
new Rectangle( | ||
new Point(0, 0), | ||
new Dimension( | ||
(int) Math.round(imageDims.getWidth() / devicePixelRatio), | ||
(int) Math.round(imageDims.getHeight() / devicePixelRatio))); | ||
String deviceName = | ||
String.format("Desktop (%sx%s)", windowDims.get("width"), windowDims.get("height")); | ||
|
||
// make regions relative to viewport | ||
List<RegionIn> visibleIgnoreRegions = new ArrayList<>(); | ||
for (RegionIn region : ignoreRegions) { | ||
// Update the offset to all ignore regions | ||
Point newPoint = | ||
CartesianHelpers.relativeTo(clipOffset, new Point(region.getX(), region.getY())); | ||
region.setX(newPoint.x - scrollOffset.getX()); | ||
region.setY(newPoint.y - scrollOffset.getY()); | ||
|
||
Rectangle regionRect = | ||
new Rectangle(region.getX(), region.getY(), region.getHeight(), region.getWidth()); | ||
|
||
if (CartesianHelpers.intersect(regionRect, viewport).isPresent()) { | ||
Point newPoint = | ||
CartesianHelpers.relativeTo( | ||
viewport.getPoint(), new Point(region.getX(), region.getY())); | ||
region.setX(newPoint.x); | ||
region.setY(newPoint.y); | ||
|
||
// Only add it if the region is visible inside the new image | ||
if (CartesianHelpers.intersect(regionRect, imageDimsDpr).isPresent()) { | ||
visibleIgnoreRegions.add(region); | ||
} | ||
} | ||
|
@@ -695,22 +724,14 @@ private String sauceVisualCheckLocal(String snapshotName, CheckOptions options) | |
} | ||
} | ||
|
||
Capabilities caps = driver.getCapabilities(); | ||
|
||
Map<String, Object> dims = | ||
(Map<String, Object>) | ||
driver.executeScript( | ||
"return { height: window.innerHeight, width: window.innerWidth, dpr: window.devicePixelRatio }"); | ||
String deviceName = String.format("Desktop (%sx%s)", dims.get("width"), dims.get("height")); | ||
|
||
// create snapshot using upload id | ||
CreateSnapshotMutation snapshotMutation = | ||
new CreateSnapshotMutation( | ||
SnapshotIn.builder() | ||
.withBrowser(CapabilityUtils.getBrowser(caps)) | ||
.withBrowserVersion(caps.getBrowserVersion()) | ||
.withDevice(deviceName) | ||
.withDevicePixelRatio(formatDevicePixelRatio(dims.get("dpr"))) | ||
.withDevicePixelRatio(devicePixelRatio) | ||
.withOperatingSystem(CapabilityUtils.getOperatingSystem(caps)) | ||
.withUploadId(uploadResult.getId()) | ||
.withBuildId(this.build.getId()) | ||
|
@@ -734,6 +755,51 @@ private String sauceVisualCheckLocal(String snapshotName, CheckOptions options) | |
return result.result.getId(); | ||
} | ||
|
||
private byte[] getScreenshot(boolean fullPage) { | ||
if (fullPage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested it across all major browsers and with various websites? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BiDi is not currently supported in Safari, but it depends on our target use case whether that might be okay. Otherwise, Diego said this works across Firefox and chromium based browsers. |
||
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); | ||
CaptureScreenshotParameters csp = new CaptureScreenshotParameters(); | ||
csp.origin(CaptureScreenshotParameters.Origin.DOCUMENT); | ||
String screenshot = browsingContext.captureScreenshot(csp); | ||
return OutputType.BYTES.convertFromBase64Png(screenshot); | ||
} | ||
|
||
return driver.getScreenshotAs(OutputType.BYTES); | ||
} | ||
|
||
/** Query the browser for the dimensions and offset of the current clip element. */ | ||
private Rectangle getClipElementDimensions(boolean fullPage, WebElement clipElement) { | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final String getElementDimensions = | ||
"const [clipElement, isFullPage] = arguments;\n" | ||
+ " if (!isFullPage) {\n" | ||
+ " clipElement.scrollIntoView({\n" | ||
+ " behavior: 'instant',\n" | ||
+ " });\n" | ||
+ " }\n" | ||
+ " const rect = clipElement.getBoundingClientRect().toJSON();\n" | ||
+ " return isFullPage ? {\n" | ||
+ " // FPS should use original coordinates of element on page\n" | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ " width: Math.round(rect.width),\n" | ||
+ " height: Math.round(rect.height),\n" | ||
+ " x: Math.round(rect.x + window.scrollX),\n" | ||
+ " y: Math.round(rect.y + window.scrollY),\n" | ||
+ " } : {" | ||
+ " width: Math.round(rect.width),\n" | ||
+ " height: Math.round(rect.height),\n" | ||
+ " x: Math.round(rect.x),\n" | ||
+ " y: Math.round(rect.y),\n" | ||
+ " };"; | ||
|
||
Map<String, Long> clipMap = | ||
(Map<String, Long>) driver.executeScript(getElementDimensions, clipElement, fullPage); | ||
|
||
return new Rectangle( | ||
clipMap.get("x").intValue(), | ||
clipMap.get("y").intValue(), | ||
clipMap.get("height").intValue(), | ||
clipMap.get("width").intValue()); | ||
} | ||
|
||
/** Parse the Selenium parsed value from window.devicePixelRatio into a Double for our API */ | ||
private double formatDevicePixelRatio(Object rawDpr) { | ||
double dpr = 1.0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking, as newer versions of Selenium (4.12+ IIRC) have dropped support for Java 8 so our build step will fail.